Table of Contents
- How a Keypad Lock Works
- Components Required
- Wiring Diagram
- Arduino Code
- EEPROM PIN Storage
- Improvements and Extensions
- FAQ
How a Keypad Lock Works
A keypad door lock with Arduino replaces a traditional key with a secret PIN code. When the correct PIN is entered on a 4×4 matrix keypad, Arduino activates a solenoid or electric strike to release the door. This type of access control is widely used in Indian offices, server rooms, and home gates costing Rs 500-1,500 in components versus Rs 3,000-8,000 for commercial smart locks.
The 4×4 matrix keypad has 16 keys (0-9, A-D, *, #) but uses only 8 GPIO pins via a matrix scanning technique. The solenoid is a 12V device that pulls a metal plunger when energised, releasing a mechanical latch. A transistor or MOSFET is required to drive the solenoid from Arduino 5V logic.
Components Required
- Arduino Uno or Nano
- 4×4 membrane matrix keypad (Rs 40-80)
- 12V solenoid lock (Rs 200-400) or 12V electric strike
- NPN transistor (TIP120 or IRF520 MOSFET)
- 1N4007 flyback diode (protects transistor from solenoid back-EMF)
- 1k ohm base resistor, 12V/1A power supply
- Small buzzer (Rs 15-30) for feedback tones
- Red and green LEDs with 220 ohm resistors
Wiring Diagram
Keypad column/row pins connect to Arduino D2-D9. Solenoid: + to 12V supply, – to MOSFET drain. Flyback diode across solenoid (cathode to +12V). MOSFET gate to Arduino D10 via 1k resistor. MOSFET source to GND. Arduino GND shared with 12V supply GND.
Arduino Code
#include <Keypad.h>
#include <EEPROM.h>
const byte ROWS=4, COLS=4;
char keys[ROWS][COLS]={{"1","2","3","A"},{"4","5","6","B"},{"7","8","9","C"},{"*","0","#","D"}};
byte rowPins[4]={2,3,4,5}, colPins[4]={6,7,8,9};
Keypad kpad=Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);
#define SOLENOID 10
#define BUZZER 11
String inputPIN="", storedPIN="1234";
void setup(){ pinMode(SOLENOID,OUTPUT); }
void loop(){
char key=kpad.getKey();
if(key){ tone(BUZZER,1000,100);
if(key=='#'){ if(inputPIN==storedPIN) unlock(); else denied(); inputPIN=""; }
else if(key=='*') inputPIN="";
else inputPIN+=key;
}
}
void unlock(){ digitalWrite(SOLENOID,HIGH); delay(3000); digitalWrite(SOLENOID,LOW); }
void denied(){ tone(BUZZER,200,500); }
EEPROM PIN Storage
Store the PIN in EEPROM so it survives power cycles. A dedicated change-PIN sequence using master PIN + new PIN + confirm prevents accidental changes. Reserve EEPROM bytes 0-5 for the PIN (up to 6 digits). EEPROM is rated 100,000 write cycles – read on startup and write only on PIN change.
Improvements and Extensions
- Wrong-PIN lockout: After 3 wrong attempts, lock for 30 seconds and sound alarm.
- RTC time-based access: Add DS1307 to restrict access to office hours only.
- Logging: SD card or ESP8266 to log entry attempts with timestamps.
- GSM alert: SIM800L to send SMS on wrong PIN attempts to owner number.
Frequently Asked Questions
What voltage does a solenoid door lock need?
Most Indian solenoid door locks require 12V DC at 500mA to 1A. Never connect directly to Arduino 5V – the solenoid will not operate and you risk damaging GPIO pins. Always use a dedicated 12V/1A adapter and a transistor or MOSFET as the switch.
Can I use a relay instead of a transistor to drive the solenoid?
Yes – a 5V relay module (Rs 20-40) can switch the 12V solenoid circuit. However, a MOSFET (IRF520) or transistor (TIP120) is more reliable – relays have mechanical wear rated 100,000 cycles, audible click, and slower switching. For a security application with multiple daily operations, a solid-state MOSFET is preferred.
How do I prevent brute-force PIN attacks?
Implement exponential backoff: after 3 wrong PINs, lock input for 30 seconds. After 6 wrong PINs, lock for 5 minutes. Store attempt count in EEPROM so power cycles do not reset the lockout. Sound a loud alarm buzzer during lockout to alert occupants.
What PIN lengths are supported?
Variable-length PINs terminated by the # key. Minimum recommended: 4 digits. Maximum practical: 8 digits. Adding a second factor (RFID card + PIN) dramatically improves security even with a short PIN.
Add comment