Clocks
Gentle alarm clock
An alarm that starts soft and slow, then grows faster and higher over a minute, so you wake up without a fright.
- Medium
- about 60 min
- ESP32-S3
- 8 parts
- 11 wires
- 99 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The clock takes the time from the internet, so it is never wrong and never needs setting. The interesting part is the ramp: one number that goes from 0 to 1 controls both the pitch and the gap between beeps.
What you learn
Getting the time over Wi-Fi with NTP, and turning elapsed time into a smooth change.
How it works
input
Stop button
decides
ESP32-S3 · Wi-Fi
output
TFT 1.54 in
Buzzer
Powered with 5 V through the board's USB-C port.
It joins your home Wi-Fi. You type the network name and password into the sketch before uploading; they stay on the board.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 11
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
gentle-alarm.ino
// Gentle alarm clock
// The alarm starts quiet and slow, then gets faster and higher over one minute.
#include <WiFi.h>
#include <time.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const long UTC_OFFSET_S = 7 * 3600; // Vietnam: UTC+7
const int ALARM_HOUR = 6;
const int ALARM_MINUTE = 30;
const int PIN_TFT_SCL = 12;
const int PIN_TFT_SDA = 11;
const int PIN_TFT_RES = 14;
const int PIN_TFT_DC = 13;
const int PIN_TFT_CS = 10;
const int PIN_BUTTON_A = 5;
const int PIN_BUZZER_POS = 17;
Adafruit_ST7789 tft(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RES);
bool ringing = false;
bool dismissedToday = false;
unsigned long ringStart = 0;
unsigned long nextBeep = 0;
int shownMinute = -1;
void drawTime(const struct tm& now) {
char text[6];
snprintf(text, sizeof(text), "%02d:%02d", now.tm_hour, now.tm_min);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(7);
tft.setCursor(16, 70);
tft.print(text);
tft.setTextSize(2);
tft.setTextColor(ST77XX_YELLOW);
tft.setCursor(40, 170);
tft.printf("Alarm %02d:%02d", ALARM_HOUR, ALARM_MINUTE);
}
void setup() {
pinMode(PIN_BUTTON_A, INPUT_PULLUP);
pinMode(PIN_BUZZER_POS, OUTPUT);
SPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
tft.init(240, 240);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 110);
tft.print("Wi-Fi...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) delay(250);
configTime(UTC_OFFSET_S, 0, "pool.ntp.org", "time.google.com");
}
void loop() {
struct tm now;
if (!getLocalTime(&now, 200)) {
delay(500);
return;
}
if (now.tm_min != shownMinute) {
shownMinute = now.tm_min;
drawTime(now);
if (now.tm_hour == 0 && now.tm_min == 0) dismissedToday = false;
}
bool alarmMinute = now.tm_hour == ALARM_HOUR && now.tm_min == ALARM_MINUTE;
if (alarmMinute && !ringing && !dismissedToday) {
ringing = true;
ringStart = millis();
nextBeep = 0;
}
if (ringing) {
if (digitalRead(PIN_BUTTON_A) == LOW) {
ringing = false;
dismissedToday = true;
noTone(PIN_BUZZER_POS);
} else if (millis() >= nextBeep) {
// 0.0 at the start, 1.0 after a minute: shorter gaps, higher notes.
float ramp = min(1.0f, (millis() - ringStart) / 60000.0f);
int pitch = 500 + (int)(ramp * 1500);
int gap = 1500 - (int)(ramp * 1250);
tone(PIN_BUZZER_POS, pitch, 120);
nextBeep = millis() + gap;
}
}
delay(20);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit ST7735 and ST7789 Library: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Before uploading, type your Wi-Fi name and password into WIFI_SSID and WIFI_PASS at the top of the sketch.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.