Project 2: Automated Night Light Using LDR

Automated Night Light Using LDR

This project demonstrates how to use an LDR (Light Dependent Resistor) to detect light levels and control an LED. When the light level falls below a certain threshold, the LED turns on, and when it rises above the threshold, the LED turns off.

Components Needed

  1. ESP32 (Microcontroller)
  2. LDR (Light Dependent Resistor)
  3. LED
  4. Breadboard and Jumper Wires

Step 1: Circuit Connections

  1. LDR Connections:
  • Connect the VCC pin of the LDR to 3V3 on the ESP32.
  • Connect the GND pin of the LDR to GND on the ESP32.
  • Connect the A0 pin of the LDR to GPIO 13 on the ESP32.

2. LED Connections:

  • Connect the anode (longer leg) of the LED to GPIO 16 on the ESP32.
  • Connect the cathode (shorter leg) of the LED directly to GND.
LED Connection
Complete Circuit

Step 2: Writing the code

Here’s the code to read light levels from the LDR and control the LED:

 

const int LDRPin = 13;    // LDR connected to GPIO 13
const int LEDPin = 16; // LED connected to GPIO 16
const int threshold = 500; // Light level threshold (adjust based on Serial Monitor values)

void setup() {
pinMode(LDRPin, INPUT); // Set LDR pin as input
pinMode(LEDPin, OUTPUT); // Set LED pin as output
Serial.begin(115200); // Initialize Serial communication at 115200 baud rate
}

void loop() {
int LDRValue = analogRead(LDRPin); // Read the value from the LDR
Serial.println(LDRValue); // Print the LDR value to the Serial Monitor

if (LDRValue > threshold) {
// If the light level is below the threshold, turn on the LED
digitalWrite(LEDPin, HIGH);
} else {
// If the light level is above the threshold, turn off the LED
digitalWrite(LEDPin, LOW);
}

delay(100); // Small delay for stability
}


How It Works

  1. LDR Reading:
  • The LDR changes its resistance based on the light level. The ESP32 reads this value using the analogRead() function.
  • The value ranges from 0 (dark) to 4095 (bright) on the ESP32’s analog pin.

2. Threshold Adjustment:

  • Open the Serial Monitor (set to 115200 baud rate) to observe the LDR values.
  • Adjust the threshold variable in the code based on the values you see. For example, if the LDR reads 800 in darkness, set the threshold slightly lower (e.g., 500).

3. LED Control:

  • If the LDR value is greater than the threshold, the LED turns on (indicating low light).
  • If the LDR value is less than the threshold, the LED turns off (indicating sufficient light).

Serial Communication and Baud Rate

  • Serial Communication is used to send data between the ESP32 and your computer.
  • Baud Rate is the speed of communication. Here, we use 115200, which is standard for ESP32.
  • To view the LDR values, open the Serial Monitor in the Arduino IDE and ensure the baud rate matches the code (115200).

Troubleshooting

  1. LED Not Turning On/Off:
  • Check the LDR values in the Serial Monitor and adjust the threshold accordingly.
  • Ensure the LDR and LED are connected to the correct pins.
  1. Serial Monitor Not Showing Values:
  • Verify the baud rate is set to 115200.
  • Ensure the ESP32 is properly connected to your computer.

Future Enhancements

  1. Add a buzzer to create an audible alert for low light conditions.
  2. Use Wi-Fi on the ESP32 to send light level data to a cloud platform.
  3. Create a dashboard to visualize light levels in real-time.
Back to blog

Leave a comment