Skip to content

Arduino Door Lock with keypad + Solenoid / Relay and changeable code

 

 

       Hi, and welcome to this tutorial, today I’m making a door lock project based on a Arduino board, keypad, LCD i²c screen, and I’ll use for the lock a solenoid and a relay, you can check my tutorial about the finger print door lock where I used a lock that uses a dc motor, so it depends on your lock system you’ll have to chose a wiring, codes and adapt them.

      N.B: For a real project, I don’t recommend using a solenoid at all, but rather hack a lock that can be opened by both electronic and mechanic lock and adapt your project for it.

 

Parts

So for this project we will need those components, alongside some jump wires, and a power supply of 12V Sorry I didn’t add it here:

 

The Push Button is meant to open the lock from inside, you can remove it if you want, the resistor is for debounce.

I used 4×4 Keypad you can use 3×4 but you’ll need to modify somethings in the code like for confirmation I use ‘A’ you can change it to ‘*’ or ‘#’.

The solenoid is powered by 12V external power supply, and driven by the IRF510N MOSFET transistor.

The Transistor is used as a switch and it’s better to use an N-channel, the IRF510N is pretty popular when used with an Arduino, when you apply a 5V voltage across the Gate and the Source, the transistor becomes like a closed switch between the Drain and the Source, and it doesn’t need any resistor like bipolar ones.

And if there’s no voltage applied the transistor acts like an open switch, and this how we control the solenoid.

For the other example I’m using a 1 channel  relay module, it works with 3.3V, and we control its input like controling the transistor, the only difference is that they are inverted (we’ll see in the code).

Algorithm flowchart

To make things easy to understand here’s the flowchart, it’s not completly detailed but it’s an overview of the code.

Untitled Diagram.png
Lock algorithm diagram

 

Wirings

Wiring 1: For the solenoid use

Wiring 1
Solenoid keypad lock

Wiring 2: For the relay use

Wiring 2

Relay keypad lock

 

N.B: You can use the relay to control any electric lock up to 250VAC, you can use it also to control the Solenoid…

Libraries

The only library I used is the LCD i²c NewLiquidCrystal: Download here
Keypad and EEPROM libraries are already installed.

Codes

Here you can download the codes I’ve created for the project:

The codes are exactly the same the only thing is that you switch between (LOW and HIGH) to open the lock.

Just remember first time you must upload the code and change the passcode then uncomment some lines (read the code to find them (they’re in setup)) and reupload the code so it can read the passcode from the EEPROM.
IT’S DONE ONLY ONCE.

Test

Test 1
Standby mode – waiting for keys or push button
Test 2
If you press the ‘*’ key it asks you to type the code
Test 3
Enter the correct code and press A to confirm
Test 4
A welcome message will show otherwise a wrong one
Test 5
System goes back to standby mode
Test 6
when you press ‘#’ it asks you to type the old code to change it
Test 7
Enter the old code and A to confirm it
Test 8
Old code is correct now you can change it
Test 9
Enter the new code and press A
Test 9_1.jpg
Confirm the new code again and press A
Test 10
If codes are matching it changes and stores in the EEPROM
Test 11
Otherwise the codes doesn’t change

Well that was the test of the project, it’s the same for the solenoid or relay, and the push button opens from the inside if you want or you can remove it.

I hope it’s useful and if you have any problem or question leave a comment or contact my FB page…

Yassine View All

Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

15 thoughts on “Arduino Door Lock with keypad + Solenoid / Relay and changeable code Leave a comment

    • You can make 4 initial passcodes, and before you can change anyone of them you ask the user which one to changes ( you can do it by a switch case and test on the number entred 1-4, other than that is an error), then when getting the passcode you can compare it with the 4 codes and open the correspondant lock…

      It just need some re arrangement, and add few lines.

      Like

  1. I did this project too.How can I write the code that everyone has different passwords and these passwords are saved in EEPROM.Could you help me ?would you return to me?

    Like

      • I want each password to belong to other users.When I enter A password, the door will open. When you enter B password, the door will open.I do not know how to do it.

        Like

      • Well this code contains the eeprom storage and the ability to change code… you better start with something that has only the code comparing, and instead of comparing the input to one code, you gonna try to compare it to both, and if one is correct the door will open.

        Like

  2. the project code is ready but the door is opened with only one password.Can you add +4 password to eeprom memory.so there will be 5 passwords in total.Can you write your email address.I will send you project code.

    Like

    • What do you mean by adding 5 passcodes? Will they have the ability to be changed later through the keypad or not?

      As I told you if you don’t need that just add the other codes arrays and inside the code compare the input to all of them, if one is correct it will open.

      If you want to keep the ability to change the code, once the code is entered put a flag on that code previously stored and change it.

      Like

  3. i was using your code with some changes. i dont know why i m unable to use the default password. it is always showing wrong password. just changed the display with oled

    Like

    • Hello, did you uncomment the “for loop that contains getting the code from the EEPROM”?

      First you should upload the sketch as it is do not uncomment that line, then change the passcode from 6601 to anything 0000-9999 (if you want longer passcode change it from the program first).

      Then when you change the passcode go back to the skech and uncomment those lines in the setup and reupload the sketch.

      Explanation: What happens here is that everytime the Arduino starts it will go to the EEPROM and look for the correct passcode, the first time it will get unkown values stored there and can’t be used that’s why you comment those lines the first time then you change the code, chaning the code will store the new code in the EEPROM… and when you reupload the sketch with the uncommented lines the Arduino will look for the passcode in the EEPROM and will find what you stored there.

      To sum up keep for loop commented, upload the sketch, change the passcode, uncomment the for loop and reupload the sketch.

      Like

  4. Hey, I’m planning on using the circuit with the use of a solenoid for a safe locking mechanism. I was wondering, what would happen if the battery pack died while the door was locked? If I was able to access the Arduino board while it was locked would I be able to plug in a 12v supply to the Arduino board? or would this not activate the solenoid once the correct code is entered? any ideas.

    Like

    • The code is stored within the the EEPROM, when the Arduino resets it retrieves the New code that you’ve set and can open the door.
      Of course always use an emergency power source unit.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: