Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA dusk-to-dawn light wastes power after everyone is home. This one needs both conditions: the LDR must see darkness and the clock must be before 23:00. Sunset moves through the year, and the light follows it.
Keep to low-voltage loads unless a qualified electrician wires the mains side in an earthed, closed enclosure.
Tick anything you already have and the total updates.
Connects the Uno to your computer to upload code and power it
Temperature-compensated real-time clock (±2 minutes a year) with a coin-cell backup, so the time survives power cuts.
Light-dependent resistor with a comparator: its output flips when the light falls below the level set by the trimmer.
An electrically isolated switch that lets a 3.3/5 V pin turn a 12 V (or mains) load on and off.
Classic 16-character, 2-line LCD with an I²C backpack, so it needs only two signal wires.
12 V supply for a 12 V LED porch lamp on the relay
Follow the colours: red is power, dark grey is ground, blue is a signal.
porch_light_dusk_timer.inoVerify & upload in Electrolark// Porch light: on at dusk, off at 11
// Geekobot project library · board: Arduino Uno
// Wiring diagram, parts list and build steps: Geekobot project page "porch-light-dusk-timer"
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
// ---- pins ----
const uint8_t LDR_PIN = 2;
const uint8_t RELAY_PIN = 3;
RTC_DS3231 rtc;
const bool RELAY_ACTIVE_LOW = true; // most relay modules switch on when IN is LOW
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ---- latest readings ----
bool dark = false;
struct Slot { uint8_t h1, m1, h2, m2; }; // on from h1:m1 until h2:m2 (24-hour clock)
const Slot SLOTS[] = {{17, 0, 23, 0}};
const int NSLOTS = sizeof(SLOTS) / sizeof(SLOTS[0]);
bool scheduled = false, output = false;
unsigned long lastShow = 0;
void setRelay(bool on) { digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW); }
bool inSlot(int mins) {
for (int i = 0; i < NSLOTS; i++) {
int a = SLOTS[i].h1 * 60 + SLOTS[i].m1, z = SLOTS[i].h2 * 60 + SLOTS[i].m2;
if (a <= z ? (mins >= a && mins < z) : (mins >= a || mins < z)) return true; // slots may run past midnight
}
return false;
}
void setup() {
Serial.begin(115200);
if (!rtc.begin()) Serial.println(F("RTC not found"));
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set from the upload time
pinMode(LDR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
lcd.init();
lcd.backlight();
setRelay(false);
}
void loop() {
DateTime now = rtc.now();
int mins = now.hour() * 60 + now.minute();
bool s = inSlot(mins) && digitalRead(LDR_PIN) == HIGH; // only after dark
if (s != scheduled) { scheduled = s; }
output = scheduled;
setRelay(output);
if (millis() - lastShow > 1000) {
lastShow = millis();
char buf[17];
snprintf(buf, sizeof(buf), "%02d:%02d:%02d ", now.hour(), now.minute(), now.second());
lcd.setCursor(0, 0); lcd.print(buf);
lcd.setCursor(0, 1); lcd.print(F("Porch ")); lcd.print(output ? F(" ON ") : F(" off "));
Serial.print(buf); Serial.println(output ? F("ON") : F("off"));
}
delay(50);
}
The sketch above runs here on a simulated Arduino Uno. Move the sliders for the sensors and buttons it reads, and watch the Serial monitor, display and outputs change.
The coin cell is flat or missing. Some modules need the charging resistor removed when using a CR2032.
Some modules invert DO. Change == HIGH to == LOW in the code.
Load must be on COM and NO, with its own supply in series.
Your module may be active-HIGH. Set RELAY_ACTIVE_LOW to false.
Some 5 V relay boards do not fully switch off with 3.3 V logic. Remove the JD-VCC jumper, feed JD-VCC from 5 V and VCC from 3.3 V, or use a relay board made for 3.3 V input.
Adjust the contrast pot on the backpack. If still blank, change 0x27 to 0x3F.
Open the Serial Monitor at 115200 baud and check for messages. Make sure you chose the right board and port in the Arduino IDE.
Close other programs using the serial port, try another USB cable (some are charge-only) and press reset just before uploading.
Yes. Most of the code works unchanged on an ESP32 with the ESP32 board package. Watch for 5 V modules: ESP32 pins are 3.3 V, so 5 V output signals need a resistor divider.
The relay is rated for mains, but mains wiring is dangerous. Keep this build on low-voltage loads (12 V pumps, fans, LED strips), or have a qualified electrician fit the relay inside an earthed, closed enclosure.
Use a 5 V USB phone charger or a power bank into the board's USB port.
9 parts: Uno R3 CH340G ATmega328p Compatible Development Board, Arduino USB-A to USB-B Data Cable for UNO/MEGA, DS3231 Precision RTC Module with EEPROM, Digital LDR Light Sensor Module – LM393, 5V Relay Module with Optocoupler (1 channel), LCD1602 16x2 Character LCD (blue, with I2C adapter), DC Power Adapter 12V 2A (5.5x2.1mm), Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. 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 2 hours. The build on this page has 10 steps.
No. This page has the complete, tested code (porch_light_dusk_timer.ino). Install these libraries first: RTClib and LiquidCrystal I2C.
Yes. The Run it panel under the code runs this exact sketch on a simulated board. Move the sliders or press the buttons for the sensors it reads, and the Serial monitor, display and outputs react as they will on the real build. It is a simulation, so real readings depend on your parts and wiring.
About ₹860 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Character displays and I²C backpacks; Isolation, relays and switching higher-power loads; Real-time clocks and backup batteries; Thresholds set in hardware vs software.
Ideas that reuse this build: Automatic dusk-to-dawn light and Geyser timer. They are listed under Take it further on this page.
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.
09:30:01 off 09:30:02 off
A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.