Build it step by step
0 of 8 doneWire the clock
SDA to A4, SCL to A5, power from 5V and GND.
Wire the LEDs
Two on D9 and two on D10, each with its own 220 ohm.
Wire the sensor
LDR to 5V, junction to A0, 10k to GND.
Wire the button
D2 to GND.
Install RTClib
From the Library Manager.
Test fast
Set DEMO true and upload.
Watch the fade
Glow rises over about 20 seconds.
Set the real wake time
DEMO false, set WAKE_H and WAKE_M.
How do you build a sunrise wake-up lamp?
A small LED lamp fades up slowly at a set time, and a light sensor holds it lower in a bright room. You need Arduino Nano, DS3231 RTC module, LED 5 mm white × 4, Resistor 220 Ω × 4, LDR Photoresistor, Resistor 10 kΩ, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 9
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- OptoelectronicsBuyLED 5 mm white × 4
- PassivesBuyResistor 220 Ω × 4
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| RTC VCC and GND | Nano 5V and GND | Power |
| RTC SDA | Nano A4 | I2C data |
| RTC SCL | Nano A5 | I2C clock |
| Nano D9 | Two 220 ohm resistors, each to a white LED anode | First pair of LEDs |
| Nano D10 | Two 220 ohm resistors, each to a white LED anode | Second pair of LEDs |
| All four LED cathodes | Nano GND | Common ground |
| LDR one leg | Nano 5V | Top of the light divider |
| LDR other leg and 10k resistor | Nano A0, 10k other end to GND | Bright light gives a higher reading |
| Button | Nano D2 to GND | Cancel today's wake-up |
Code
A sunrise lamp wakes you with light that rises slowly instead of a sudden alarm. Here a DS3231 real time clock keeps the time and a Nano raises four white LEDs from off to full over 20 minutes starting at the wake time, then holds them for another 30 minutes. Two points make the fade look natural. The first is gamma: your eye sees brightness roughly as a power of the light, so the sketch raises the fade fraction to the power of 2.2 before setting the PWM, and the lamp seems to grow evenly instead of jumping on at the start. The second is the light sensor. A photoresistor in a divider with a 10 kilohm resistor gives a higher reading when the room is bright, and if the reading goes above a threshold the lamp is capped at 30 percent, so it does not shine at full power in a room that already has daylight. A button cancels today's wake-up. To test quickly, set DEMO to true and the fade runs in 20 seconds. Install RTClib by Adafruit. The LEDs here are small indicator parts, so the lamp is a soft glow rather than a reading light, and each LED has its own 220 ohm resistor and each pin drives only two LEDs to stay inside the pin current limit. Do not use it as a clinical light therapy device.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <RTClib.h> // "RTClib" by Adafruit
RTC_DS3231 rtc;
const int LED_A = 9, LED_B = 10, LDR = A0, BTN = 2;
const int WAKE_H = 7, WAKE_M = 0; // wake time
const int FADE_MIN = 20; // minutes to fade from off to full
const int STAY_MIN = 30; // minutes to stay on after full
const int ROOM_BRIGHT = 600; // LDR reading above this means a bright room
const bool DEMO = false; // true: fade over 20 seconds, to test quickly
int cancelledDay = -1;
void setup() {
pinMode(LED_A, OUTPUT); pinMode(LED_B, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
if (!rtc.begin()) { Serial.println("No RTC found"); while (1); }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // sets the clock to the upload time
}
void loop() {
DateTime now = rtc.now();
if (digitalRead(BTN) == LOW) cancelledDay = now.day();
float m; // minutes since the wake time
if (DEMO) {
m = (millis() / 1000UL) % 60; // one second stands for one minute
} else {
m = (now.hour() * 60 + now.minute()) - (WAKE_H * 60 + WAKE_M);
if (m < 0) m += 1440;
}
float frac = 0;
if (cancelledDay != now.day() && m < FADE_MIN + STAY_MIN) {
frac = (m >= FADE_MIN) ? 1.0 : m / FADE_MIN;
}
int room = analogRead(LDR);
float cap = (room > ROOM_BRIGHT) ? 0.3 : 1.0; // dim the lamp when it is already bright
float level = pow(frac * cap, 2.2); // gamma, so the fade looks smooth
int duty = (int)(level * 255);
analogWrite(LED_A, duty);
analogWrite(LED_B, duty);
static unsigned long last = 0;
if (millis() - last > 10000) {
last = millis();
Serial.print(now.hour()); Serial.print(":"); Serial.print(now.minute());
Serial.print(" room "); Serial.print(room);
Serial.print(" duty "); Serial.println(duty);
}
delay(100);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
No RTC found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Before the wake time | LEDs off |
| Fade period | Smooth rise with no steps |
| Cover the LDR | Lamp can reach full power |
| Shine a light on the LDR | Lamp capped lower |
| Press the button | Lamp stays off for the rest of the day |
If it does not work
No RTC found
Check SDA, SCL and power on the clock module.
Wrong time
Upload once so the clock is set, then check the coin cell.
Fade looks like steps
Check the gamma line and the PWM pins.
Lamp always dim
Check the LDR divider, the room reading may be above the threshold.
Pin or LEDs warm
Check each LED has its own 220 ohm.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the sunrise wake-up lamp?
Arduino Nano, DS3231 RTC module, LED 5 mm white × 4, Resistor 220 Ω × 4, LDR Photoresistor, Resistor 10 kΩ, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the sunrise wake-up lamp take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the sunrise wake-up lamp works?
Before the wake time: LEDs off. Fade period: Smooth rise with no steps. Cover the LDR: Lamp can reach full power.
No RTC found: what should I check?
Check SDA, SCL and power on the clock module.
Wrong time: what should I check?
Upload once so the clock is set, then check the coin cell.
Fade looks like steps: what should I check?
Check the gamma line and the PWM pins.
Lamp always dim: what should I check?
Check the LDR divider, the room reading may be above the threshold.
