Ning Kailiang Electronics Blog 简体中文
STM32 calendar clock

STM32 calendar clock:How do I implement a calendar clock on STM32 in 2026 using the built-in RTC?

Author:Ning Kailiang Electronics Blog · Date:20260929 · Cooperation · Report

This page answers the following questions about“STM32 calendar clock”:How do I implement a calendar clock on STM32 in 2026 using the built-in RTC?What is the best way to keep STM32 calendar clock accurate over long periods in 2026?Can the STM32 calendar clock wake the MCU from low-power modes for scheduled tasks?

Q: How do I implement a calendar clock on STM32 in 2026 using the built-in RTC?

A: In 2026, implementing a calendar clock on STM32 is best done with the built-in RTC peripheral, which now supports calendar mode on most STM32 families including H5, U5, and the newer H7R/S series. Start by enabling the RTC clock source, typically a 32.768 kHz LSE crystal for accuracy, and configure the prescalers so the RTC ticks once per second. Then set the date and time registers using the HAL_RTC_SetDate and HAL_RTC_SetTime functions from STM32CubeIDE or the newer STM32CubeMX 6.14 generated code. The RTC automatically handles month lengths and leap years up to 2100, so you do not need manual correction logic. For battery-backed operation, connect a CR2032 or supercapacitor to VBAT so the calendar keeps running when main power is removed. Add a backup register to flag whether the time has been initialized. To read the current time, call HAL_RTC_GetTime followed by HAL_RTC_GetDate in that exact order, because reading the time register latches the date shadow registers. Many 2026 projects also combine the RTC with a low-power timer and an e-paper or OLED display refreshed only once per minute to keep average current in the microamp range.

Q: What is the best way to keep STM32 calendar clock accurate over long periods in 2026?

A: Accuracy over months and years depends mainly on the clock source and calibration. In 2026, the preferred approach for STM32 calendar clocks is a 32.768 kHz crystal on the LSE oscillator with a load capacitance matched to the crystal datasheet, usually 6 to 12 pF. Even good crystals drift a few seconds per month, so use the RTC calibration features available on STM32 devices such as the H5 and U5 series. These chips offer smooth digital calibration with roughly 0.95 ppm resolution, letting you correct measured drift without changing the hardware. Measure your board against a reliable reference, for example a GPS PPS signal or an NTP-synced gateway, over several days, then compute the correction value and write it to the RTC calibration register. If your product has network access, a common 2026 pattern is to periodically sync the STM32 RTC from SNTP over Ethernet or Wi-Fi and store the offset. For completely offline designs, consider a temperature-compensated crystal oscillator or an external RTC like the DS3231, which holds about two minutes per year. Always keep the crystal traces short, guard them with ground, and avoid routing near switching regulators, because noise on the LSE pins is the most common cause of unexpected drift.

Q: Can the STM32 calendar clock wake the MCU from low-power modes for scheduled tasks?

A: Yes, and this is one of the most useful 2026 design patterns for battery-powered STM32 products. The RTC alarm and wakeup timer can generate interrupts or events that bring the MCU out of Stop, Standby, or the newer low-power modes on STM32U0, U5, and H5 devices. In Stop mode the RTC keeps running from the LSE or LSI clock while the core is off, so an alarm set for a specific calendar time, such as 06:00 every day, will wake the chip. In Standby mode, waking resets most of the system, so you should save state in the backup SRAM or RTC backup registers before sleeping and restore it after wake. A typical implementation sets an RTC alarm for the next scheduled event, enters the lowest possible power mode, then on wake performs the task and reprograms the next alarm. The STM32 wakeup timer is better for fixed periodic intervals, from microseconds up to 36 hours, while the alarm is better for real calendar times. Watch out for the alarm flag: always clear it in the interrupt handler, or the MCU will wake continuously. Also account for the few milliseconds of startup time after wake when timing critical tasks.

STM32 calendar clock

Dialogue about

Common scenarios of "STM32 calendar clock"

【Engineer】 Hi! I'm working on a project to build a calendar clock using an STM32 microcontroller. Do you have any advice on where to start?

【Expert】 Great project! First, you'll need to choose an STM32 model. A common choice is the STM32F103C8T6 (Blue Pill) because it's cheap and has plenty of peripherals. For a calendar clock, you'll need an RTC (Real-Time Clock) peripheral, which many STM32s have built-in.

【Engineer】 I have an STM32F103C8T6. I heard it has a built-in RTC, but it requires an external crystal. Is that correct?

【Expert】 Yes, the STM32F103 has an RTC, but it needs a 32.768 kHz crystal connected to the LSE (Low-Speed External) oscillator pins. That gives you accurate timekeeping. Alternatively, you can use the internal LSI, but it's less accurate.

【Engineer】 I'll use the LSE with a 32.768 kHz crystal. How do I configure the RTC in code?

【Expert】 You can use STM32CubeMX to configure the RTC. Enable the LSE, then enable the RTC and set it to use the LSE. You also need to configure the RTC calendar. After generating code, you can use HAL_RTC_SetTime and HAL_RTC_SetDate to set the initial time and date.

【Engineer】 Great, I'll try that. For the display, I'm thinking of using an LCD. What type would you recommend?

【Expert】 A common choice is a 16x2 character LCD with an I2C backpack, which only requires two pins. Or you could use an OLED display for a more modern look. Both are easy to interface with STM32.

【Engineer】 I have a 16x2 LCD with I2C. How do I connect it to the STM32?

【Expert】 Connect the I2C backpack's SDA to the STM32's I2C SDA pin (e.g., PB7) and SCL to SCL (e.g., PB6). Also connect VCC and GND. Then use a library like LiquidCrystal_I2C for Arduino, but since you're using STM32, you'll need to write your own or use a HAL-based library.

【Engineer】 I see. I'll look for an STM32 I2C LCD library. Now, how do I keep the time updated? Do I need to read the RTC periodically?

【Expert】 Yes, you can read the RTC time and date every second using HAL_RTC_GetTime and HAL_RTC_GetDate. Note that you must read both, and the date read must follow the time read to unlock the shadow registers. Then format the time and display it on the LCD.

【Engineer】 Should I use an interrupt for the 1-second update, or just poll in the main loop?

【Expert】 Polling in the main loop is simpler. But if you want precise updates, you can use the RTC wakeup timer or the RTC second interrupt. For a basic clock, polling every second is fine. Just make sure your main loop doesn't have long delays.

【Engineer】 Okay. I also want to add buttons to set the time and date. How can I do that?

【Expert】 You can connect a few push buttons to GPIO pins with pull-up resistors. Use external interrupts or poll them. Implement a simple state machine: when a button is pressed, enter a setting mode, then use buttons to increment values. Save the new time to the RTC.

【Engineer】 What about backup battery? I want the time to keep running when power is off.

【Expert】 Connect a 3V coin cell (like CR2032) to the VBAT pin of the STM32. That powers the RTC domain when main power is off. Make sure to configure the RTC to use the LSE and enable the backup domain access.

【Engineer】 I'll do that. Any tips for making the clock accurate?

【Expert】 Use a high-quality 32.768 kHz crystal with low load capacitance, and place it close to the MCU. You can also calibrate the RTC using the calibration register if needed. But for most applications, the LSE with a good crystal is accurate enough.

This article was published byNing Kailiang Electronics Blog, For more knowledge about“stm32” please followNing Kailiang Electronics Blog。

Recent Articles