An LDR module is a light-dependent resistor with a comparator: it gives you an analog light level and a ready-made dark/bright switch.
On the Uno it runs at 5 V. The sketch prints the raw level and turns the on-board LED on when it gets dark — the start of an automatic night light or street light.
Wiring
| Arduino Uno R3 | LDR light sensor module | Wire | Notes |
|---|---|---|---|
| 5V | VCC | red | |
| GND | GND | black | |
| A0 | AO | yellow | |
| D2 | DO | green |
3.3–5 V part, 5 V board. AO swings up to VCC. On ESP32 and Pico, power the module from 3.3 V so AO never exceeds the ADC's 3.3 V limit; on Uno use 5 V.
Board quirks
- AO is a relative level (0–1023), not lux — use a BH1750 when you need real units.
- Turn the blue trimmer until DO switches at the light level you want; the module’s second LED shows its state.
- On most modules AO rises and DO goes HIGH as it gets darker — if yours is the other way round, invert the test in code.
Code
// LDR (light-dependent resistor) module on an Arduino Uno R3.
// VCC → 5V, GND → GND, AO → A0 (light level), DO → D2 (dark/bright, threshold on the trimmer).
const uint8_t AO_PIN = A0;
const uint8_t DO_PIN = 2;
void setup() {
Serial.begin(9600);
pinMode(DO_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int raw = analogRead(AO_PIN); // most modules: higher = darker
bool dark = digitalRead(DO_PIN) == HIGH; // DO goes HIGH when it is darker than the threshold
digitalWrite(LED_BUILTIN, dark ? HIGH : LOW);
Serial.print(F("Light level (raw): "));
Serial.print(raw);
Serial.println(dark ? F(" -> DARK") : F(" -> bright"));
delay(500);
}// LDR (light-dependent resistor) module on an Arduino Uno R3.
// VCC → 5V, GND → GND, AO → A0 (light level), DO → D2 (dark/bright, threshold on the trimmer).
const uint8_t AO_PIN = A0;
const uint8_t DO_PIN = 2;
void setup() {
Serial.begin(9600);
pinMode(DO_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int raw = analogRead(AO_PIN); // most modules: higher = darker
bool dark = digitalRead(DO_PIN) == HIGH; // DO goes HIGH when it is darker than the threshold
digitalWrite(LED_BUILTIN, dark ? HIGH : LOW);
Serial.print(F("Light level (raw): "));
Serial.print(raw);
Serial.println(dark ? F(" -> DARK") : F(" -> bright"));
delay(500);
}Expected serial output
Light level (raw): 214 -> bright
Light level (raw): 802 -> DARKLight level (raw): 214 -> bright Light level (raw): 802 -> DARK
Common errors
DO never changes
The trimmer is at one end. Turn it slowly while covering the sensor until the module LED switches.
Readings are inverted
Module variants differ. Swap the comparison in the sketch.