Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsSwitch a real lamp through a relay when the room gets dark, with a delay that stops it flickering at dusk. Experiment 19 switched LEDs the instant the reading crossed the threshold. A relay driving a real lamp cannot do that, at dusk the light level hovers around the threshold and the relay would chatter, wearing out its contacts in minutes. The fix is a confirmation delay: the new condition must hold continuously for three seconds before the relay is allowed to move. Recording the time the condition first appeared and comparing it with millis() is all that takes.
Switch only low-voltage demo loads such as the LED here. Never wire mains to these relay modules in a classroom project.
Every part for this build comes in the Geekobot Explorer Kit box, a Geekobot exclusive, together with the parts for all 61 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
The brain of every Explorer Kit project
Male-to-female and female-to-female leads for the modules
LEDs from the kit's red, green, yellow and blue packs (one is the lamp the relay switches)
220 ohm resistors: one in series with every LED segment
10k resistor: the fixed half of the light-sensing divider
16x2 LCD with I2C backpack: four wires, two lines of text
LDR: its resistance falls as the light gets brighter
Nano I/O shield from the kit: every pin has its own S, V, G header, so modules plug in with jumpers
Follow the colours: red is power, dark grey is ground, blue is a signal.
explorer_kit_57_smart_room_light.inoVerify & upload in Electrolark#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int ldrPin = A0;
int relayPin = 7;
int darkBelow = 350;
int brightAbove = 450;
unsigned long confirmTime = 3000;
bool lampOn = false;
bool pendingState = false;
unsigned long pendingSince = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Active LOW relay - lamp OFF
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Room Light");
delay(2000);
lcd.clear();
}
void loop()
{
int light = analogRead(ldrPin);
bool wantOn = lampOn;
if (light < darkBelow)
{
wantOn = true;
}
if (light > brightAbove)
{
wantOn = false;
}
if (wantOn != lampOn)
{
if (wantOn != pendingState)
{
pendingState = wantOn;
pendingSince = millis();
}
else if (millis() - pendingSince > confirmTime)
{
lampOn = wantOn;
digitalWrite(relayPin, lampOn ? LOW : HIGH);
Serial.print("Lamp switched ");
Serial.println(lampOn ? "ON" : "OFF");
}
}
else
{
pendingState = lampOn;
}
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(light);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Lamp: ");
lcd.print(lampOn ? "ON " : "OFF");
lcd.print(" ");
delay(200);
}
Use the kit's mini-USB cable, install the CH340 driver on Windows if no port appears, pick Tools > Board > Arduino Nano and try Tools > Processor > ATmega328P (Old Bootloader) if the upload times out.
Turn it round: the long leg (anode) goes to the pin side through the 220 ohm resistor, the short leg to GND.
Turn the small contrast pot on the back of the I2C board. If still blank, change 0x27 in the sketch to 0x3F.
The LDR and the 10k resistor must meet at the analog pin: LDR to 5V, resistor to GND.
Experiment 19 switched LEDs the instant the reading crossed the threshold. A relay driving a real lamp cannot do that, at dusk the light level hovers around the threshold and the relay would chatter, wearing out its contacts in minutes. The fix is a confirmation delay: the new condition must hold continuously for three seconds before the relay is allowed to move. Recording the time the condition first appeared and comparing it with millis() is all that takes.
Light: 720 / Lamp: OFF in a lit room. Cover the LDR and hold it: after 3 seconds the relay clicks and Lamp: ON appears. A brief shadow does nothing at all, the delay filters it out.
Yes. It is project 57 of 61 in the Geekobot Explorer Kit, in level 10 (Smart home). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Explorer Kit project is wired on the Nano I/O shield and the breadboard with jumper wires.
11 parts: Generic Nano CH340 Development Board (Soldered), USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), Through-Hole LED Pack (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), Carbon Film Resistor 1/4W 5% (Pack of 10), LCD1602 16x2 Character LCD Display, 5 mm LDR Light Dependent Resistor, 5V Relay Module with Optocoupler, plus 1 more. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 45 minutes. The build on this page has 7 steps.
No. This page has the complete, tested code (explorer_kit_57_smart_room_light.ino).
About ₹653 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Light automation with relays; Confirmation delays to stop relay chatter; Two thresholds plus a timer for stability; Why real switching gear needs gentler logic than an LED.
Switch only low-voltage demo loads such as the LED here. Never wire mains to these relay modules in a classroom project.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.