4.Keypad-Controlled Lock System with Servo and I2C LCD Using Arduino

 Keypad-Controlled Lock System with Servo and I2C LCD Using Arduino

Components Needed:

  1. Arduino board
  2. 4x4 or 4x3 Keypad
  3. I2C LCD
  4. Servo motor
  5. Jumper wires
  6. Breadboard (optional)

Connections:

  1. Keypad: Connect the keypad pins to Arduino digital pins (e.g., rows to pins 9, 8, 7, 6 and columns to pins 5, 4, 3, 2).
  2. I2C LCD: Connect the SDA and SCL pins to Arduino I2C pins (A4 and A5 on an Uno).
  3. Servo: Connect the servo motor's signal pin to pin 10.

Example Code:

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

const byte ROWS = 4; // Number of rows
const byte COLS = 4; // Number of columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};    // Connect to row pins of keypad
byte colPins[COLS] = {5, 4, 3, 2};     // Connect to column pins of keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

Servo myServo; // Create a Servo object
Adafruit_LiquidCrystal lcd(0); // Create LCD object

int servoPin = 10; // Servo motor connected to pin 10
String inputPassword = "";
const String openPassword = "9870";
const String closePassword = "2413";

void setup() {
  myServo.attach(servoPin); // Attach the servo to the pin
  myServo.write(0);         // Set initial servo position to 0 degrees
  lcd.begin(16, 2);         // Initialize the LCD with 16 columns and 2 rows
  lcd.print("Enter Password:");
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    if (key == 'D') {
      // Check password when 'D' is pressed
      if (inputPassword == openPassword) {
        lcd.clear();
        lcd.print("Lock open");
        myServo.write(90); // Move servo to 90 degrees
      } else if (inputPassword == closePassword) {
        lcd.clear();
        lcd.print("Locked");
        myServo.write(0); // Move servo to 0 degrees
      } else {
        lcd.clear();
        lcd.print("Wrong Password");
      }

      delay(2000); // Display message for 2 seconds
      lcd.clear();
      lcd.print("Enter Password:");
      inputPassword = ""; // Clear input
    } else if (key == '*') {
      // Clear input when '*' is pressed
      inputPassword = "";
      lcd.clear();
      lcd.print("Enter Password:");
    } else {
      // Append the key to the inputPassword
      inputPassword += key;
      lcd.setCursor(0, 1); // Move cursor to the second row
      lcd.print(inputPassword);
    }
  }
}

Comments

Popular posts from this blog

2.Controlling LEDs with a Keypad and Arduino

4.Displaying Weight and Force from an FSR on an I2C LCD using Arduino