Introduction
A Light Dependent Resistor (LDR), also called a photoresistor, changes its resistance based on the amount of light hitting it. In bright light, resistance drops to a few hundred ohms. In darkness, it can rise to several megaohms. In this tutorial, we use this property to build an automatic night light that turns on when it gets dark.
Components Needed
- Arduino Uno
- LDR (photoresistor)
- 10k-ohm resistor
- LED and 330-ohm resistor
- Breadboard and jumper wires
Circuit: Voltage Divider
The LDR and 10k-ohm resistor form a voltage divider. As light decreases, LDR resistance increases, raising the voltage at the midpoint (analog pin A0). We read this value and use it to determine when to turn on the LED.
Project 1: Read Light Level
#define LDR_PIN A0
void setup() { Serial.begin(9600); }
void loop() {
int lightLevel = analogRead(LDR_PIN);
Serial.println("Light Level: " + String(lightLevel));
delay(500);
}
Project 2: Automatic Night Light
#define LDR_PIN A0
#define LED_PIN 9
#define THRESHOLD 500 // Adjust based on your environment
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int light = analogRead(LDR_PIN);
if (light < THRESHOLD) {
digitalWrite(LED_PIN, HIGH); // Dark - turn LED on
} else {
digitalWrite(LED_PIN, LOW); // Bright - turn LED off
}
Serial.println("LDR: " + String(light));
delay(200);
}
Project 3: Brightness-Controlled Dimming
Instead of simply ON/OFF, this project smoothly dims the LED inversely proportional to ambient light using PWM.
#define LDR_PIN A0
#define LED_PIN 9
void setup() { pinMode(LED_PIN, OUTPUT); }
void loop() {
int light = analogRead(LDR_PIN);
int brightness = map(light, 0, 1023, 255, 0);
analogWrite(LED_PIN, brightness);
delay(100);
}
Conclusion
The LDR is a simple but powerful sensor. Combine it with a relay module instead of an LED and you have a fully automatic street light controller!