Arduino Keypad-Controlled Safe with LCD Display

This is an easy arduino uno project with code for beginners, it is a keypad controlled safe with cd display simulated online.

Project description:

This project implements a secure, servo-controlled safe with a 4x4 keypad for input and an LCD (16x2) display for user feedback. The safe is locked and unlocked based on a predefined 4-digit code (1234 by default). The servo motor controls the locking mechanism, while the keypad lets users enter the code to unlock the safe. Feedback messages such as "Access Denied" or "Safe Unlocked" are displayed on the LCD to enhance the user experience.

The project is simulated on Wokwi.com, a powerful platform for simulating Arduino projects. A JSON file is provided to view the complete schematic of the components and connections.


Materials Required:

  1. Arduino Uno (or compatible board)
  2. 16x2 LCD Display with 4-bit interface
  3. 4x4 Keypad
  4. Servo Motor (e.g., SG90)
  5. Resistors (for pull-up configurations, if needed)
  6. Connecting Wires
  7. Breadboard
  8. Power Supply (or USB connection to the Arduino)

Features:

  • Secure Access: The safe unlocks only when the correct 4-digit code is entered.
  • LCD Display: Provides real-time feedback for user interactions (e.g., "Enter Code," "Access Denied").
  • Auto-locking: After being unlocked for 3 seconds, the safe automatically locks.
  • Customizable Code: The default code can be changed in the Arduino sketch.
  • Simulation Ready: Designed for Wokwi, with a JSON file for schematic representation.

Additional Notes:

  • JSON File: Use the provided JSON file in Wokwi to visualize the project connections and simulate the safe’s functionality.
  • Expansion: This setup can be expanded by adding features like an EEPROM to save the code or integrating a buzzer for failed attempts.


Schematic:

schematic of the circuit



 Code:

--------------------------------------------------------------------------------------------------------------------------------
// Pin definitions for LCD (16x2)
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;

// Servo motor control
const int servoPin = 6;
int lockPosition = 20;    // Locked position (in degrees)
int unlockPosition = 90;  // Unlocked position (in degrees)

// Pin definitions for Keypad
const int rowPins[4] = {5, 4, 3, 2};
const int colPins[4] = {A3, A2, A1, A0};
char keys[4][4] = {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}
};

// Variables for safe state
char code[5] = "1234"; // Default code
char inputBuffer[5] = "";
int inputIndex = 0;

// Servo control
void setServoAngle(int angle) {
  int pulseWidth = map(angle, 0, 180, 544, 2400);
  for (int i = 0; i < 50; i++) { // Generate PWM signal for 1 second
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseWidth);
    digitalWrite(servoPin, LOW);
    delay(20); // Wait 20ms between pulses
  }
}

// Initialize LCD
void lcdCommand(byte cmd) {
  digitalWrite(rs, LOW);
  sendToLCD(cmd);
}

void lcdData(byte data) {
  digitalWrite(rs, HIGH);
  sendToLCD(data);
}

void sendToLCD(byte data) {
  // Send high nibble
  digitalWrite(d4, data & 0x10);
  digitalWrite(d5, data & 0x20);
  digitalWrite(d6, data & 0x40);
  digitalWrite(d7, data & 0x80);
  lcdPulseEnable();
  // Send low nibble
  digitalWrite(d4, data & 0x01);
  digitalWrite(d5, data & 0x02);
  digitalWrite(d6, data & 0x04);
  digitalWrite(d7, data & 0x08);
  lcdPulseEnable();
}

void lcdPulseEnable() {
  digitalWrite(en, HIGH);
  delayMicroseconds(1);
  digitalWrite(en, LOW);
  delayMicroseconds(100);
}

void lcdPrint(const char *message) {
  while (*message) {
    lcdData(*message++);
  }
}

void lcdSetCursor(byte col, byte row) {
  byte rowOffsets[] = {0x00, 0x40, 0x14, 0x54};
  lcdCommand(0x80 | (col + rowOffsets[row]));
}

void lcdClearWithDelay(unsigned long delayMillis) {
  delay(delayMillis);
  lcdCommand(0x01); // Clear display
}

// Initialize Keypad
char getKeypadKey() {
  for (int r = 0; r < 4; r++) {
    pinMode(rowPins[r], OUTPUT);
    digitalWrite(rowPins[r], LOW);

    for (int c = 0; c < 4; c++) {
      pinMode(colPins[c], INPUT_PULLUP);
      if (digitalRead(colPins[c]) == LOW) {
        while (digitalRead(colPins[c]) == LOW); // Wait for release
        return keys[r][c];
      }
    }
    digitalWrite(rowPins[r], HIGH);
    pinMode(rowPins[r], INPUT);
  }
  return 0;
}

// Safe control logic
void lockSafe() {
  setServoAngle(lockPosition);
  lcdSetCursor(0, 1);
  lcdPrint("Safe Locked   ");
  lcdClearWithDelay(1000); // Clear after 1 second
}

void unlockSafe() {
  setServoAngle(unlockPosition);
  lcdSetCursor(0, 1);
  lcdPrint("Safe Unlocked ");
  delay(3000); // Keep safe unlocked for 3 seconds
  lockSafe();  // Automatically lock after 3 seconds
}

void resetInputBuffer() {
  inputIndex = 0;
  memset(inputBuffer, 0, sizeof(inputBuffer));
}

void checkCode() {
  if (strcmp(inputBuffer, code) == 0) {
    unlockSafe();
  } else {
    lcdSetCursor(0, 1);
    lcdPrint("Access Denied ");
    lcdClearWithDelay(1000); // Clear after 1 second
    lcdSetCursor(0, 1);
    lcdPrint("Enter Code    ");
    lcdClearWithDelay(1000); // Clear after 1 second
  }
  resetInputBuffer();
}

void setup() {
  // Initialize LCD pins
  pinMode(rs, OUTPUT);
  pinMode(en, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(d5, OUTPUT);
  pinMode(d6, OUTPUT);
  pinMode(d7, OUTPUT);

  lcdCommand(0x33); // Initialize LCD in 4-bit mode
  lcdCommand(0x32); // Set to 4-bit mode
  lcdCommand(0x28); // 2 lines, 5x7 matrix
  lcdCommand(0x0C); // Display ON, cursor OFF
  lcdCommand(0x06); // Increment cursor
  lcdCommand(0x01); // Clear display

  // Display startup message
  lcdSetCursor(0, 0);
  lcdPrint("Arduino Safe");
  delay(2000);
  lcdClearWithDelay(0); // Clear immediately after delay

  // Initialize Servo
  pinMode(servoPin, OUTPUT);
  lockSafe();
  resetInputBuffer();

  // Display prompt
  lcdSetCursor(0, 1);
  lcdPrint("Enter Code    ");
  lcdClearWithDelay(1000); // Clear after 1 second
}

void loop() {
  char key = getKeypadKey();
  if (key) {
    lcdSetCursor(inputIndex, 1);
    lcdData('*'); // Display * for each key pressed
    inputBuffer[inputIndex++] = key;

    if (inputIndex >= 4) {
      delay(500);
      checkCode();
    }
  }
}


--------------------------------------------------------------------------------------------------------------------------------
Json code(Just paste it in wokwi):
{
  "version": 1,
  "author": "https://www.youtube.com/@EasyElectronics2",
  "editor": "wokwi",
  "parts": [
    { "id": "uno", "type": "wokwi-arduino-uno", "top": 200, "left": 20 },
    {
      "id": "keypad",
      "type": "wokwi-membrane-keypad",
      "left": 360,
      "top": 140
    },
    {
      "id": "servo",
      "type": "wokwi-servo",
      "left": 400,
      "top": 20,
      "attrs": { "hornColor": "black" }
    },
    { "id": "lcd", "type": "wokwi-lcd1602", "top": 8, "left": 20 },
    {
      "id": "r1",
      "type": "wokwi-resistor",
      "top": 140,
      "left": 220,
      "attrs": { "value": "220" }
    }
  ],
  "connections": [
    ["uno:GND.1", "lcd:VSS", "black", ["v-51", "*", "h0", "v18"]],
    ["uno:GND.1", "lcd:K", "black", ["v-51", "*", "h0", "v18"]],
    ["uno:GND.1", "lcd:RW", "black", ["v-51", "*", "h0", "v18"]],
    ["uno:5V", "lcd:VDD", "red", ["v16", "h-16"]],
    ["uno:5V", "r1:2", "red", ["v16", "h-118", "v-244", "h50"]],
    ["r1:1", "lcd:A", "pink", []],
    ["uno:12", "lcd:RS", "blue", ["v-16", "*", "h0", "v20"]],
    ["uno:11", "lcd:E", "purple", ["v-20", "*", "h0", "v20"]],
    ["uno:10", "lcd:D4", "green", ["v-24", "*", "h0", "v20"]],
    ["uno:9", "lcd:D5", "brown", ["v-28", "*", "h0", "v20"]],
    ["uno:8", "lcd:D6", "gold", ["v-32", "*", "h0", "v20"]],
    ["uno:7", "lcd:D7", "gray", ["v-36", "*", "h0", "v20"]],
    ["uno:6", "servo:PWM", "orange", ["v-40", "*", "h0", "h-52"]],
    ["uno:5V", "servo:V+", "red", ["v16", "h-118", "v-244", "*", "h-56"]],
    ["uno:GND.1", "servo:GND", "black", ["v-46", "*", "v88", "h-60"]],
    ["uno:A3", "keypad:C1", "brown", ["v116", "*", "h0", "v0"]],
    ["uno:A2", "keypad:C2", "gray", ["v120", "*", "h0", "v0"]],
    ["uno:A1", "keypad:C3", "orange", ["v124", "*", "h0", "v0"]],
    ["uno:A0", "keypad:C4", "pink", ["v128", "*", "h0", "v0"]],
    ["uno:5", "keypad:R1", "blue", ["v-34", "h96", "*", "v12"]],
    ["uno:4", "keypad:R2", "green", ["v-30", "h80", "*", "v16"]],
    ["uno:3", "keypad:R3", "purple", ["v-26", "h64", "*", "v20"]],
    ["uno:2", "keypad:R4", "gold", ["v-22", "h48", "*", "v24"]]
  ]
}

----------------------------------------------------------------------------------------------------------------------

  • Arduino safe project
  • Arduino keypad safe
  • Arduino servo lock system
  • Secure Arduino project
  • LCD display with Arduino
  • Arduino keypad tutorial
  • Keypad-controlled lock
  • Arduino safe simulation
  • Wokwi Arduino safe
  • Arduino safe JSON schematic
  • arduino project with code
  • arduino online simulation
  • arduino easy project
  • arduino for beginners
  • arduino uno project with code





  • Comments