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

Arduino UNO-Based Password-Protected Servo Lock with 4x4 Keypad

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Password-Protected Servo Lock with 4x4 Keypad

Circuit Documentation

Summary

This circuit is designed to control a servo motor using an Arduino UNO and a 4x4 membrane matrix keypad. The keypad is used to input a password, and upon entering the correct password, the servo motor will move to an unlocked position. The circuit also includes LEDs to indicate the locked and unlocked states.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. 4x4 Membrane Matrix Keypad

    • Description: A keypad with 4 rows and 4 columns, used for inputting data.
    • Pins: R1, R2, R3, R4, C1, C2, C3, C4
  3. Servo

    • Description: A servo motor used for precise control of angular position.
    • Pins: GND, VCC, PWM

Wiring Details

Arduino UNO

4x4 Membrane Matrix Keypad

Servo

Code Documentation

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

Servo ServoMotor;
char* password = "0221"; // You can change the Password

int position = 0;
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] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Create two variables for LED Lights
int RedpinLock = 12;
int GreenpinUnlock = 10;

unsigned long lastUnlockedTime = 0; // To store the time when unlocked
bool isUnlocked = false; // To track the current state

void setup()
{
  pinMode(RedpinLock, OUTPUT);
  pinMode(GreenpinUnlock, OUTPUT);
  ServoMotor.attach(11);
  LockedPosition(true);
}

void loop()
{
  char key = keypad.getKey();
  
  // Reset position if '*' or '#' is pressed
  if (key == '*' || key == '#')
  {
    position = 0;
    LockedPosition(true);
  }

  // Check if key matches the password
  if (key == password[position])
  {
    position++;
  }

  // Unlock if password is correct
  if (position == 4)
  {
    LockedPosition(false);
    lastUnlockedTime = millis(); // Store the unlock time
    isUnlocked = true; // Mark as unlocked
    position = 0; // Reset position
  }

  // Auto-lock after 5000ms
  if (isUnlocked && (millis() - lastUnlockedTime >= 20000))
  {
    LockedPosition(true);
    isUnlocked = false; // Mark as locked
  }
  
  delay(100);
}

void LockedPosition(int locked)
{
  if (locked)
  {
    digitalWrite(RedpinLock, HIGH);
    digitalWrite(GreenpinUnlock, LOW);
    ServoMotor.write(11); // Locked position
  }
  else
  {
    digitalWrite(RedpinLock, LOW);
    digitalWrite(GreenpinUnlock, HIGH);
    ServoMotor.write(90); // Unlocked position
  }
}

This code initializes the servo motor and keypad, sets up the pin modes for the LEDs, and defines the locked and unlocked positions for the servo motor. The main loop checks for key presses on the keypad, compares them to the password, and unlocks the servo motor if the correct password is entered. The servo motor will automatically lock again after 20 seconds.