Interface DS3231 Precision RTC Module with STM32 Blue Pill

This STM32 tutorial shows how to build a real time clock using STM32 Blue Pill board and DS3231 RTC module where time & date are printed on SSD1306 OLED display with 128×64 pixel resolution.
Two push buttons connected to the STM32 Blue Pill board used to set time and date.

To see how to interface the STM32 Blue Pill board with SSD1306 OLED display, go to this post:
Interface SSD1306 OLED Display with STM32 Blue Pill Board

Abbreviations:
OLED: Organic light Emitting Diode.
I2C: Inter-Integrated Circuit.
TWI: Tow Wire Interface.
SPI: Serial Peripheral Interface.
IoT: Internet of things.
RTC: Real Time Clock.

STM32F103C8T6 with DS3231 RTC module and SSD1306 OLED display

The DS3231 RTC Module:
The DS3231 is an extremely accurate real time clock (RTC) chip provides timekeeping in hours, minutes, and seconds, as well as day, month, and year. The high accuracy of the DS3231 RTC is maintained by a built-in temperature-compensated crystal oscillator (TCXO).

Benefits and Features of the DS3231 Module:
Here are some key features of the DS3231 RTC module.

  1. High Accuracy: The integrated temperature-compensated crystal oscillator (TCXO) reduces time drift, achieving an accuracy of ±2ppm from 0°C to +40°C, and ±3.5ppm from -40°C to +85°C.
  2. I²C Communication: Communicates with master device (microcontrollers, development boards …) via the I²C (TWI) serial interface protocol with support of Fast (400kHz) I2C Interface.
  3. Timekeeping Registers: Real time clock counts Seconds, Minutes, Hours, Day of the Week, Date of the Month, Month, and Year with automatic Leap-Year compensation valid up to 2100.
  4. Battery Backup: Includes a slot for a CR2032 coin cell battery for continuous timekeeping during power outages.
  5. Alarm Functionality: Supports two programmable alarms with interrupt signal output.
  6. Built-in Temperature Sensor: Measures chip temperature with an accuracy of ±3°C and a resolution of 0.25°C.
  7. Square Wave Output: Provides a programmable square wave output signal.
  8. EEPROM (Commonly AT24C32 or AT24C64): The DS3231 RTC module with an integrated EEPROM adds non-volatile storage to the already powerful real-time clock functionalities of the DS3231 chip with 4kB or 8kB memory capacity. It communicates also via I²C and shares the SDA and SCL lines with the RTC chip.

The DS3231 RTC module used in this project is the one shown below:

DS3231 RTC board module

DS3231 RTC Module Pinout:
Generally, the DS3231 RTC module comes with a set of pins, depending on the module, for connecting to master device. Below is a detailed description of each pin.

  • 32K: Outputs a clock signal with frequency of 32.768 kHz clock signal, it’s an open drain output and requires a pull-up resistor.
  • INT/SQW: This pin provides an active-low interrupt signal or square wave with programmable frequency of 1 Hz, 4 kHz, 8 kHz, or 32 kHz. This pin is an open drain output and requires a pull-up resistor.
  • SCL: Serial Clock Line for the I²C communication. Connected to the SCL pin of the master device.
  • SDA: Serial Data Line for the I²C communication. Connected to the SDA pin of the master device.
  • VCC: Power supply for the module. Typically connected to +3.3V to +5V.
  • GND: Ground pin of the module. Connected to circuit ground (0V).

The DS3231 RTC board uses I2C interface for the communication with the microcontroller. The default I2C address for the DS3231 chip is 0x68 (7-bit address).

The integrated EEPROM chip has a configurable I2C address with a default address of 0x57.
The three jumpers A0, A1, and A2 are used to change the I2C address of the EEPROM from the default one according to the following combination (The R/W bit not shown):

1010A2A1A0

For example, if the three jumpers left open then the I2C address is 0x57 (default). If the three jumpers are closed then the I2C address becomes 0x50. Closing any jumper changes its state from 1 to 0.

Interfacing DS3231 Precision RTC Module with STM32 Blue Pill Board:
The circuit schematic diagram below shows the wiring of the STM32 board with DS3231 RTC module and SSD1306 OLED display.
The SSD1306 OLED is configured to work in I2C mode and it shares the same I2C bus with the DS3231 RTC.

In this project hardware I2C2 (2nd hardware module) interface is used with SDA line mapped at pin PB11 and SCL line mapped at pin PB10. Both the DS3231 RTC chip and the SSD1306 OLED display are slave devices, whereas the STM32 Blue Pill microcontroller, STM32F103C8T6, is the master device.

The good thing in this circuit is that there are two push buttons, Button1 and Button2, used to set time & date of the real time clock. These push buttons are respectively connected to the STM32 Blue Pill board pins B4 and B5.

Interfacing STM32 Blue Pill with DS3231 RTC and SSD1306 OLED

Hardware Required:
This is a summary of the parts required to build this project.

Interfacing DS3231 Precision RTC Module with STM32 Blue Pill Board Code:
Arduino IDE (Integrated Development Environment) is used to write project code, the STM32 Blue Pill board has to be added to the IDE before compiling the code.
The STM32 Blue Pill board can be installed using Arduino IDE Boards Manager.

Programming the STM32F103C8T6 microcontroller is done with the FT232RL USB to serial UART converter, Arduino IDE supports the ST-LINK V2 programmer as well.

To be able to compile project Arduino code, three libraries from Adafruit Industries are required:
The first library is a driver for the SSD1306 OLED display and it can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries…, in the search box write “ssd1306” and install the one published by Adafruit).
The second library is Adafruit graphics library which can be installed from Arduino IDE library manager.
The last library is Adafruit RTC library and it is a library for the DS3231 RTC. It can be installed also from Arduino IDE library manager.
During the installation of any of the mentioned libraries, Arduino IDE may ask for installing some other libraries form Adafruit Industries (dependencies).

Project code was tested with the following library versions:
Adafruit GFX Library: Version 1.11.11.
Adafruit SSD1306 OLED Display Library: Version 2.5.13.
Adafruit RTC Library: Version 2.1.4.
Adafruit BusIO: Version 1.17.0.

Programming Hints:
The used libraries are included in the Arduino code as shown below:

I2C2 Hardware peripheral is used for I2C protocol communication and it is initialized in the software as shown below:

The SSD1306 OLED display library is initialized as shown below. The display is connected to the previously initialized I2C (Wire) interface with address of 0x3C, if the display does not work then try with slave address 0x3D.
-1 Is passed as there is no reset line connected between the display and the microcontroller.

Screen width and height is previously defined in the code as 128 and 64, respectively.

The push buttons which are used to set time & date of the real time clock, they’re connected to the STM32 Blue Pill board B4 and B5 and are defined in the code as:

These push buttons are configured as inputs with internal pull-up enabled so that no external pull-up resistors are required.

Rest of code is described through comments.

Project Arduino Code:

Interfacing DS3231 Precision RTC Module with STM32 Blue Pill Video:
The video below shows my protoboard circuit of the interfacing of the STM32 Blue Pill board with DS3231 RTC module and SSD1306 OLED display.


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top