SimpleClock for ESP32-C6
An ESP32 display project for Waveshare ESP32-C6-Touch-AMOLED-2.16 involving AMOLED display.
Parts and sourceBefore you start
Check the exact AMOLED display configuration against the upstream wiring or parts list.
Upstream README
ESP32 SimpleClock
A desk clock for the Waveshare ESP32-C6-Touch-AMOLED-2.16 (480 × 480 square AMOLED). Big hours on top, minutes below in the same columns, small seconds in the corner. It sets itself over Wi-Fi (NTP), keeps time offline with the on-board RTC, and takes care of the AMOLED panel against burn-in.
| Even hours | Odd hours (side column on the left) | Wi-Fi setup | Status page (tap the screen) |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
The screenshots are read back from a real board with tools/screenshot.py; the Wi-Fi name on the status page is masked.
Features
- Hours and minutes in tabular digits, aligned column by column; seconds in small type at the minutes' lower corner.
- Weekday at the top of the side column; below it the month ("OCT") and the day in a rounded frame whose outer side is on the column edge, centred between the weekday and the SYNC/BATT indicator (in 12-hour mode between the weekday and AM/PM). Weekday, month and day are centred on the frame's axis. The day always has two digits and one fixed size, chosen so the widest day is as wide as "SUN". One setting shows or hides weekday, month and day together.
- Time sync over NTP every hour, with up to three servers you can edit (defaults
tock.stdtime.gov.tw,time.stdtime.gov.tw,pool.ntp.org). Each sync asks four times and keeps the answer with the shortest round trip, corrected for the network delay, so Wi-Fi power save on battery does not throw it off. The PCF85063 RTC is kept in step with NTP, and the clock starts from the RTC time after a restart, so it keeps working without Wi-Fi. The RTC's drift is measured at every sync and corrected automatically. - Eight brightness levels, remembered separately for day and night; automatic night dimming (default 23:00–07:00).
- Screen-off schedule (default 03:00–09:00): the panel is off in that window; a button press or a tap shows the clock for 30 s.
- Auto-rotation from the accelerometer, or a fixed orientation.
- Burn-in protection: the whole face drifts 1 px every minute around a 12 × 12 loop (offsets −6…+5 px); every hour the side column swaps sides and the digits move 92 px; warm white instead of pure white; night dimming; screen-off schedule. Without a valid time ("--") the face still drifts every minute and swaps sides every hour of uptime; the Wi-Fi setup page drifts too and drops to the night brightness after 10 minutes without a press or tap.
- Wi-Fi setup from a phone: the clock opens its own hotspot (random 12-character password) and shows a QR code to join it; the setup page opens by itself (captive portal).
- Settings page at
http://clock.localin Traditional Chinese or English (follows the browser language; switch at the top right). Changes are accepted only from the page itself, not from other web sites. - Diagnostics for Wi-Fi drops: every disconnect and failed attempt is recorded with the USB supply voltage at that moment (settings page, console).
- Indicators: orange
SYNCwhen the last NTP sync is more than 24 h old (or none succeeded in the first 5 minutes after start-up), redBATTwhen the battery is at 15 % or less without USB power.
Hardware
| Part | Details |
|---|---|
| Board | Waveshare ESP32-C6-Touch-AMOLED-2.16 (with or without the lithium battery) |
| MCU | ESP32-C6, RISC-V 160 MHz, 512 KB SRAM, 16 MB flash, no PSRAM; Wi-Fi 6 (2.4 GHz only) |
| Display | 2.16″ AMOLED, 480 × 480, CO5300 controller over QSPI |
| Touch | CST9220 (I²C) |
| Power | AXP2101 PMU: panel power rails, PWR key, battery charging |
| RTC | PCF85063 |
| Motion sensor | QMI8658 (orientation) |
| Buttons | BOOT (GPIO 9), KEY / IO10 (GPIO 10), PWR (through the AXP2101) |
| Cable | USB-C data cable |
No soldering or extra parts are needed.
Buttons
| Button | Press | Hold |
|---|---|---|
| KEY (IO10) | Brightness + | 1.5 s: change orientation (auto → USB down → left → up → right → auto) |
| BOOT | Brightness − | 3 s: open / close Wi-Fi setup |
| PWR | Screen on / off | 6 s: power the board off (AXP2101 hardware; a short press turns it back on, per Waveshare) |
| Tap the screen | Show the status page for 10 s (tap again to close) | — |
While the screen is off, the first press or tap only wakes it. Inside the screen-off schedule it turns off again 30 s after the last press.
Build and flash
You need Python 3 (tested with 3.14) and a USB-C data cable. On the first build PlatformIO downloads the toolchain into ~/.platformio (about 7 GB on the test machine, measured with pioarduino 55.03.38-1), so that run depends on your connection. After that, on the test machine (Apple M4), a full build from a fresh clone took 33–41 s and a rebuild after a small change about 10 s.
macOS / Linux:
git clone https://github.com/marvin3991/ESP32-SimpleClock.git
cd ESP32-SimpleClock
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/pio run -t upload
Windows (PowerShell, not tested):
py -m venv .venv
.venv\Scripts\pip install -r requirements.txt
.venv\Scripts\pio run -t upload
- The serial port is detected automatically; add
--upload-port /dev/cu.usbmodemXXXX(orCOM5) to pick one. - If downloads from GitHub are slow, set
IDF_GITHUB_ASSETS=dl.espressif.com/github_assetsso the compiler is fetched from Espressif's mirror. - Optional, before the first flash: back up the factory firmware (16 MB, about a minute).
backup/is ignored by git.
mkdir -p backup
~/.platformio/penv/bin/esptool --chip esp32c6 read-flash 0 0x1000000 backup/original-flash.bin
To restore it later, run the same command with write-flash 0 backup/original-flash.bin in place of read-flash 0 0x1000000 backup/original-flash.bin.
Wi-Fi
Either of these works; whichever was changed last wins.
.envfile: copy.env.exampleto.env, fill inWIFI_SSIDandWIFI_PASSWORD, and flash again. Quote values that contain spaces or#; outside quotes, a#after a space starts a comment. The file is never committed;scripts/pio_env.pyturns it into a header inside the build directory only.- From a phone: when there is no Wi-Fi and no valid time, the clock starts its setup hotspot by itself; otherwise hold BOOT for 3 s. Scan the QR code on the screen to join
Clock-XXXX(the password is random, 12 characters, and shown on the screen). The setup page opens by itself; if it does not, openhttp://192.168.4.1. If the connection fails, the hotspot stays open so you can correct the settings.
After it has joined your network, the settings page is at http://clock.local (or the IP shown on the status page).
Settings page
Wi-Fi, time zone, 12/24-hour clock, date on/off, three time servers, day and night brightness, night hours, screen-off hours, hourly side swap and orientation. The page never sends the saved Wi-Fi password back to the browser. Anyone on the same network can open it.
Changes are accepted only from the page itself: it sends an X-Clock header that pages from other web sites cannot add, and its API answers only requests addressed to an IP address, a name without dots or a local-only name such as clock.local (this also stops DNS rebinding). Open the page at http://clock.local or at the clock's IP address.
Serial console and screenshots
.venv/bin/python tools/console.py status
.venv/bin/python tools/screenshot.py shot.png
Commands (type help): status, shot, btn boot|key|pwr|touch [short|long], level <1-8>, rot auto|0|1|2|3, page clock|status|setup, settime <unix-epoch>, ntp, rtc (RTC time, where its tick falls against the system clock's second, and its drift correction), diag (Wi-Fi and USB power events since start-up), imu, reboot, factory yes. The tools find the port by themselves on macOS and Linux; add --port to choose one. A screenshot ends with a CRC-32 of its pixels and is retried if the stream was damaged.
Diagnostics
When the clock keeps dropping off Wi-Fi, it records why, with the state of its power supply at that moment. The record lives in RAM: a restart clears it, moving the USB cable to another port or computer does not (the battery bridges the change).
- Settings page, last section: the USB input voltage (now, lowest, highest) and the lowest system rail voltage, how often USB power was lost and how long the clock ran on battery, the PMU's input limits, Wi-Fi drops and failed attempts, offline time in all and the longest stretch, the failure codes, and the last 32 events (Wi-Fi up / lost / failed, USB power off / on) with the signal, the USB voltage, Wi-Fi power save and whether a computer (not a charger) is on the USB port.
- Console:
diagprints the same;statushas a one-line summary. - Status page: the battery line shows the USB voltage (
99% CHG USB 5.01V); while offline, the Wi-Fi line shows the reason.
The codes are the Wi-Fi driver's (ESP-IDF wifi_err_reason_t). On the test network (WPA2/WPA3), a wrong password gave 2 AUTH_EXPIRE and 202 AUTH_FAIL; 2, 15 and 204 are also what timeouts (lost frames) give. Once the stored password has connected, such failures read "HANDSHAKE FAILED", before that "WRONG PASSWORD". Arduino-ESP32 has no names for codes 208–212, so the firmware names them itself; 209 SA_QUERY_TIMEOUT means the router did not answer a protected (802.11w) check that the connection was still there. The shot command holds the main loop while it streams a frame (about 2 s), which shows up as the longest loop. With a computer on the USB port that does not read the serial port (no program has it open), each log line holds the loop for up to 40 ms.
Tests
The NTP code and the timekeeping have tests that run on a computer, without the board. They need a C/C++ compiler and bash: macOS or Linux (on Windows, WSL); so far run with Apple clang only.
tests/run.sh
tests/ntp_proto_test.cpp: NTP packets. Timestamps across the 2036 era change, offset and delay, the error bound, and the replies that must be refused (another request's, kiss-o'-death, an unsynchronised server, malformed or impossible times).tests/diag_test.cpp: the diagnostics bookkeeping. Offline time, the event log and its wrap-around, the failure code counts, the uptime across the 49.7-daymillis()wrap.tests/timekeep_sim.cpp: runssrc/timekeep.cppthrough 72 simulated hours per scenario against a PCF85063 model (crystal error, Offset correction pulses, STOP and release), an ESP32 clock that drifts, and NTP replies with uneven delays. 20 scenarios: crystals from −250 to +60 ppm and one beyond the register (−300), replies held up to 300 ms (Wi-Fi power save), queries held 600 ms (one burst in ten, every burst, every burst after a day of normal ones), a crystal swinging ±3 ppm a day, a time set by hand, NTP and Wi-Fi outages, a server answering with the year 2000, an RTC that lost power. Each checks the final Offset, how often it changed and how far the RTC strayed; the held queries also how far the system clock strayed and how many syncs got through.
All of it takes a few seconds and ends with "host tests passed" (exit status 0); -v also prints the firmware's log. The NTP task itself (src/ntp.cpp: sockets, DNS, timing) runs only on the board; its measurements are under Specifications.
Using another font
The digits and text are pre-rendered from a TrueType font. To use another one:
.venv/bin/python tools/gen_fonts.py --ttf path/to/font.ttf --preview preview.png
.venv/bin/pio run -t upload
The layout (column widths, date size, positions) is recalculated for the new font. The generator stops with an error if any glyph would come closer than 12 px to the screen edge at the largest drift, if digits, the two rows or the side-column text would overlap, or if the date frame would come closer than 12 px to the hour digits or not fit between the weekday and SYNC/BATT or AM/PM. Check the font's license before publishing it.
Specifications
| Item | Value | Notes |
|---|---|---|
| Font | M PLUS Rounded 1c Black | Hours/minutes 176 px cap height, seconds 44 px, weekday and month 25 px, day 39 px, UI text 22 px; ASCII only |
| Layout | Digit columns 159 px; baselines y = 225 / 431; side column 80 px | Digits at x = 35 (column right) or x = 127 (column left) |
| Date | Month above two tabular digits in 35 px cells, both centred by their ink in an 87 × 96 px frame (4 px line in #48484A, corner radius 10 px) whose outer side is on the column edge; the weekday centred on the same axis above it | Widest month (MAY) 73 px of ink, widest day 68 px, "SUN" 67 px. Frame top y = 103, or 89 in 12-hour mode; 12 px from the hour digits |
| Edge margin | ≥ 29 px left, 30 right, 40 top, 41 bottom | At the largest drift, both sides, all digits and labels |
| Rendering | 480 × 32 px bands, only changed areas redrawn | The ESP32-C6 has no PSRAM for a full 450 KB frame |
| Time sync | NTP every 3600 s, own client (RFC 5905): 4 queries 2 s apart to the first of up to 3 servers that answers; the answer with the shortest round trip sets the clock, offset ((t2 − t1) + (t3 − t4)) / 2 | Host names or IPv4 addresses (letters, digits, ., -, up to 63 characters); all empty restores the defaults. After a failure: again after 30 s, doubling up to 10 min. An answer whose error bound (the round trip plus the server's root delay, halved) is over 50 ms is used only when the clock may by now be further off: the last sync's bound plus 20 ppm since (5× the drift measured here); the first after start-up is always used. Against a Mac, 8 forced syncs: spread 3.4 ms, 3.5 ms with Wi-Fi power save (lwIP's SNTP before, 10 syncs: 45 ms and 115 ms) |
| RTC | PCF85063 kept in UTC, marker byte 0xC7 in its RAM register | Ignored when the marker is missing, the oscillator-stop flag is set, or the time is more than a day before the firmware build date |
| RTC setting | Written with STOP held; STOP released at x.500 s | The RTC on this board ticks 0.500 s after the release (datasheet: 0.507813–0.507935 s); its tick then falls +1…+3 ms after the system clock's second (rtc command). With a drift correction the tick also swings by up to 1/1024 s per Offset step every 4 minutes, as the correction pulses come once a second at the start of every 4th minute (measured at −21: +17 ms before them, −2 ms after). Written again when it is surely 100 ms off (beyond the sync's error bound), its correction changed, or the measured drift is implausible (over 260 ppm beyond the noise) |
| RTC drift | Measured at every NTP sync against the time since the RTC was last set | Offset register in fast mode (4.069 ppm per step, applied every 4 minutes); changed when the drift exceeds half a step plus the uncertainty of the two measurements (correction pulses, half of each sync's round trip, the tick reads); a manually set time gives no base; kept in NVS for an RTC power loss (factory yes clears it; it is measured again at the second NTP sync after the restart). This board (firmware log): −85.0 ppm over the first 16 min (7.3 s a day slow) → Offset −21; then +2.4 ppm over 1.04 h (0.2 s a day fast) |
| Diagnostics | Last 32 Wi-Fi and USB power events and counters since start-up, in RAM | Voltages read every 1 s; the PMU latches USB power loss, so short dips count too; diag, settings page |
| Brightness | 8 levels: 3, 8, 16, 30, 55, 95, 160, 255 (register 0x51) | Saved to flash 5 s after the last change |
| Night dimming | Default 23:00–07:00 | Same start and end disables it; may cross midnight |
| Screen-off schedule | Default on, 03:00–09:00 | Switches only at the window edges; 30 s on after a press; never during Wi-Fi setup |
| Orientation | QMI8658, > 0.5 g held for 0.7 s | Lying flat keeps the last orientation |
| Wi-Fi retry | 10 s, doubling up to 5 min | An attempt with no answer for 30 s counts as failed. While offline, a change of USB power or of the USB host (cable moved) retries at once, at most once per 10 s; the doubling stays |
| Wi-Fi power save | Off on USB power, on with battery only | Keeps clock.local responsive |
| Setup hotspot | Clock- + last 2 MAC bytes, WPA2, random 12-character password (31 symbols, no 0/o/1/l/i), 192.168.4.1 |
Closes 8 s after a successful connection, or after 10 min without web requests if Wi-Fi is configured and nothing was tried in this session |
| Memory | RAM 25.8 % (84,428 / 327,680 B), flash 25.1 % (1,642,536 / 6,553,600 B) | Fresh clone without .env, 2026-10-14 |
Credits
- Font: M PLUS Rounded 1c by Coji Morishita and the M+ Fonts Project, © 2016 The Rounded M+ Project Authors, SIL Open Font License 1.1 (from Google Fonts).
- QR codes: QR Code generator library by Project Nayuki (MIT).
- Libraries used at build time: Arduino-ESP32, ESP-IDF, pioarduino, GFX Library for Arduino, SensorLib, XPowersLib.
- Board pins and panel initialisation values: Waveshare's documentation, schematic and examples.
Details and licenses: THIRD_PARTY_NOTICES.md.
License
The code is under the MIT License. The font in fonts/ and the glyph bitmaps generated from it in src/generated/ are under the SIL Open Font License 1.1.



