English
Español
PCBWAY PCB service

PCBWAY PCB service

PCBONLINE PCB service






Keypad door lock






This should be a cool and easy project. We will conect a small thin Keypad that I've bought from eBay for less than 70 cnts to one Arduino microntroller. You could use any Arduino boar that you want. Using the KeyPad library we will be able to read each of the 16 buttons that this keypad has using just 8 pins of the arduino. The library use is simple. Normally we shoud use two pins for each button to be able to detech esch push. But this library uses 4 pins for columns and 4 pins for rows for this 4x4 keypad. The library is non-blocking which means you can press and hold the key all day long and your Arduino will continue processing the rest of your code. Consider, though, when you are writing your code that every delay() you use will take processing time away from the keypad. Something as short as delay(250) can make the keypad seem very unresponsive. And the same thing will happen if you sprinkle a bunch of delay(10)'s all through your code. The function getKey() returns a key value as soon as you press the key but it does not repeat automatically. Also, when you release the key you can track the key RELEASED event if you are using the eventListener feature of the library.




Now that we know how this keypad should work we can take a look at our schematic. So we will connect this keypad to digital pins D2 to D9 of the microcontroller. We will have a simple code that will read each inserted character in order to create our password. If the password is OK, a servo connected to pin D10 will spin in order to slide a door lock. If not, an error will show up and we have to try again. We also connect a buzzer to pin 11 in order to give sound error signals as well. A push button will be connected to dgital pin D12 and this button will be placed inside of the room. This button will open the door from inside for "emergency" exit. Another push button connected to pin D13 will close the door from inside of the room. Let's take a look at the schematic first.




Remember that to any push button that we add we have to give it a pullup or pulldown resistor. In this case I want to detect HIGH state of the button and that means that the button will normally be in the LOW state. For that we add a 1k ohm resistor from the push button output pin to ground. We supply 5 volts to the oposite pin. In this way every time that the button is pushed we detect a 5 volts voltage which representes a HIGH digital read. Connect the Keypad as shown in the schematic. Also connect the LCD data and clock pins to analog pins A4 and A5. Supply 5V and GND to the servo motor and connect the signal pin to D10 of the microcontroller. All we need now is to upload the next code and pue everything togheder in a case and install the system to the door. Remember, in order to use this i2c LCD module you have to install the i2c liquid crystal library to the Arduino IDE. Download it from the link below.


You can download the i2c Lyquid crystal library here
arduino ohm meter schematic
To install it we just go to Sketch -> Include library and we open the .zip file that we've just downloaded.


If you press a key and the LCD prints another, that means that your ROW and COL pins are inverted or so... Make tests and edit your "hexaKeys[ROWS][COLS]" to match your KEYPAD.


/*Thanks. Remember to visit my Youtube channel
  I've used a i2c LCD screen module and 9g servo motor. 
*/

//LCD config
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4);

#include <Servo.h>
#include <Keypad.h>

//Variables
int mot_min = 90;   //min servo angle  (set yours)
int mot_max = 180; //Max servo angle   (set yours)
int character = 0;
int activated =0;
char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};  

//Pins
Servo myservo;
int buzzer=11;     //pin for the buzzer beep
int external = 12; //pin to inside open
int internal = 13; //pin to inside close

//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','4','7','*'},
  {'2','5','8','0'},
  {'3','6','9','#'},
  {'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  myservo.attach(10); //attach the servo to pin D10
  pinMode(buzzer,OUTPUT); 
  pinMode(external,INPUT);
  pinMode(internal,INPUT); 
  //Init the screen and print the first text
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("    PASSWORD");
  lcd.setCursor(0,1);
  lcd.print("      -***     ");
  //put the servo in the close position first
  myservo.write(mot_min);  
  
}
  
void loop(){
///////////////EMERGENCY OPEN/CLOSE/////////
  if (digitalRead(external))
  {
     myservo.write(mot_max);
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("INSIDE  OPEN");
      activated = 2;
      analogWrite(buzzer,240);
      delay(250);
      analogWrite(buzzer,200);
      delay(250);
      analogWrite(buzzer,180);
      delay(250);
      analogWrite(buzzer,250);
      delay(250);
      analogWrite(buzzer,LOW);
     
      
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("WELLCOME");
      
      lcd.setCursor(2,1);
      lcd.print("ELECTRONOOBS");
      

      lcd.clear();    
      lcd.setCursor(3,0);
      lcd.print("DOOR  OPEN");
      lcd.setCursor(2,1);
      lcd.print("ELECTRONOOBS");
      delay(500);
    
  }

  if (digitalRead(internal))
  {
    myservo.write(mot_min);
    activated = 0;
    character=0;
    Str[6]= '-';
    Str[7]= '*'; 
    Str[8]= '*'; 
    Str[9]= '*';
    Str[10]= ' ';   
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    PASSWORD");    
    lcd.setCursor(0,1);
    lcd.print(Str);
  }
    
///////////////KEYPAD OPEN/CLOSE////////////  
  char customKey = customKeypad.getKey(); //this function reads the presed key
  
  if (customKey){

    if (character ==0)
    {  
    Serial.println(customKey);
    Str[6]= customKey;   
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    PASSWORD");    
    lcd.setCursor(0,1);
    lcd.print(Str);
   
    }

    if (character ==1)
    {  
    Serial.println(customKey);
    Str[7]= customKey;   
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    PASSWORD");    
    lcd.setCursor(0,1);
    lcd.print(Str);
   
    }

    if (character ==2)
    {  
    Serial.println(customKey);
    Str[8]= customKey;   
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    PASSWORD");    
    lcd.setCursor(0,1);
    lcd.print(Str);
   
    }

    if (character ==3)
    {  
    Serial.println(customKey);
    Str[9]= customKey;   
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    PASSWORD");    
    lcd.setCursor(0,1);
    lcd.print(Str);
   
    }

    if (character ==4)
    {  
    Serial.println(customKey);
    Str[10]= customKey;
    activated=1;
   
    }
    character=character+1;
  }

  if (activated == 1)
    {
/*Change your password below!!! 
Change each of Str[6], Str[7], Str[8], Str[9]*/

    if(Str[10]='A' && character==5 && Str[6]=='3' && Str[7]=='0' && Str[8]=='0' && Str[9]=='7' )
    {
      myservo.write(mot_max);
      lcd.clear();
      lcd.setCursor(4,0);
      lcd.print("ACCEPTED");
      activated = 2;
      analogWrite(buzzer,240);
      delay(250);
      analogWrite(buzzer,200);
      delay(250);
      analogWrite(buzzer,180);
      delay(250);
      analogWrite(buzzer,250);
      delay(250);
      analogWrite(buzzer,LOW);
      delay(1000);
      
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("WELLCOME");
      delay(500);
      lcd.setCursor(2,1);
      lcd.print("ELECTRONOOBS");
      delay(1000);

      lcd.clear();    
      lcd.setCursor(3,0);
      lcd.print("DOOR  OPEN");
      lcd.setCursor(2,1);
      lcd.print("ELECTRONOOBS");
      
    }
    else
    {
      lcd.clear();    
      lcd.setCursor(1,0);
      lcd.print("PASSWORD ERROR");
      lcd.setCursor(3,1);
      lcd.print("TRY  AGAIN");
      analogWrite(buzzer,150);
      delay(3000);
      analogWrite(buzzer,LOW);
      character=0;
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';
      activated = 0;
      lcd.clear();    
      lcd.setCursor(4,0);
      lcd.print("PASSWORD");
      lcd.setCursor(0,1);
      lcd.print(Str);   
    }
  }
  if (activated == 2)
    {
    if(customKey == 'B' )
    {
      myservo.write(mot_min);
      activated = 0;
      character=0;
      Str[6]= '-';
      Str[7]= '*'; 
      Str[8]= '*'; 
      Str[9]= '*';
      Str[10]= ' ';   
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("    PASSWORD");    
      lcd.setCursor(0,1);
      lcd.print(Str);
     
    }
  }  
}

 

So, upload this code. The "customKeypad.getKey()" function will read the pressed key. The function will read just one key at a time and if you keep pushing the button it won't start reading more keys. You can see in the code that first we use the "internal" and "external" pins to open/close the door using the two push buttons. Then we read the pressed key and start editing the password "characters". When the all the 4 charracters were inserted we check if those are the same as the one that we want. If they are we open the door with the servo. If not, we give an error.

To change your password just go to the line from the photo below and change each of the four Str values. In this example the password is 3007. The "A" character will be the "OK" and the "B" will be the "close".



Un vídeo publicado por ELECTRONOOBS (@electronoobs) el



Now that everything is ready, turn on the Arduino and start typeing your password. Press "A" for OK when all the 4 characters were inserted. To close the door press "B". I hope you'll enjoy this tut.




See some fotos




See also the bluetooth part of this project