Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsTurn the light on when someone walks in and off again a set time after they leave, resetting the timer on every movement. The key idea is the retriggerable timer. Every time motion is seen, lastMotion is set to the current millis(), so the countdown restarts rather than continuing. The light only goes out once a full quiet period has passed with no movement at all. This is exactly how commercial occupancy sensors behave, and it is why they do not switch off while you are still standing there.
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
16x2 LCD with I2C backpack: four wires, two lines of text
IR obstacle sensor: OUT goes LOW when something is in front
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_58_automatic_bathroom_light.inoVerify & upload in Electrolark#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int sensorPin = 2;
int relayPin = 7;
unsigned long holdTime = 15000; // Stay on 15 s after last motion
unsigned long lastMotion = 0;
bool lightOn = false;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Lamp OFF
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Auto Bathroom");
delay(2000);
lcd.clear();
}
void loop()
{
bool motion = (digitalRead(sensorPin) == LOW);
if (motion)
{
lastMotion = millis(); // Retrigger the timer
if (!lightOn)
{
lightOn = true;
digitalWrite(relayPin, LOW);
Serial.println("Motion detected - light ON");
}
}
if (lightOn && millis() - lastMotion > holdTime)
{
lightOn = false;
digitalWrite(relayPin, HIGH);
Serial.println("No motion - light OFF");
}
lcd.setCursor(0, 0);
if (lightOn)
{
lcd.print("Light: ON ");
long remaining = (holdTime - (millis() - lastMotion)) / 1000;
if (remaining < 0)
{
remaining = 0;
}
lcd.setCursor(0, 1);
lcd.print("Off in: ");
lcd.print(remaining);
lcd.print("s ");
}
else
{
lcd.print("Light: OFF ");
lcd.setCursor(0, 1);
lcd.print("Waiting... ");
}
delay(150);
}
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.
These relay modules are active LOW: writing LOW switches them on. The sketch already does this, so check IN goes to the right pin.
The key idea is the retriggerable timer. Every time motion is seen, lastMotion is set to the current millis(), so the countdown restarts rather than continuing. The light only goes out once a full quiet period has passed with no movement at all. This is exactly how commercial occupancy sensors behave, and it is why they do not switch off while you are still standing there.
Wave a hand in front of the sensor: the relay clicks and Light: ON appears. The second line counts down from 15 seconds. Move again and the countdown restarts from 15.
Yes. It is project 58 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.
10 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), LCD1602 16x2 Character LCD Display, 5V Relay Module with Optocoupler, Generic Infrared Obstacle Avoidance Sensor Module (Active Low) – LM393 Comparator, 2-30cm Range and Arduino Nano IO Expansion Shield (328P Compatible). 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_58_automatic_bathroom_light.ino).
About ₹662 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
IR triggers and retriggerable timers; Restarting a countdown on every event; Displaying remaining time from a millis() difference; Modelling real occupancy-sensor behaviour.
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.
Learning project. Check every circuit and the datasheet of each part before you power it up.