Skip to content

Uno · Build guide

Arduino Keypad Door Lock with Servo, I2C LCD and Lockout

Build a password door lock with an Arduino Uno, a 4x4 membrane keypad, an SG90 servo bolt and an I2C LCD. Masked PIN entry, a 30-second lockout after three wrong tries, a change-PIN mode and a version you can run in the browser first.

Arduino Keypad Door Lock with Servo, I2C LCD and Lockout
Difficulty
Intermediate
Read time
18 min
Board
Uno
Author
Shantanu Kumar Mohanty
Published
Nov 20, 2026
Views
0

What You Will Build

Quick answer: wire a 4x4 keypad to Arduino Uno pins D9 to D2, a servo to D10 and an I2C LCD to A4/A5, then load the sketch below. Type 1234 and press # and the servo swings the bolt open for five seconds. Three wrong PINs block the keypad for 30 seconds.

This is the classic keypad lock, built the way a real product would behave rather than the bare demo most tutorials stop at:

  • Masked entry: the LCD shows ****, never the digits
  • # to confirm, * to clear, like a real door panel
  • Auto re-lock five seconds after opening
  • Lockout: three wrong PINs disable the keypad for 30 seconds with a countdown
  • Change PIN: press A while unlocked and enter a new four-digit code
  • Feedback: a key click, a green LED when open and a red LED when locked

You will learn how a matrix keypad is scanned with only eight wires, how to build the program as a small state machine, and how to drive a servo and an I2C display from the same sketch without one blocking the other.

Components

9 items
NameQtyLink
Arduino Uno R3×1—
4x4 membrane keypad (8-pin)×1—
SG90 micro servo (or MG90S for a heavier bolt)×1—
16x2 LCD with I2C backpack (PCF8574, address 0x27 or 0x3F)×1—
Active buzzer, 5 V×1—
Green LED + 220 Ω resistor×1—
Red LED + 220 Ω resistor×1—
Breadboard and jumper wires×1—
5 V 2 A supply for the servo (recommended for a real door)×1—

Need these parts? Get this build sheet by email

Parts list with buy links, plus new builds as they're published. By subscribing you agree to our Privacy Policy. Unsubscribe anytime.

⚠️ Warning

A small SG90 is fine for a drawer latch or a cabinet demo, but it is **not** a security device. For a real door use a 12 V solenoid lock or an electric strike switched by a relay or MOSFET, keep a mechanical override, and never make the lock the only way out of a room.

How a 4x4 Keypad Is Read

A 16-key membrane keypad has no chip inside. It is a grid of four row wires crossing four column wires, and each key simply joins one row to one column when pressed. That is why it needs only 8 pins instead of 16.

The Keypad library scans the grid hundreds of times a second:

  1. Every row pin is set as an input with its internal pull-up, so it reads HIGH.
  2. One column at a time is driven LOW.
  3. If a key in that column is held, its row is pulled LOW too, and the library knows exactly which key it is.
  4. The column is released and the next one is driven LOW.
Keypad pinFunctionUno pin
1 (R1)Row 1: 1 2 3 AD9
2 (R2)Row 2: 4 5 6 BD8
3 (R3)Row 3: 7 8 9 CD7
4 (R4)Row 4: * 0 # DD6
5 (C1)Column 1: 1 4 7 *D5
6 (C2)Column 2: 2 5 8 0D4
7 (C3)Column 3: 3 6 9 #D3
8 (C4)Column 4: A B C DD2

Pin 1 is usually on the left when the keys face you, but cheap keypads vary. If keys come out wrong, the troubleshooting section shows how to fix the mapping in code instead of rewiring.

The Lock as a State Machine

The sketch is always in exactly one of four states, and each key means something different depending on the state:

StateWhat the keys doLeaves the state when
LOCKEDDigits build the PIN, * clears, # checks itRight PIN (to UNLOCKED) or third wrong PIN (to LOCKOUT)
UNLOCKEDA starts a PIN change, D locks at once5 s pass or D is pressed
LOCKOUTIgnored30 s pass
NEW_PINDigits build the new PIN, # saves it# with four digits

Writing it this way keeps every rule in one place and makes features like the lockout a few lines rather than a tangle of flags.

Try It in the Simulator First

The complete circuit and sketch run in the SolderHub Simulator below, with the same pins as this guide. Press and hold keys on the keypad itself: type 1 2 3 4 and # and watch the servo turn, the green LED light and the LCD change. Then enter a wrong PIN three times to see the lockout countdown.

Try it in the simulator

Wiring

PartPart pinArduino Uno
KeypadR1 to R4D9, D8, D7, D6
KeypadC1 to C4D5, D4, D3, D2
SG90 servoSignal (orange)D10
SG90 servoVCC (red) / GND (brown)5V / GND
Buzzer+ / −D11 / GND
Green LEDAnode via 220 ΩD12
Red LEDAnode via 220 ΩD13
LCD I2CVCC / GND5V / GND
LCD I2CSDA / SCLA4 / A5

D10 is used for the servo because the Servo library takes over Timer1, which also disables PWM on D9 and D10. Neither pin needs PWM here, so nothing is lost.

Powering the Servo

An SG90 can draw over 500 mA for a moment when it starts moving. Powered from the Uno's 5V pin through USB that is usually fine on the bench, but if the LCD flickers or the Uno resets when the bolt moves, power the servo from a separate 5 V supply and join its ground to the Uno's GND. Without a shared ground the signal has no reference and the servo jitters.

The Code

Install two libraries from Sketch → Include Library → Manage Libraries: Keypad by Mark Stanley and Alexander Brevig, and LiquidCrystal I2C by Frank de Brabander. Servo and Wire come with the IDE. If your LCD stays blank, run an I2C scanner and change 0x27 to the address it finds (often 0x3F).

C++

Key Parts of the Code Explained

CodeWhat it does
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4)Tells the library which pins are rows and columns and what each key means
keypad.getKey()Returns a key once when it is pressed, or 0 when nothing new happened, and handles debouncing
enum State { LOCKED, UNLOCKED, LOCKOUT, NEW_PIN }The four states the lock can be in
showMasked()Prints one * per digit typed, so nobody can read the PIN over your shoulder
strcmp(entry, pinCode) == 0Compares the typed PIN with the stored one
beep(ms) and beepUntilStarts a beep and lets loop() stop it later, so a beep never freezes the keypad
now - stateSince >= UNLOCK_MSNon-blocking timer for the auto re-lock and the lockout

The new PIN is kept in RAM, so it returns to 1234 after a power cut. To keep it, save it with EEPROM.put() when it changes and read it back in setup().

Upload and Test

  1. Select Tools → Board → Arduino Uno and the right port, then upload.
  2. The LCD shows Keypad Lock v1, then Door LOCKED and PIN:. The red LED is on.
  3. Work through the table below and tick each result.
You doLCDLEDs and servo
Type 1 2 3PIN: ***Red on, servo at 0°
Press *PIN: clearedNo change
Type 1 2 3 4 #Access granted / A:new PIN D:lockGreen on, servo at 90°, beep
Wait 5 sDoor LOCKEDRed on, servo back to 0°
Wrong PIN + # (1st, 2nd)Wrong PIN / 2 tries leftLong beep
Wrong PIN + # (3rd)Too many tries / Wait 30 sRed blinks, keys ignored
Unlock, press A, type 4 new digits, #PIN changedNext unlock needs the new PIN

Fit the servo horn so that 0° is the locked position before you fix it to the bolt, then adjust LOCKED_ANGLE and UNLOCKED_ANGLE in a few degrees at a time.

Troubleshooting

ProblemLikely causeFix
Wrong characters appearKeypad pinout differsSwap the order in rowPins or colPins, or swap rows and columns entirely
A whole row or column never worksLoose ribbon contactPress the 8-pin connector fully onto the header pins
Same digit appears twiceVery worn membraneThe library debounces 10 ms; raise it with keypad.setDebounceTime(30)
LCD backlight on, no textWrong I2C address or contrastRun an I2C scanner, then turn the blue trimmer on the backpack
Uno resets when the servo movesSupply dipPower the servo from its own 5 V supply with a shared ground
Servo buzzes at restHorn pushing against an end stopReduce the angle range by a few degrees
New PIN lost after power offStored in RAM onlySave it to EEPROM as described above

Make It Your Own

  1. Real lock: swap the servo for a 12 V solenoid through a relay module and a separate supply.
  2. Keep the PIN: store it in EEPROM, with a factory-reset key combination.
  3. Two-factor: add an RC522 RFID reader and require card and PIN.
  4. Alarm: after a lockout, sound the buzzer continuously until a master code is entered.
  5. Log: add a DS3231 clock and print each access with a timestamp to the Serial Monitor.

FAQ

How many pins does a 4x4 keypad need on Arduino?

Eight: four rows and four columns. The keypad is a passive grid, so the Arduino finds a key by driving one column LOW at a time and checking which row follows it.

Do I need resistors on the keypad?

No. The Keypad library turns on the Uno's internal pull-up resistors on the row pins, so the keypad connects directly.

Why does my keypad print the wrong numbers?

The pinout of cheap keypads varies. Change the order of the pins in rowPins and colPins until each key matches, which is quicker than rewiring.

Can I use a 4x3 keypad?

Yes. Set COLS to 3, remove the last column from the key map, and use seven pins. You lose the A to D keys, so move the change-PIN function to another key.

Is a servo strong enough for a real door?

No. Use it for boxes, drawers and demos. A real door needs a solenoid lock or an electric strike with its own power supply and a manual override.

What You Learned

You scanned a key matrix with eight wires, organised a sketch as a state machine and kept the timing non-blocking. For a card-based version of the same idea see the Arduino RFID door lock, and for the Uno's pins and current limits see the Arduino Uno pinout guide.

Topicsarduino-unokeypad4x4-keypadservosg90lcd1602i2cdoor-lock

Questions & Answers