#include <DHT.h>
#include <Keypad.h>
// Keypad configuration
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'} // Using '#' as Enter key and '*' as Backspace key
};
// Pin assignments
byte rowPins[ROWS] = {14, 27, 26, 25}; // Adjust these pins as per your ESP32 model
byte colPins[COLS] = {33, 32, 18, 19}; // Adjust these pins as per your ESP32 model
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Security code
const char* password = "1234"; // Your defined security code
char enteredPassword[5]; // Array to store the entered password
int buzzerPin = 17; // Pin for the buzzer
int redLedPin = 5; // Pin for the red LED
#define trigPin 12 // Trig pin of the ultrasonic sensor connected to Arduino pin 12
#define echoPin 13 // Echo pin of the ultrasonic sensor connected to Arduino pin 13
#define LDR_PIN 34 // LDR connected to GPIO34 (Analog)
#define PIR_PIN 23 // PIR connected to GPIO23 (Digital)
#define LED_PIN 22 // LED 1 connected to GPIO22 (Digital)
#define TEMP_LED_PIN 21 // LED 2 connected to GPIO21 (Digital)
#define COLD_LED_PIN 15 // LED 3 connected to GPIO19 (Digital)
#define DHT_PIN 4 // DHT22 sensor connected to GPIO4
#define LED4 16 // LED 4 connected to Arduino pin 11
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Set trigPin as an output
pinMode(echoPin, INPUT); // Set echoPin as an input
pinMode(PIR_PIN, INPUT); // Set PIR pin as input
pinMode(LED_PIN, OUTPUT); // Set LED 1 pin as output
pinMode(TEMP_LED_PIN, OUTPUT); // Set LED 2 pin as output
pinMode(COLD_LED_PIN, OUTPUT); // Set LED 3 pin as output
pinMode(LED4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
digitalWrite(redLedPin, LOW); // Ensure LED is off at the start
dht.begin(); // Initialize DHT sensor
digitalWrite(LED_PIN, LOW); // Turn OFF LED 1
digitalWrite(TEMP_LED_PIN, LOW); // Turn OFF LED 2
digitalWrite(COLD_LED_PIN, LOW); // Turn OFF LED 3
digitalWrite(LED4, LOW);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Clear the trigger
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(trigPin, HIGH); // Set the trigger high
delayMicroseconds(10); // Keep it high for 10 microseconds
digitalWrite(trigPin, LOW); // Set the trigger low
// Read the echo pin
duration = pulseIn(echoPin, HIGH); // Read the incoming pulse
distance = (duration / 2) * 0.0343; // Calculate the distance in cm, speed of sound =
int ldrValue = analogRead(LDR_PIN); // Read LDR value
int pirValue = digitalRead(PIR_PIN); // Read PIR value
float temperature = dht.readTemperature(); // Read temperature from DHT22
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" | PIR Value: ");
Serial.print(pirValue);
Serial.print(" | Temperature: ");
Serial.println(temperature);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check the conditions for LED 1
if (pirValue == HIGH && ldrValue < 999) {
digitalWrite(LED_PIN, HIGH); // Turn ON LED 1
} else {
digitalWrite(LED_PIN, LOW); // Turn OFF LED 1
}
// Check the temperature condition for LED 2
if (pirValue == HIGH && temperature >= 30.0) {
digitalWrite(TEMP_LED_PIN, HIGH); // Turn ON LED 2
} else {
digitalWrite(TEMP_LED_PIN, LOW); // Turn OFF LED 2
}
// Check conditions for LED 3
if (pirValue == HIGH && temperature < 20.0) {
digitalWrite(COLD_LED_PIN, HIGH); // Turn ON LED 3
} else {
digitalWrite(COLD_LED_PIN, LOW); // Turn OFF LED 3
}
// Control LEDs based on distance
if (distance < 200) {
digitalWrite(LED4, HIGH); // Turn on the 4th LED if distance is less than 200 cm
} else {
digitalWrite(LED4, LOW); // Turn off the 4th LED if distance is 200 cm or more
}
delay(100); // Delay for stability and to prevent excessive readings
char key = keypad.getKey(); // Get the key pressed
if (key) {
Serial.println(key); // Display the pressed key for debugging
// If the enter key '#' is pressed
if (key == '#') {
enteredPassword[4] = '\0'; // Null-terminate the string
if (strcmp(enteredPassword, password) == 0) {
// If the password is correct
digitalWrite(redLedPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
Serial.println("Correct Password!"); // Successful message
delay(2000); // Optional delay before reset
} else {
// If the password is incorrect
triggerAlarm(); // Call the alarm function
Serial.println("Incorrect Password!"); // Failure message
}
// Reset the password entry
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear the entered password
return; // Exit the loop
}
// If the backspace key '*' is pressed
else if (key == '*') {
for (int i = 3; i >= 0; i--) {
if (enteredPassword[i] != 0) { // Find the last entered character
enteredPassword[i] = 0; // Clear the last character
break; // Exit the loop after removing one character
}
}
} else {
// Add the entered key to the enteredPassword array
for (int i = 0; i < 4; i++) {
if (enteredPassword[i] == 0) { // Find the first empty spot
enteredPassword[i] = key; // Add the key to the enteredPassword
break;
}
}
}
}
}
// Function to handle incorrect password entry
void triggerAlarm() {
while (true) {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
tone(buzzerPin, 1000); // Start buzzer sound (1000 Hz)
delay(300); // Duration buzzer and LED ON
digitalWrite(redLedPin, LOW); // Turn off the red LED
noTone(buzzerPin); // Stop buzzer sound
delay(300); // Duration buzzer and LED OFF
Serial.println("Incorrect Password!");
// Check for keypad input to reset alarm
char key = keypad.getKey();
if (key == '*') { // Press '*' to reset the alarm
digitalWrite(redLedPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
break; // Exit the alarm routine
}
}
}