Overview
A photoresistor (LDR) has high resistance in the dark (often around 1 MΩ) and low resistance in bright light (a few kΩ). Paired with a fixed resistor it forms a voltage divider whose output rises with light.
You will need
- Arduino Uno R3
- Photoresistor (LDR, e.g. GL5528)
- 10 kΩ resistor
- Breadboard and jumper wires
Wiring
| Uno pin | Connects to | Notes |
|---|---|---|
| 5V | LDR, one leg | |
| A0 | LDR other leg and 10 kΩ resistor | Divider midpoint |
| GND | Other end of 10 kΩ resistor | |
| 13 | On-board L LED | Night-light output |
Diagram
The sketch
const int LIGHT_PIN = A0;
const int DARK_THRESHOLD = 300; // adjust for your room
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int light = analogRead(LIGHT_PIN);
Serial.println(light);
if (light < DARK_THRESHOLD) {
digitalWrite(LED_BUILTIN, HIGH); // dark: light on
} else {
digitalWrite(LED_BUILTIN, LOW);
}
delay(100);
}How the code works
Watch the Serial Monitor in your normal lighting and with your hand over the sensor, then set
DARK_THRESHOLD between the two values. If the LED flickers at the threshold, add hysteresis: switch
on below one value and off above a higher one.
What you should observe
- Readings go up in bright light and down in shadow.
- Covering the LDR turns on the L LED.