Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Hotel Safe System with 4x4 Keypad and I2C LCD

View and Edit Project In Cirkit DesignerImage of Arduino-Based Hotel Safe System with 4x4 Keypad and I2C LCD

Circuit Documentation

Summary

This circuit is designed for a hotel safe system that utilizes a 4x4 keypad for user input, a 16x2 I2C LCD for display, and a servo motor to control the locking mechanism. The system allows users to set a 4-digit code to lock the safe and requires the same code to unlock it. The LCD provides user feedback, and the servo transitions gradually between open and closed positions.


Component List

4x4 Keypad

16x2 I2C LCD

Servo

Arduino UNO


Wiring Details

4x4 Keypad


16x2 I2C LCD


Servo


Documented Code

/*
 * Arduino Sketch for a hotel safe system using a 4x4 Keypad, 16x2 I2C LCD, 
 * and a Servo motor. The system allows the user to set a 4-digit code to 
 * lock the safe and requires the same code to unlock it. The servo motor 
 * controls the locking mechanism, and the LCD provides user feedback.
 * The servo transitions gradually between open and closed positions.
 */

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

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4};   // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Servo setup
Servo lockServo;
const int servoPin = 3;

// Variables
String enteredCode = "";
String storedCode = "";
bool isLocked = false;

void setup() {
  // Initialize LCD
  lcd.begin(16,2);
  lcd.backlight();
  
  // Initialize Servo
  lockServo.attach(servoPin);
  lockServo.write(0); // Initial position (unlocked)
  
  // Display initial message
  lcd.setCursor(0, 0);
  lcd.print("Code to Lock:");
}

void loop() {
  char key = keypad.getKey();
  
  if (key) {
    if (key == '#') {
      if (!isLocked) {
        if (enteredCode.length() == 4) {
          storedCode = enteredCode;
          lockSafe();
        } else {
          displayError("Enter 4 digits", 0);
        }
      } else {
        if (enteredCode == storedCode) {
          unlockSafe();
        } else {
          displayError("Wrong Code", 1);
        }
      }
      enteredCode = "";
    } else if (key >= '0' && key <= '9') {
      if (enteredCode.length() < 4) {
        enteredCode += key;
        lcd.setCursor(enteredCode.length() - 1, 1);
        lcd.print('*');
      }
    }
  }
}

void lockSafe() {
  isLocked = true;
  lcd.setCursor(0, 0);
  lcd.print("Locking Safe...");

  for (int pos = 0; pos <= 180; pos += 1) { // Gradually lock
    lockServo.write(pos);
    delay(15);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Safe Locked");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Code to Unlock");
}

void unlockSafe() {
  isLocked = false;
  lcd.setCursor(0, 0);
  lcd.print("Unlocking Safe...");

  for (int pos = 180; pos >= 0; pos -= 1) { // Gradually unlock
    lockServo.write(pos);
    delay(15);
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Safe Unlocked");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Code to Lock");
}

void displayError(String message, int code) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(message);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  if (code == 0)
    lcd.print("Code to Lock");
  else
    lcd.print("Code to Unlock");
}

This documentation provides a comprehensive overview of the circuit, detailing each component, its wiring, and the code that drives the system.