#include <LiquidCrystal.h>
// LCD pin connections (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
// Define pin connections
int ldrPin = A0; // Analog pin for LDR
int ledPin = 13; // Digital pin for LED
// Set a threshold value (adjust based on your lighting conditions)
int threshold = 500;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize serial communication (optional for debugging)
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2); // Set up the LCD's number of columns and rows
lcd.print("hi nasir"); // Display initial message
delay(2000); // Pause to allow reading message
lcd.clear();
}
void loop() {
// Read the analog value from the LDR
int ldrValue = analogRead(ldrPin);
// Print the LDR value to the Serial Monitor (optional for debugging)
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Display the LDR value on the LCD
lcd.setCursor(0, 0); // First row, first column
lcd.print("LDR: ");
lcd.print(ldrValue);
// If the LDR value is below the threshold (it's dark), turn on the LED
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn on LED
lcd.setCursor(0, 1); // Move cursor to second row
lcd.print("LED: ON ");
}
// If the LDR value is above the threshold (it's bright), turn off the LED
else {
digitalWrite(ledPin, LOW); // Turn off LED
lcd.setCursor(0, 1); // Move cursor to second row
lcd.print("LED: OFF ");
}
// Small delay to avoid rapid changes
delay(500);
}