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 Nano | LDR light sensor module | Wire | Notes |
|---|---|---|---|
| 5V | VCC | red | |
| GND | GND | black | |
| D3 | DO | green | Comparator output, threshold set by the trimmer. |
| A6 | AO | yellow | Analog-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 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);
}// 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
AO: 214 DO: light Lamp: OFF
AO: 712 DO: dark Lamp: ONAO: 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.