Arduino Projects

Password based solenoid door lock system using Arduino and Keypad

Learn how to build Solenoid door lock system using Arduino Microcontroller and 4x4 keypad

Overview:

In this Project lets learn how to build a Password based door lock system by interfacing Arduino with 4×4 Keypad to enter the passcode. We also connect an LCD display to make ease of entering passwords and changing the passwords without hardcoding Arduino Again.  We are using a Solenoid lock which locks and unlocks the doors by entering the password through the keypad and LCD display is used to display the status and messages.

With this Project we can build a security system which works with password. In day today life everything is becoming simple and advanced, previously to lock something we used to have a padlocks, combination locks. But due to increase in thefts and technology, new type of locks such as electronic locks, smart locks were invented and people are using them widely to protect their assets.

password based door lock using arduino locker safe

From these couple of years Fingerprint based locks, RFID based locks, Smart phone app based locks, Face recognition locks are trending, Password based door locks are one of them. So, lets use the features of Arduino microcontroller and keypad to operate the Solenoid lock using password. You can integrate this project with even lockers.

Bill of Materials:

Here are the required components along with Amazon buying links at best prices.
Product NameQuantityamazon logoamazon logo india
Arduino Uno Board1https://amzn.to/3H4cKxZhttps://amzn.to/3638aTS
4x4 keypad1https://amzn.to/3JsTuwnhttps://amzn.to/3ubjxSi
16x2 LCD display with I2C adapter1https://amzn.to/3I3Uaaxhttps://amzn.to/363ki7F
12v Solenoid lock1https://amzn.to/3KWFKu2https://amzn.to/3wkEjBs
Relay module 5v1https://amzn.to/3N5L8gshttps://amzn.to/3wqHmIq
Push button1https://amzn.to/3uc4gRhhttps://amzn.to/3ubrLdc
10k ohm resistor1https://amzn.to/3hBlqlqhttps://amzn.to/3sXBKmI
Few connecting wireshttps://amzn.to/3H2BV4ehttps://amzn.to/3J0WVu2

Password based Solenoid door lock system using Arduino Circuit Diagram:

Connect all the above required components as shown in the below circuit diagram.

Password based Solenoid door lock system using Arduino Circuit Diagram

As you can see from the above circuit diagram we connected 8 pins of 4×4 keypad to Arduino UNO digital pins from pin9 to pin2. 4×4 keypad has total of 8 pins, first 4 pins for rows control and another 4 pins for column control which we mentioned in the above image.

Here we are using 16X2 LCD display with I2C adapter to reduce the connecting cables as it takes only 4 connecting wires. It is connected to Arduino as Vcc and GND power pins of LCD display are connected to 5V and GND pins of Arduino. I2C pins SDA and SCL of LCD display are connected to A4 and A5 respectively.

A 5v relay module is connected as the power pins VCC and GND are connected to 5v pin and GND pins of Arduino, and the IN pin from relay is connected to digital pin 11 of Arduino.

And as we are using a 12volts Solenoid Lock for Arduino we need to get a separate 12volts power supply either from the 12 volts battery of from any 12volts adapter. Connect the solenoid lock in series with relay module through Normally Open (NO) connection as shown in the circuit diagram.

And finally we are connecting a push button as the bottom legs are connected to 5v and GND, as 5v is high for it we are using a 10Kohm resistor connected in series to reduce the current flow. and the upper right leg is connected to pin 10 of Arduino.

If you want to connect an LCD display without I2C adapter please refer this Article: Interfacing LCD Display with Arduino in detail

Concept and Working of Password based door lock system using Arduino

As we connected the lock to relay in Normally Open condition the solenoid lock is in locked position when voltage is not passed through it.  When we upload the code and turn the this DIY password door lock device, the lock is still in locked position till we enter the correct password.

password door lock arduino welcome message

Through LCD display we can view the status of the Lock and Guide you to open the lock by entering the password. The default password is “1234”. To enter password press * button and enter 1234 through keypad and to confirm press A button. Here A button is used as OK button. So when you enter the correct password Arduino send the signal to relay module and it closes the connection making the lock to open.

password door lock arduino unlocking step

password door lock arduino locking and unlocking

You can change the default password to your desired one by pressing # button on keypad. Which first asks you to enter the old password and then to enter the new desired password and confirm by pressing A.

password based door lock using arduino password changing step

Here we added an additional function using a push button, when you click the button the device gets unlocks without the password. we can place this button inside the door if you are using for the room.

To Lock the device press B button..

  • Button A – Confirm or OK
  • Button B – Lock the door
  • Button * – Enter password to unlock
  • Button # – To change the password.
  • Push button – Unlock without password.

Also read: How to control Servo motor with Arduino

Program code:

Connect Arduino UNO board to PC where Arduino IDE is installed, Choose the correct port and board as Arduino UNO from Tools menu and install the required libraries from the Library manager of download from the below links and extract them in Arduino Libraries folder.

Required Libraries:

  • Download LiquidCrystal_I2C – Link
  • Download Keypad – Link

Next copy the below code and paste it in IDE workspace and press upload button. That’s it the code will be uploaded.

//**** Password based Solenoid door lock system using Arduino ****//
// Created by https://www.circuitschools.com 

#include <Keypad.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>

int Button=10;             //Push Button
int Lock = 11;    // relay
const byte numRows= 4;          //number of rows on the keypad
const byte numCols= 4;          //number of columns on the keypad

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

char keypressed;                 //Where the keys are stored it changes very often
char code[]= {'1','2','3','4'};  //The default code, you can change it or make it a 'n' digits one

char check1[sizeof(code)];  //Where the new key is stored
char check2[sizeof(code)];  //Where the new key is stored again so it's compared to the previous one

short a=0,i=0,s=0,j=0;          //Variables used later

byte rowPins[numRows] = {A0,A1,A2,A3}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols]= {A4,A5,3,2}; //Columns 0 to 3

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows

void setup()
         {
            lcd.init();                    // initialize the lcd
      lcd.backlight();
      lcd.print("CircuitSchools");
      lcd.setCursor(0,1);
      lcd.print("Password Lock");
      delay(2000);
      lcd.clear(); 
      lcd.print("* to unlock");
      lcd.setCursor(0,1);
      lcd.print("B to lock");
      delay(2000);
      lcd.clear(); 
      
          lcd.print("Press* to unlock");      //What's written on the lcd you can change
          pinMode(Button,INPUT);
      pinMode(Lock, OUTPUT);
 
         //  for(i=0 ; i<sizeof(code);i++){        //When you upload the code the first time keep it commented
//            EEPROM.get(i, code[i]);             //Upload the code and change it to store it in the EEPROM
//           }                                  //Then uncomment this for loop and reupload the code (It's done only once) 
         }

void loop()
{
  keypressed = myKeypad.getKey();               //Constantly waiting for a key to be pressed
    if(keypressed == '*'){                      // * to open the lock
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Enter code");            //Message to show
            ReadCode();                          //Getting code function
                  if(a==sizeof(code))           //The ReadCode function assign a value to a (it's correct when it has the size of the code array)
                  OpenDoor();                   //Open lock function if code is correct
                  else{
                  lcd.clear();               
                  lcd.print("Wrong code");          //Message to print when the code is wrong
          lcd.setCursor(0,1);
                  lcd.print("Press * to Unlock");
                  }
            delay(2000);
            lcd.clear();
            lcd.print("Standby");             //Return to standby mode it's the message do display when waiting
        }

     if(keypressed == '#'){                  //To change the code it calls the changecode function
      ChangeCode();
      lcd.clear();
      lcd.print("Standby");                 //When done it returns to standby mode
    lcd.setCursor(0,1);
      lcd.print("Press * to Unlock");
     }

     if(digitalRead(Button)==HIGH){      //Opening by the push button
      digitalWrite(Lock, HIGH);    
     }
   if(keypressed == 'B'){                  //To change the code it calls the changecode function
      
      lcd.clear();
    digitalWrite(Lock, LOW);
      lcd.print("Locked: Press * to ");
      lcd.setCursor(0,1);
                  lcd.print("to unlock!!"); 
     }        
}

void ReadCode(){                  //Getting code sequence
       i=0;                      //All variables set to 0
       a=0;
       j=0; 
                                     
     while(keypressed != 'A'){                                     //The user press A to confirm the code otherwise he can keep typing
           keypressed = myKeypad.getKey();                         
             if(keypressed != NO_KEY && keypressed != 'A' ){       //If the char typed isn't A and neither "nothing"
              lcd.setCursor(j,1);                                  //This to write "*" on the lcd whenever a key is pressed it's position is controlled by j
              lcd.print("*");
              j++;
            if(keypressed == code[i]&& i<sizeof(code)){            //if the char typed is correct a and i increments to verify the next caracter
                 a++;                                              
                 i++;
                 }
            else
                a--;                                               //if the character typed is wrong a decrements and cannot equal the size of code []
            }
            }
    keypressed = NO_KEY;

}

void ChangeCode(){                      //Change code sequence
      lcd.clear();
      lcd.print("Changing code");
      delay(1000);
      lcd.clear();
      lcd.print("Enter old code");
      ReadCode();                      //verify the old code first so you can change it
      
            if(a==sizeof(code)){      //again verifying the a value
            lcd.clear();
            lcd.print("Changing code");
            GetNewCode1();            //Get the new code
            GetNewCode2();            //Get the new code again to confirm it
            s=0;
              for(i=0 ; i<sizeof(code) ; i++){     //Compare codes in array 1 and array 2 from two previous functions
              if(check1[i]==check2[i])
              s++;                                //again this how we verifiy, increment s whenever codes are matching
              }
                  if(s==sizeof(code)){            //Correct is always the size of the array
                  
                   for(i=0 ; i<sizeof(code) ; i++){
                  code[i]=check2[i];         //the code array now receives the new code
                  EEPROM.put(i, code[i]);        //And stores it in the EEPROM
                  
                  }
                  lcd.clear();
                  lcd.print("Code Changed");
                  delay(2000);
                  }
                  else{                         //In case the new codes aren't matching
                  lcd.clear();
                  lcd.print("Codes are not");
                  lcd.setCursor(0,1);
                  lcd.print("matching !!");
                  delay(2000);
                  }
          }
          else{                     //In case the old code is wrong you can't change it
          lcd.clear();
          lcd.print("Wrong");
          delay(2000);
          }
}

void GetNewCode1(){                      
  i=0;
  j=0;
  lcd.clear();
  lcd.print("Enter new code");   //tell the user to enter the new code and press A
  lcd.setCursor(0,1);
  lcd.print("and press A");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("and press A");     //Press A keep showing while the top row print ***
             
         while(keypressed != 'A'){            //A to confirm and quits the loop
             keypressed = myKeypad.getKey();
               if(keypressed != NO_KEY && keypressed != 'A' ){
                lcd.setCursor(j,0);
                lcd.print("*");               //On the new code you can show * as I did or change it to keypressed to show the keys
                check1[i]=keypressed;     //Store caracters in the array
                i++;
                j++;                    
                }
                }
keypressed = NO_KEY;
}

void GetNewCode2(){                         //This is exactly like the GetNewCode1 function but this time the code is stored in another array
  i=0;
  j=0;
  
  lcd.clear();
  lcd.print("Confirm code");
  lcd.setCursor(0,1);
  lcd.print("and press A");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("and press A");

         while(keypressed != 'A'){
             keypressed = myKeypad.getKey();
               if(keypressed != NO_KEY && keypressed != 'A' ){
                lcd.setCursor(j,0);
                lcd.print("*");
                check2[i]=keypressed;
                i++;
                j++;                    
                }
                }
keypressed = NO_KEY;
}

void OpenDoor(){             //Lock opening function open for 3s
  lcd.clear();
  lcd.print("Unlocked:Welcome");
  digitalWrite(Lock,HIGH);
  }

After uploading the code You can see the welcome messages on LCD display and a message “Press * to Unlock“. The password based security lock system using Solenoid lock is ready to use as explained in the working section above.

Also read: Barcode Scanner using Arduino

Important Notes and Troubleshoot guide:

  • The Solenoid Lock will be locked only when you press button B.
  • Push button is used to unlock the lock without password.
  • # button is used to change the password, to reset the password to default press reset button on Arduino board.
  • If you are using 12V battery for Solenoid lock, if battery charge is empty the lock wont gets unlocked until 12 volts is passed through it.
  • If the LCD display or relay is not working with PC connected use external 5v power supply using Mobile charger or adapter.

If you like this project please subscribe to our YouTube Channel “Circuit Schools to encourage us to publish more interesting projects. If you have any doubts write to us from below comment section.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

5 Comments

  1. There are 2 issues with the code.
    1- if code changed for more or less than 4 digits, code dont work. It always says wrong password.
    2- there should be 1 backup password (fixed/not changeable) . so that user can change the password when he/she forget the set password.

    Note: if you fix these two issues, then it would be the best project.

  2. Court<<This idea I love , now I going back to my book to learn CPP because I love anything automated. I can design but not for programmable ic but now you have given me choice to comeback. My previous project used to be bulky due to big components I uses. I need in my next project to design circuit that I will program android ap to control it with through WiFi and internet medium
    Thanks for you encouragement av lost my entire Hope before but now is still early. I love CCT. SCH.;

  3. Dear Sir,
    Can you add some more code to make the changecode function like long press “#” or mulitiple keys
    at the code line number 80 if (keypressed == ‘#’){
    because a single press by mistake on “#” is goes to changecode function.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button