Projects / Sunrise alarm lamp

Sunrise alarm lamp

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANO18.2 mA

    Wire the ring

    Add the capacitor and data resistor first.

  2. NANODS323113:27

    Set the clock

    Upload RTClib's ds3231 example once to set the time.

  3. NANO12:45

    Add the display and buttons

    Check the clock shows on the TM1637.

  4. NANO

    Set the alarm

    Press SET to choose hour or minute, UP to change them, SET to save.

  5. NANO

    Test the fade

    Set the alarm a few minutes ahead and temporarily shorten FADE_S to 120 seconds.

  6. NANO

    Restore 30 minutes

    Put FADE_S back to 1800 and diffuse the light.

In short

How do you build a sunrise alarm lamp?

Thirty minutes before your alarm, a lamp slowly brightens from deep red to warm white so you wake gently. You need Arduino Nano, WS2812B ring 16 LEDs, DS3231 RTC module, TM1637 4-digit display, Tactile push button 6 mm × 2, 5 V 2 A USB adapter, Resistor 330 Ω and Electrolytic capacitor 1000 µF. It takes 6 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
5 V USB adapter +LED ring 5V, Nano 5V pin and capacitor +Power
5 V USB adapter -LED ring GND, Nano GND and capacitor -Common ground
1000 µF capacitorAcross 5V and GNDSmooths power
Nano D6330 Ω resistor then LED ring DINData
DS3231 SDA, SCLNano A4, A5I2C
DS3231 VCC, GNDNano 5V, GNDPower
TM1637 CLK, DIONano D4, D5Clock display
TM1637 VCC, GNDNano 5V, GNDPower
Set buttonNano D2 and GNDChoose hour or minute
Up and snooze buttonNano D3 and GNDAdjust the alarm or snooze

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "RTClib", "Adafruit NeoPixel" and "TM1637" by Avishay Orpaz. Set the DS3231 clock once with RTClib's example. Brightness is capped near 200 of 255 so the ring stays within a 2 A adapter, and the 1000 µF capacitor and 330 Ω data resistor protect the LEDs. In the setting mode the display shows the alarm; the UP button moves the hour by one or the minutes by five, and pressing SET again stores it. Use the lamp with a diffuser such as a frosted lampshade.

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>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#include <TM1637Display.h>

RTC_DS3231 rtc;
Adafruit_NeoPixel ring(16, 6, NEO_GRB + NEO_KHZ800);
TM1637Display display(4, 5);
const byte SET = 2, UP = 3;
byte alarmH = 7, alarmM = 0, mode = 0;              // mode 0 clock, 1 set hour, 2 set minute
unsigned long snoozeUntil = 0;
const unsigned long FADE_S = 1800UL;                // 30 minute fade

void saveAlarm() { EEPROM.update(0, alarmH); EEPROM.update(1, alarmM); }

void setup() {
  pinMode(SET, INPUT_PULLUP); pinMode(UP, INPUT_PULLUP);
  rtc.begin();
  ring.begin();
  display.setBrightness(2);
  alarmH = EEPROM.read(0); alarmM = EEPROM.read(1);
  if (alarmH > 23 || alarmM > 59) { alarmH = 7; alarmM = 0; }
}

void loop() {
  if (!digitalRead(SET)) { mode = (mode + 1) % 3; if (mode == 0) saveAlarm(); delay(300); }
  if (!digitalRead(UP)) {
    if (mode == 1) alarmH = (alarmH + 1) % 24;
    else if (mode == 2) alarmM = (alarmM + 5) % 60;
    else snoozeUntil = millis() + 5UL * 60000UL;     // snooze: dim for 5 minutes
    delay(250);
  }

  DateTime n = rtc.now();
  if (mode == 0) display.showNumberDecEx(n.hour() * 100 + n.minute(), 0b01000000, true);
  else display.showNumberDecEx(alarmH * 100 + alarmM, 0b01000000, true);

  long nowS = (long)n.hour() * 3600 + n.minute() * 60L + n.second();
  long startS = ((long)alarmH * 3600 + alarmM * 60L - FADE_S + 86400L) % 86400L;
  long elapsed = (nowS - startS + 86400L) % 86400L;
  bool active = elapsed < FADE_S + 600 && mode == 0 && millis() > snoozeUntil;   // fade, then stay on 10 minutes
  if (active) {
    float p = elapsed >= FADE_S ? 1.0 : (float)elapsed / FADE_S;
    byte level = (byte)(pow(p, 2.2) * 199 + 1);       // gamma curve so the fade looks even
    uint8_t r = 255, g = 20 + (uint8_t)(160 * p), b = (uint8_t)(100 * p * p);
    ring.setBrightness(level);
    for (byte i = 0; i < 16; i++) ring.setPixelColor(i, ring.Color(r, g, b));
  } else {
    ring.clear();
  }
  ring.show();
  delay(100);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Well before the alarmLamp off
Start of the fadeVery dim red
Alarm timeBright warm white
UP pressed during the fadeLamp goes dark for 5 minutes then returns

If it does not work

Fade starts at the wrong time

Set the DS3231 clock and check the alarm hour.

No light

Check DIN and the 5 V supply and that the ring ground is shared.

Flicker

Use a 2 A adapter and keep the capacitor in place.

Alarm not remembered

Press SET to leave the setting mode so it saves.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the sunrise alarm lamp?

Arduino Nano, WS2812B ring 16 LEDs, DS3231 RTC module, TM1637 4-digit display, Tactile push button 6 mm × 2, 5 V 2 A USB adapter, Resistor 330 Ω and Electrolytic capacitor 1000 µF.

How long does the sunrise alarm lamp take to build?

About 120 minutes for a intermediate maker, in 6 steps.

How do I know the sunrise alarm lamp works?

Well before the alarm: Lamp off. Start of the fade: Very dim red. Alarm time: Bright warm white.

Fade starts at the wrong time: what should I check?

Set the DS3231 clock and check the alarm hour.

No light: what should I check?

Check DIN and the 5 V supply and that the ring ground is shared.

Flicker: what should I check?

Use a 2 A adapter and keep the capacitor in place.

Alarm not remembered: what should I check?

Press SET to leave the setting mode so it saves.