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 with Arduino Nano — automatic night lamp

Connect an LDR light-sensor module to an Arduino Nano with AO on A6 and DO on D3, and switch a lamp at dusk with hysteresis so it never flickers.

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

Community draft — report a mistake

An LDR module is the classic way to switch a light at dusk: a garden lamp, a porch light or a night lamp for the stairs. It gives both an analog level (AO) and a ready-made on/off output (DO).

On the Nano, AO goes on the analog-only A6 and DO on D3. The sketch uses AO with two thresholds so the on-board LED (standing in for the lamp) switches cleanly. To drive a real lamp, add a relay module and keep any 230 V wiring for an electrician.

Wiring

Arduino NanoOnly the pins used are shown · 5 V logicLDR light sensor moduleA65VGNDD3VCCGNDDOAO
Every connection
Arduino NanoLDR light sensor moduleWireNotes
5VVCCred
GNDGNDblack
D3DOgreenComparator output, threshold set by the trimmer.
A6AOyellowAnalog-only pin — ideal for AO.

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

  • On most modules the reading goes up as it gets darker. If yours goes down, swap the comparison in the sketch.
  • Two thresholds (700 on, 600 off) stop the lamp flickering at dusk. A single threshold makes it chatter on cloudy evenings.
  • Keep the LDR away from the lamp it controls, or the lamp’s own light switches it off again.
  • DO is fine for a simple on/off; AO lets you choose the switching level in code without opening the box to turn a trimmer.

Code

ldr-nano.inoArduino C++Open in PlaygroundOpen in PlaygroundFlash
// LDR light-sensor module on an Arduino Nano as an automatic night lamp (on-board LED).
// VCC → 5V, GND → GND, AO → A6 (analog-only pin), DO → D3.
// Uses hysteresis so the lamp doesn't flicker at dusk.
const uint8_t PIN_AO = A6;
const uint8_t PIN_DO = 3;
const int DARK_ON = 700;   // higher = darker on most modules (LDR to GND side)
const int DARK_OFF = 600;

bool lampOn = false;

void setup() {
  Serial.begin(115200);
  pinMode(PIN_DO, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  int level = analogRead(PIN_AO);
  if (!lampOn && level > DARK_ON) lampOn = true;
  if (lampOn && level < DARK_OFF) lampOn = false;
  digitalWrite(LED_BUILTIN, lampOn ? HIGH : LOW);
  Serial.print(F("AO: "));
  Serial.print(level);
  Serial.print(F("  DO: "));
  Serial.print(digitalRead(PIN_DO) ? F("dark") : F("light"));
  Serial.print(F("  Lamp: "));
  Serial.println(lampOn ? F("ON") : F("OFF"));
  delay(500);
}

Expected serial output

Serial Monitor · 115200Output
AO: 214  DO: light  Lamp: OFF
AO: 712  DO: dark  Lamp: ON

Common errors

AO stuck at 0 or 1023

AO on a pin that is not analog, or VCC/GND reversed on the module.

Lamp switches on and off at dusk

Thresholds too close together, or the lamp lighting the sensor. Widen the gap and shield the LDR.

↑↓ to move · Enter to open