Home/ Projects/ Light-triggered night light
Project 3 of 16 Automation Starter

Light-triggered night light

The photoresistor forms a voltage divider with a fixed resistor, and the board reads the midpoint on its analog pin. Below a threshold you pick, the relay closes and a lamp comes on. Note the relay module is low-level trigger, so the output pin must go LOW to switch it — a detail that catches people out. Add a delay before switching so a passing shadow does not flick the lamp on and off.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 114.05
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 114.05

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

03 Light-triggered night light Arduino C++
/*
  03 — Light-triggered night light
  Board: NodeMCU V3 ESP8266 development board.  Parts: photoresistor, relay module, 10 kΩ resistor.

  Wiring:
    Photoresistor: one leg -> 3V3, other leg -> A0 AND -> 10 kΩ -> GND
                   (a voltage divider: more light = higher reading)
    Relay module:  VCC -> VIN (5 V), GND -> GND, IN -> D1
                   Low-level trigger: the pin must go LOW to switch the relay ON.
    Lamp: through the relay's COM and NO contacts. Low-voltage lamps only
          on the bench; anything mains needs an electrician-grade enclosure.

  The delay before switching stops a passing shadow flicking the lamp.
*/

const int LDR_PIN   = A0;
const int RELAY_PIN = D1;

const int DARK_BELOW    = 300;   // pick by watching the Serial Monitor in your room
const int LIGHT_ABOVE   = 450;   // higher than DARK_BELOW = hysteresis, no chatter
const unsigned long HOLD_MS = 3000;  // must stay dark/light this long before switching

bool lampOn = false;
unsigned long since = 0;
bool pendingDark = false;

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);   // HIGH = relay OFF on a low-level-trigger module
}

void loop() {
  int light = analogRead(LDR_PIN);           // 0..1023 on the ESP8266
  bool wantOn = lampOn ? (light < LIGHT_ABOVE) : (light < DARK_BELOW);

  if (wantOn != pendingDark) { pendingDark = wantOn; since = millis(); }
  if (wantOn != lampOn && millis() - since > HOLD_MS) {
    lampOn = wantOn;
    digitalWrite(RELAY_PIN, lampOn ? LOW : HIGH);
    Serial.println(lampOn ? "Lamp ON" : "Lamp OFF");
  }

  Serial.print("light="); Serial.print(light);
  Serial.println(lampOn ? "  lamp on" : "  lamp off");
  delay(200);
}
Pick DARK_BELOW / LIGHT_ABOVE by watching the Serial Monitor in your room.

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.