This circuit is designed to create a keypad-based access control system using an Arduino UNO microcontroller. The system allows users to input a passcode using a 4x4 keypad. If the correct passcode is entered, a green LED lights up indicating access granted, and a buzzer sounds a single long beep. If an incorrect passcode is entered, a red LED lights up indicating access denied, and the buzzer emits three short beeps. The circuit includes resistors to limit current to the LEDs and a buzzer for audible feedback.
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[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 customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Define the correct passcode
String correctPasscode = "1234";
String enteredPasscode = "";
// Define LED and buzzer pins
const int greenLEDPin = 13; // Green LED for Access Granted
const int redLEDPin = 12; // Red LED for Access Denied
const int buzzerPin = 11; // Buzzer pin for sound feedback
void setup(){
Serial.begin(9600);
// Set LED and buzzer pins as output
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Turn off both LEDs and the buzzer at the start
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey) {
// Print the entered key
Serial.print(customKey);
// Add key to entered passcode (except if it's '*' or '#')
if (customKey != '*' && customKey != '#') {
enteredPasscode += customKey;
}
// If '#' is pressed, it means user is done entering the passcode (Enter button)
if (customKey == '#') {
Serial.print("\nPasscode Entered: ");
Serial.println(enteredPasscode);
// Check if the entered passcode matches the correct one
if (enteredPasscode == correctPasscode) {
Serial.println("Access Granted!");
digitalWrite(greenLEDPin, HIGH); // Turn on Green LED (Access granted)
digitalWrite(redLEDPin, LOW); // Ensure Red LED is off
// Buzzer for correct passcode: Single long beep
tone(buzzerPin, 1000); // 1000 Hz frequency
delay(500); // 500 ms beep
noTone(buzzerPin); // Stop the buzzer
} else {
Serial.println("Access Denied!");
digitalWrite(redLEDPin, HIGH); // Turn on Red LED (Access denied)
digitalWrite(greenLEDPin, LOW); // Ensure Green LED is off
// Buzzer for incorrect passcode: 3 short beeps
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 1000); // 1000 Hz frequency
delay(200); // 200 ms beep
noTone(buzzerPin); // Stop the buzzer
delay(100); // Small pause between beeps
}
}
enteredPasscode = ""; // Clear the entered passcode after checking
}
// If '*' is pressed, it clears the current entry (Reset button)
if (customKey == '*') {
enteredPasscode = "";
Serial.println("\nPasscode Cleared!");
// Turn off both LEDs and the buzzer when passcode is cleared
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
noTone(buzzerPin);
}
}
}
This code is designed to run on an Arduino UNO and interfaces with a 4x4 keypad, a red LED, a green LED, and a buzzer. It sets up the keypad and defines the pins for the LEDs and buzzer. The main loop waits for a keypress, builds the entered passcode, and checks it against a predefined correct passcode. Depending on whether the passcode is correct or not, it provides visual and audible feedback.