Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

LDR light sensor module with Arduino Uno — wiring and code

Read an LDR light-sensor module on an Arduino Uno: light level on A0, dark/bright threshold on D2, and the on-board LED as a night light.

Arduino C++Verified — core avr 1.8.8, 3 Nov 2026Compile-only

Community draft — report a mistake

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 R3Only the pins used are shown · 5 V logicLDR light sensor module5VGNDA0D2VCCGNDDOAO
Every connection
Arduino Uno R3LDR light sensor moduleWireNotes
5VVCCred
GNDGNDblack
A0AOyellow
D2DOgreen

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-uno.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// 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

Serial Monitor · 9600Output
Light 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.

↑↓ to move · Enter to open