An RTC helps in many everyday systems because it can track time. For example, in data loggers (like weather stations or temperature monitors), the RTC ensures every reading has the correct timestamp so you can analyze changes over days or months. In GPS systems, RTCs provide precise timing to synchronize signals. In servers and computers, they keep the system clock correct after shutdowns or power loss. In home automation and appliances, RTCs allow scheduled tasks such as turning lights on at night or running a washing machine at a set time. Security systems also rely on RTCs to trigger alarms or record events at exact times.
So, by adding an RTC it gives reliable timekeeping ability. In this tutorial, the STM32 communicates with the RTC over I²C to read the current time, then sends it through USART1 to the PC. Using Terminal BPP (Br@y++ Terminal), the time is displayed live and updated every second via the serial port, showing how the RTC and STM32 work together to provide accurate, real‑time clock functionality that can be used in practical applications such as data logging, alarms, or scheduling tasks.
Real-Time Clock Monitor on RT-Spark STM32F407 with DS3231 using Terminal BPPThis demonstrates how to build a simple real-time clock monitor using the STM32F407ZGT6 microcontroller and a DS3231 RTC module. The microcontroller reads the current time from the RTC via I2C and sends it to a PC through USART. On the PC side, we use Terminal v1.93b 20141030 (Terminal BPP) to display the time in real-time.
Development is carried out using STM32CubeIDE, an integrated development environment provided by STMicroelectronics. STM32CubeIDE combines project management, peripheral configuration (via STM32CubeMX), set up peripherals through STM32CubeMX, generate code, and debug programs.
Steps to Interface DS1307/DS3231 with STM32F407
1. Create a New Project: Open STM32CubeMX, then go to New Project. Select your MCU or board for example, STM32F407ZGT6, and click then Start Project to begin.
2.Configure I²C1 (DS3231)
A. In the left panel, expand Connectivity and click I2C1. Set the Mode to I2C. STM32CubeMX will automatically highlight the SDA and SCL pins. For the STM32F407, assign the pins as follows: SCL on PB6 and SDA on PB7.
B. GPIO Pin Assignment: Make sure the selected pins are configured as Alternate Function Open-Drain, with pull-up resistors enabled if you are not using external pull-ups.
C. I2C1 Configuration Window: Click I2C1, go to Parameter Settings, and configure the following: set the I2C Clock Speed to 100 kHz, use 7-bit for the Addressing Mode, and keep both Dual Address Mode and No Stretch Mode disabled.
3.ConfigureUSART1
A. Enable USART1
First, you must navigate to the Connectivity section and select USART1. Once there, set the Mode to Asynchronous. STM32CubeMX will automatically assign the default pins for this peripheral, which are PA9 for the Transmit (TX) signal and PA10 for the Receive (RX) signal.
B. USART1 Parameter Settings
Next, click on the USART1 configuration tab and then select Parameter Settings. Within this menu, you need to define the communication parameters: the Baud Rate should be set to 9600, the Word Length must be 8 Bits, and the Stop Bits should be set to 1. Furthermore, ensure the Parity is set to None and Hardware Flow Control is also set to None. Finally, confirm that the communication Mode is set to TX/RX
Connecting the DS3231 to the STM32F407ZGT6, requiring only a few pins. The VCC and GND of the module are linked to the STM32 power supply, while PB6 and PB7 are assigned to I2C1_SCL and I2C1_SDA for communication with the RTC. For serial output, PA9 and PA10 are used as USART1_TX and USART1_RX, allowing the STM32 to send time data to the PC through Terminal BPP.
The firmware begins by including the necessary HAL and standard libraries:
The DS3231 Real-Time Clock (RTC) modules are assigned the I²C address of 0x68. However, when using the Hardware Abstraction Layer (HAL) in an STM32 environment, the 7-bit device address must be left-shifted by 1 bit to accommodate the necessary Read/Write bit that the HAL function automatically inserts. Therefore, the calculation is 0X68 << 1, which results in the 8-bit address 0xD0. This 8-bit value of 0xD0 is the final device address that is used in all subsequent I²C read and write operations within the STM32 HAL functions.
The DS1307 and other similar Real-Time Clocks (RTCs) store time values in Binary-Coded Decimal (BCD) format, where a single byte holds two decimal digits (e.g., the byte 0x25 represents the number 25, not 37). The purpose of this conversion function is to take that BCD-encoded byte and convert it into a standard binary or decimal number that can be easily used in calculations or printed as a readable number.
These functions read the hour, minute, and second from RTC registers 0x02, 0x01, and 0x00. Each value is masked to remove control bits (like 12/24-hour format or CH bit). Then converted from BCD to binary using bcd2bin().
The program reads the current time from the RTC every second, formats it into a string such as "Time: 07:15:42", and sends it to the PC via UART using HAL_UART_Transmit. The HAL_Delay(1000) function ensures that the update occurs once every second.
Step4.RunningtheProgram
After building and flashing the firmware to your STM32F407 board, it's time to run the system and observe the output using Terminal v1.93b by Br@y++.
Once the STM32 is powered and the RTC is connected, the program will begin executing. Every second, it will read the current time from the RTC and send it to the terminal.
The image below shows the live time output in Terminal BPP, sent from the STM32F407 through UART after reading the DS3231 RTC. Each element of the date and time is accessed from the RTC registers and displayed together, demonstrating how the microcontroller can reliably present real‑time clock data to the PC.
Output like this:
The workflow consists of three main stages:
1.Hardware Integration – Connecting the DS3231 RTC module to the STM32F407ZGT6 board via I2C, and establishing USART communication with the PC.
2. Firmware Development (HAL-Based) – Writing embedded C code using STM32CubeIDE and HAL drivers to initialize peripherals, read time data from the RTC, and transmit it via serial communication.
3.Terminal Configuration – Setting up Terminal BPP with the correct COM port parameters to receive and display the time data in real-time. Select the correct COM port to: Set the baud rate to 9600, data bits to 8, parity to None and stop bits to 1.
CONCLUSION
The DS1307/DS3231 RTC is a small chip that keeps track of time—seconds, minutes, and hours. The STM32F407 communicates with the RTC over I²C to read the stored time values, converts them from BCD format into normal numbers, and then sends them through UART to a PC terminal so you can see the clock update every second, making it a reliable way to add accurate timekeeping.





Comments