Keyless Entry / Arduino

Keyless Entry System

Enter the correct code into the keypad and the servo will unlock the door. Enter the incorrect code and Sorry you stay out.
There was a couple of stipulations i wanted in my Keyless Entry system:
Keyless Entry

  1. Helper Light. A light that would come on whenever a key was pressed. Then off after the preset time limit.
  2. An override switch so the servo could be operated without the keypad.
  3. Some indication that the pass code entered was correct or not.
  4. Powered by a wall charger.
  5. Be able to operate manually during a power outage

With these stipulations i started out to build.

The Keypad works on a matrix system. Pressing any key will involve 2 wires.
Example if you press number 8 it will effect the wire ROW3 and CLO2. By this the micro controller can determine what key was pressed with only 7 wires from the keypad.
While this keypad schematic is not correct to this build the theory is still the same.
This build does not use the external resistors. It takes advantage of the internal pullup resistors in the Atmega328.

Keypad in Arduino Playground.

The Circuit

Keyless Entry Schematic

I couldn’t find a Keypad in Fritzing so i used 3 mystery parts where it says KEYPAD in the diagram.

Its probably better to use a pullup resistor on the switch instead of a pull down, but either way will work.


I had some help from  maewert  for the Helper Light.
The Bypass switch the hardest for me. It was a collaboration between sath02 and my self. I am always greatful to these guys for there knowledge.

If you don’t have the libraries needed for the sketch they are here for downloading.


Arduino on a Breadboard

Keypad Coding

Keypad Tutorial 

#include <Password.h>
#include <Keypad.h>
#include <Servo.h> //tells to use servo library
int pos = 5;    // variable to store the servo position
int button = A0;  // The button will be on Pin 7
Servo myservo; //declares servo
#define DELAY       20  // Delay per loop in ms

Password password = Password( “4444” ); //password to unlock door, can be changed

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {

byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6,};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long offtime;
boolean  helper_light_is_on = false; // helper light off
// variables will change:
int buttonState     = 0;  // variable for reading the pushbutton status
int lastButtonState = 0;

void handle_button()
// read the state of the pushbutton value:
buttonState = digitalRead(button);
// check if the pushbutton is pressed.
if (buttonState != lastButtonState) {
// Lock release
digitalWrite(10, HIGH);
delay(1000); // this delay could be changed to suitable value
digitalWrite(10, LOW);
myservo.write(160);// move to lock position after 5 second
lastButtonState = buttonState;
void setup()
pinMode(10, OUTPUT);  //Helper light
pinMode(11, OUTPUT);  //green light
pinMode(12, OUTPUT);  //red light
pinMode(button, INPUT);
myservo.attach(9); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
void loop()

//take care of some special events
void keypadEvent(KeypadEvent eKey)
switch (keypad.getState())
// a key is pressed so light the helper light
helper_light_is_on = true;
offtime = millis() + 10000;  // set the offtime for 30 seconds in the future

Serial.print(” enter: “);
switch (eKey)
case ‘*’:
case ‘#’:

void checkPassword()
if (password.evaluate())  //if password is right unlock door
Serial.println(” Accepted”);
//Add code to run if it works
myservo.write(90); //160deg
digitalWrite(11, HIGH);//turn on Green Led
delay(3000); //wait 5 seconds
digitalWrite(11, LOW);// turn off Green Led
Serial.println(” Denied”); //if passwords wrong keep door locked
//add code to run if it did not work
digitalWrite(12, HIGH); //turn on RedLed
delay(3000); //wait 5 seconds
digitalWrite(12, LOW);//turn off Red Led
if (digitalRead(button) == LOW)
//for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees
{                                  // in steps of  degree
Serial.println(” Opened “);
myservo.write(90); //160deg
myservo.write(pos);              // tell servo to go to position in variable ‘pos’

if (digitalRead(button) == HIGH)
//for(pos = 90; pos>=90; pos-=90)  // goes from 90 degrees to 0 degrees
Serial.println(” Stay Closed “);
myservo.write(pos);               // tell servo to go to position in variable ‘pos’
myservo.write(5); //160deg
myservo.write(pos);              // tell servo to go to position in variable ‘pos’

// this routine turns off the light when the timer expires
void process_helper_light(void)
if (helper_light_is_on)
if (millis() >= offtime)
digitalWrite(10,LOW);  //turn off the helper light
helper_light_is_on = false;

[box color=”#985D00″ bg=”#FFF8CB” font=”verdana” fontsize=”14 ” radius=”20 ” border=”#985D12″ float=”right” head=”Major Components in Project” headbg=”#FFEB70″ headcolor=”#985D00″]
  • Atmega328P-UP Bootloaded
  • 28 Pin socket
  • 16Mhz Crystal
  • 22pF caps ( 2 )
  • LM317 voltage regulator
  • LM7805 Voltage regulator
  • 10uF Caps ( 2 )
  • #104 Caps ( 2 )
  • 220R Resistor
  • 1K Resistor
  • 470R Resistor
  • 10K Resistor
  • LED of your choice
  • Tact Switch
  • 5mm Power Connector
  • 2 pin female header
  • 16 Pin Female header
  • Perf Board 70mm x 90mm
  • Flat wire strips 2 wire   4 wire   7 wire  1′ long
  • Servo


For more detail: Keyless Entry / Arduino

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

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

Scroll to Top