An Arduino-based automated power regulator — scheduling relay switching via a real-time clock and cutting power based on ambient light levels through an LDR sensor, all configurable from a 4x4 keypad with live LCD feedback.
- Overview
- Features
- Hardware Required
- Pin Connections
- Libraries Required
- First-Time RTC Setup
- How to Use
- Serial Monitor Output
- Project Structure
- License
Arduino Power Regulator connects two independent relay channels to two separate control strategies — time and ambient light. Relay 1 follows a user-defined ON/OFF schedule stored against the DS1302 real-time clock, cutting or restoring power to a load on a fixed daily timetable. Relay 2 reads ambient brightness from an LDR and switches power automatically when darkness falls below a configurable threshold. Both channels are fully configurable at runtime from a 4x4 keypad, with live status shown on a 16x2 I2C LCD. No PC connection, no re-flashing required — everything is set in the field.
| Feature | Description |
|---|---|
| Time-Scheduled Relay | Relay 1 follows a custom ON/OFF time set via keypad against the DS1302 RTC |
| Ambient Light Relay | Relay 2 switches ON when dark and OFF when bright using an LDR |
| Adjustable LDR Threshold | Darkness sensitivity configured from the keypad — no code changes needed |
| Overnight Schedule Support | Correctly handles schedules that cross midnight (e.g. 22:00 to 06:00) |
| Manual Override | Relay 1 can be toggled instantly from the keypad at any time |
| Backspace Input | Press C while entering numbers to delete the last digit |
| Live LCD Status | Current time, date, and both relay states always visible on screen |
| Serial Monitor Logging | All sensor readings and relay events printed at 9600 baud for debugging |
| Component | Quantity |
|---|---|
| Arduino Uno or Mega | 1 |
| DS1302 RTC Module | 1 |
| 16x2 I2C LCD (address 0x27) | 1 |
| 4x4 Matrix Keypad | 1 |
| LDR (Light Dependent Resistor) | 1 |
| 10k Ohm Resistor (voltage divider) | 1 |
| 5V Relay Module — Normally Open x2 | 2 |
| Jumper Wires | Several |
| Breadboard | 1 |
| DS1302 Pin | Arduino Pin |
|---|---|
| DAT (IO) | D7 |
| CLK | D6 |
| RST (CE) | D8 |
| VCC | 5V |
| GND | GND |
| LCD Pin | Arduino Pin |
|---|---|
| SDA | A4 |
| SCL | A5 |
| VCC | 5V |
| GND | GND |
The I2C address is set to
0x27. If your display stays blank on startup, try0x3Fand update the address in the code on line 4.
| Keypad Pin | Arduino Pin |
|---|---|
| R1 | D9 |
| R2 | D10 |
| R3 | D11 |
| R4 | D12 |
| C1 | D5 |
| C2 | D4 |
| C3 | D3 |
| C4 | D2 |
Wire the LDR as a voltage divider with a 10k Ohm resistor:
5V ──── LDR ──── A0 ──── 10kOhm ──── GND
| Connection | Arduino Pin |
|---|---|
| LDR junction (middle point) | A0 |
Raw analog values range from 0 (very dark) to 1023 (very bright). Relay 2 activates when the reading falls below your configured threshold.
| Relay | Arduino Pin | Function |
|---|---|---|
| Relay 1 | D13 | Time-scheduled load |
| Relay 2 | A1 | LDR ambient light load |
Both relays must be wired as Normally Open (NO). The relay closes when the Arduino sends
HIGH, completing the circuit to the load.
Safety: If switching mains voltage AC loads, use an optocoupler-isolated relay module and ensure all high-voltage wiring is handled by a qualified electrician.
Install via the Arduino IDE Library Manager — Sketch > Include Library > Manage Libraries:
| Library | Purpose |
|---|---|
LiquidCrystal_I2C |
I2C LCD display driver |
ThreeWire |
DS1302 three-wire communication |
Rtc by Makuna |
DS1302 RTC driver |
Keypad |
4x4 matrix keypad input |
Wire |
I2C bus (built-in, no install needed) |
The DS1302 must have its date and time initialized before use. If the LCD shows "RTC ERROR!! Upload Code 1!" on startup, upload the sketch below once to set the module, then immediately re-upload the main project sketch.
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(7, 6, 8); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);
void setup() {
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
Rtc.SetDateTime(compiled);
Rtc.SetIsWriteProtected(false);
Rtc.SetIsRunning(true);
}
void loop() {}The LCD shows live status at all times:
12:45:30 R1:OFF
D:01/05/26 R2:ON
| Row | Content |
|---|---|
| Row 0 | Current time + Relay 1 state |
| Row 1 | Current date + Relay 2 state |
| Key | Action |
|---|---|
A |
Open schedule setup for Relay 1 |
B |
Set LDR darkness threshold for Relay 2 |
D |
Manually toggle Relay 1 ON / OFF |
# |
Confirm input / advance to next step |
* |
Cancel and return to normal screen |
C |
Backspace — delete last entered digit |
0 – 9 |
Numeric input |
- Press A from the normal screen.
- Enter the ON hour (00–23) then press
#. - Enter the ON minute (00–59) then press
#. - Enter the OFF hour (00–23) then press
#. - Enter the OFF minute (00–59) then press
#. - Review the schedule summary shown on the LCD.
- Press
#to save or*to cancel.
Overnight schedules are fully supported. Setting ON=22:00 and OFF=06:00 will correctly keep the relay on through midnight.
Press C at any input step to delete the last typed digit.
- Press B from the normal screen.
- The current threshold value is shown on the LCD.
- Enter a new value between 0 and 1023 then press
#. - Press
*to cancel without saving.
Threshold reference:
| Environment | Typical Raw LDR Value |
|---|---|
| Bright sunlight | 800 – 1023 |
| Indoor lighting | 400 – 700 |
| Dusk / dim | 150 – 400 |
| Dark / night | 0 – 150 |
A threshold of 400 means Relay 2 activates when the raw reading drops below 400. Adjust to suit your specific LDR and environment — every LDR behaves slightly differently.
Press D from the normal screen to immediately flip Relay 1. This disables the active schedule until a new one is saved via A.
Connect at 9600 baud to view live logs:
Time=12:45:30 Light=312 Thresh=400 R1=OFF R2=ON
RELAY2 ON
ON=18:00 OFF=06:00
RELAY1 ON
arduino-power-regulator/
├── power_regulator.ino # Main sketch
└── README.md # This file
This project is open source under the MIT License. Feel free to use, modify, and share.
Built for reliable, fieldworthy power regulation.

