Built-in project · Arduino Uno R3

I2C keypad door lock with an LCD on an Arduino Uno

A 4x4 keypad on a PCF8574 and an I2C LCD share A4 and A5: the right code and # open a servo bolt, and three wrong codes lock the keypad for 30 s. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $40. On Mokxi they're free.

I2C keypad door lock with an LCD · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The note on the canvas

Word for word, as it sits beside the circuit in the editor.

Try it
1. Press Run. The LCD says Enter code:
2. Press 1, 2, 3, 4 on the keypad, then #. The servo pulls the bolt back for five seconds, then locks again.
3. * clears what you typed.
4. Enter a wrong code three times: the keypad is locked for 30 seconds, with a countdown.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// I2C keypad door lock: a 4x4 keypad, a 16x2 LCD and a servo bolt, and the
// keypad and the screen need only two wires between them.
//
// A 4x4 keypad usually takes eight pins. Here a PCF8574 I2C expander (at
// 0x20) scans it, and the LCD's I2C backpack (at 0x27) sits on the same two
// wires, so the whole lock uses A4, A5 and pin 9.
//
//   PCF8574   SDA -> A4  SCL -> A5   P0..P3 -> keypad R1..R4
//                                    P4..P7 -> keypad C1..C4
//   LCD I2C   SDA -> A4  SCL -> A5   (address 0x27)
//   servo     PWM -> 9   VCC -> 5V   GND -> GND
//
// Type the code and press # to open: the servo pulls the bolt back for five
// seconds, then locks again. * clears what you typed. Three wrong codes in
// a row lock the keypad for 30 seconds, with a countdown on the screen.
//
// Things to try. Change CODE to your own, up to eight digits. Change
// OPEN_MS to keep the door open longer.

#include <Wire.h>
#include <I2CKeyPad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

const char CODE[] = "1234";
const unsigned long OPEN_MS = 5000;
const unsigned long LOCKOUT_MS = 30000;
const uint8_t MAX_TRIES = 3;
const uint8_t LOCKED_ANGLE = 0;
const uint8_t OPEN_ANGLE = 90;
const uint8_t SERVO_PIN = 9;

I2CKeyPad keyPad(0x20);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo bolt;

// The keypad's layout, then N for no key and F for a failed read.
char keymap[19] = "123A456B789C*0#DNF";

char entered[9];
uint8_t length = 0;
uint8_t wrong = 0;
bool keyWasDown = false;
enum State { LOCKED, OPEN, LOCKOUT } state = LOCKED;
unsigned long since = 0;
unsigned long lastSecond = 0;

void prompt() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("Enter code:"));
  lcd.setCursor(0, 1);
  for (uint8_t i = 0; i < length; i++) lcd.print('*');
}

void lockDoor() {
  bolt.write(LOCKED_ANGLE);
  state = LOCKED;
  length = 0;
  prompt();
  Serial.println(F("locked"));
}

void check() {
  entered[length] = '\0';
  if (strcmp(entered, CODE) == 0) {
    wrong = 0;
    bolt.write(OPEN_ANGLE);
    state = OPEN;
    since = millis();
    lcd.clear();
    lcd.print(F("Access granted"));
    lcd.setCursor(0, 1);
    lcd.print(F("Door open"));
    Serial.println(F("open"));
    return;
  }
  wrong++;
  length = 0;
  Serial.print(F("wrong code "));
  Serial.println(wrong);
  if (wrong >= MAX_TRIES) {
    wrong = 0;
    state = LOCKOUT;
    since = millis();
    lastSecond = 0;
    lcd.clear();
    lcd.print(F("Too many tries"));
    Serial.println(F("keypad locked out"));
    return;
  }
  lcd.clear();
  lcd.print(F("Wrong code"));
  lcd.setCursor(0, 1);
  lcd.print(MAX_TRIES - wrong);
  lcd.print(F(" tries left"));
  delay(1500);
  prompt();
}

void onKey(char key) {
  Serial.print(F("key "));
  Serial.println(key);
  if (key == '*') {
    length = 0;
    prompt();
  } else if (key == '#') {
    if (length > 0) check();
  } else if (key >= '0' && key <= '9' && length < sizeof(entered) - 1) {
    entered[length++] = key;
    lcd.setCursor(length - 1, 1);
    lcd.print('*');
  }
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  lcd.init();
  lcd.backlight();
  bolt.attach(SERVO_PIN);
  if (!keyPad.begin()) {
    lcd.print(F("No keypad found"));
    Serial.println(F("No PCF8574 at 0x20: check SDA, SCL and its power"));
    while (true);
  }
  keyPad.loadKeyMap(keymap);
  lockDoor();
  Serial.println(F("keypad lock ready"));
}

void loop() {
  // One press is one key: act when a key goes down, then wait for it to come up.
  bool down = keyPad.isPressed();
  if (down && !keyWasDown) {
    char key = keyPad.getChar();
    if (key != 'N' && key != 'F' && state == LOCKED) onKey(key);
  }
  keyWasDown = down;

  if (state == OPEN && millis() - since >= OPEN_MS) {
    lockDoor();
  }
  if (state == LOCKOUT) {
    unsigned long left = (LOCKOUT_MS - (millis() - since) + 999) / 1000;
    if (millis() - since >= LOCKOUT_MS) {
      Serial.println(F("keypad unlocked"));
      lockDoor();
    } else if (left != lastSecond) {
      lastSecond = left;
      lcd.setCursor(0, 1);
      lcd.print(F("Wait "));
      lcd.print(left);
      lcd.print(F(" s  "));
    }
  }
  delay(20);
}

Parts list

6 parts, plus the jumper wires. They retail for about $40; on Mokxi they're free.

How it is wired

15 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin SCL; Character LCD with I2C backpack pin SCL
  • Arduino Uno R3 pin SDA; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin 9; Hobby servo pin PWM
  • Arduino Uno R3 pin 5V; I/O expander, PCF8574 pin VCC; Character LCD with I2C backpack pin VCC; Hobby servo pin VCC
  • Ground: Arduino Uno R3 pin GND; I/O expander, PCF8574 pin GND; Character LCD with I2C backpack pin GND; Hobby servo pin GND
  • Arduino Uno R3 pin A4; I/O expander, PCF8574 pin SDA
  • Arduino Uno R3 pin A5; I/O expander, PCF8574 pin SCL
  • I/O expander, PCF8574 pin P0; Keypad, 4x4 pin R1
  • I/O expander, PCF8574 pin P1; Keypad, 4x4 pin R2
  • I/O expander, PCF8574 pin P2; Keypad, 4x4 pin R3
  • I/O expander, PCF8574 pin P3; Keypad, 4x4 pin R4
  • I/O expander, PCF8574 pin P4; Keypad, 4x4 pin C1
  • I/O expander, PCF8574 pin P5; Keypad, 4x4 pin C2
  • I/O expander, PCF8574 pin P6; Keypad, 4x4 pin C3
  • I/O expander, PCF8574 pin P7; Keypad, 4x4 pin C4

Questions and answers

Why put the keypad on a PCF8574?

A 4x4 keypad wired directly needs eight pins. Through a PCF8574 it needs only SDA and SCL, which the LCD's I2C backpack already uses, so the whole lock takes A4, A5 and pin 9.

Which PCF8574 pins go to the keypad?

Rows R1 to R4 on P0 to P3 and columns C1 to C4 on P4 to P7, the order I2CKeyPad scans. Swapped rows and columns give the wrong keys.

What if my expander or LCD has another address?

A PCF8574 is 0x20 to 0x27, set by A0 to A2, and a PCF8574A is 0x38 to 0x3F. LCD backpacks are usually 0x27 or 0x3F. Change the numbers in I2CKeyPad keyPad(0x20) and LiquidCrystal_I2C lcd(0x27, 16, 2).

How do I change the code or the lockout?

Change CODE (up to eight digits), MAX_TRIES and LOCKOUT_MS at the top of the sketch. The bolt stays open for OPEN_MS, 5 seconds.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.