This circuit is designed to control a 12V solenoid lock using an Arduino UNO, a 4x4 membrane matrix keypad, an LCD I2C display, a 1-channel relay module, a red pushbutton, and a 12V power supply. The user can unlock the door by entering a correct password on the keypad or by pressing the exit button. The status and instructions are displayed on the LCD.
Arduino UNO
LCD I2C Display
4X4 Membrane Matrix Keypad
1 Channel Relay 5V
12V Solenoid Lock
Red Pushbutton
12V Power Supply
Resistor
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Define the pins
const int relayPin = 13; // Pin connected to the relay module
const int exitButtonPin = 12; // Pin connected to the exit button
// LCD setup: I2C address, columns, rows
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 is the typical I2C address, 16 columns, 2 rows
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four 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 the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
// Initialize the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Password setup
String password = "1234"; // Correct password
String input = ""; // Variable to store user input
// Exit button debounce variables
bool exitButtonState = LOW; // Current state of the exit button
bool lastExitButtonState = LOW; // Previous state of the exit button
unsigned long exitButtonLastTime = 0; // Last time the button was toggled
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize the relay and exit button pins
pinMode(relayPin, OUTPUT);
pinMode(exitButtonPin, INPUT_PULLUP); // Configure the exit button with an internal pull-up resistor
digitalWrite(relayPin, LOW); // Ensure relay is OFF initially
// Initialize the LCD
lcd.begin(16, 2); // 16 columns, 2 rows
lcd.backlight(); // Turn on the backlight
lcd.print("Enter Password:"); // Display initial message
// Begin Serial Monitor (optional for debugging)
Serial.begin(9600);
}
void loop() {
handleExitButton(); // Check and handle the exit button
// Check keypad input
char key = keypad.getKey(); // Get keypress
if (key) {
if (key == '*') { // Clear input if '*' is pressed
input = ""; // Clear input
lcd.clear();
lcd.print("Input Cleared!");
delay(1000);
lcd.clear();