In this project, we will interface Real-Time Clock (RTC) Module DS3231 with STM32 Bluepill Microcontroller, i.e STM32F103C8 and display the Time and Date value on 0.96″ OLED Display.
Overview
In this project we will learn how to interface the RTC Module DS3231 with STM32 (STM32F103C8 Board) and 0.96″ I2C OLED Display. We will use DS3231 Real Time Clock (RTC) module to keep track of the correct time & date & display it on OLED Display using STM32F103C8 as our microcontroller.
The alternative IC for DS3231 is DS1307. The DS3231 RTC has a built-in alarm functions as well as temperature sensor with a resolution of 0.25 and an accuracy of ±3°C which make this project more easier.
You can refer to our earlier post if you wanna use DS3231 with Arduino Boards:
1. Arduino DS3231 Real Time Clock (RTC) with Temperature Monitor
2. Arduino DS3231 Real Time Clock with Alarm & Temperature
Bill of Materials
Following are the components required for making this project. All the components can be easily purchased from Amazon. The purchase link is given as well.
S.N. | Components | Quantity | Purchase Links |
---|---|---|---|
1 | STM32F103C Microcontroller | 1 | Amazon | AliExpress |
2 | RTC Module DS3231 | 1 | Amazon | AliExpress |
3 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
4 | Connecting Wires | 10 | Amazon | AliExpress |
5 | Breadboard | 1 | Amazon | AliExpress |
DS3231 RTC Module
The DS3231 is a low-cost, extremely accurate I²C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input and maintains accurate timekeeping when the main power to the device is interrupted.
The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an active-low AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided.
A precision temperature-compensated voltage reference and comparator circuit monitor the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the active-low RST pin is monitored as a pushbutton input for generating a µP reset.
Key Features:
1. Highly Accurate RTC Completely Manages All Timekeeping Functions
2. Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
3. Accuracy ±2ppm from 0°C to +40°C
4. Accuracy ±3.5ppm from -40°C to +85°C
5. Digital Temp Sensor Output: ±3°C Accuracy
6. Register for Aging Trim
7. Active-Low RST Output/Pushbutton Reset Debounce Input
8. Two Time-of-Day Alarms
9. Programmable Square-Wave Output Signal
10. Simple Serial Interface Connects to Most Microcontrollers
11. Fast (400kHz) I2C Interface
12. Battery-Backup Input for Continuous Timekeeping
13. Low Power Operation Extends Battery-Backup Run Time
14. 3.3V Operation
15. Operating Temperature Ranges: Commercial (0°C to +70°C) and Industrial (-40°C to +85°C)
16. Underwriters Laboratories® (UL) Recognized
Circuit: STM32 & DS3231 Based Real Time Clock
Here is the circuit diagram for interfacing DS3231 Module with STM32 Bluepill Microcontroller & OLED. The connection is fairly simple. You can assemble the circuit in breadboard as well.
The DS3231 & the OLED Display both are I2C Module. So we just need 2 pins for connection. So, connect the Serial Data (SDA) pins to STM32F103C8 B7 pin & Serial Clock (SCL) to STM32F103C8 B6 pin. Supply 3.3V to OLED & RTC Module through 3.3V pin of STM32F103C8. You can also use 5V supply for both DS3231 & OLED Module.
Source Code/Program
The source code for STM32 & DS3231 RTC Module is given below. The program is written on Arduino IDE. You need STM32 Boards to be installed on Arduino IDE. You can copy the code and upload directly to the STM32 board. To upload the code you can either use the Serial Method or use ST link Debugger or J-Link Module. But before that, you will need a few libraries. So download and add the libraries first.
1. Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
2. Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
3. RTClib : https://github.com/adafruit/RTClib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include "RTClib.h" RTC_DS3231 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } rtc.adjust(DateTime(__DATE__, __TIME__)); display.display(); delay(2); display.clearDisplay(); display.clearDisplay(); display.setTextColor(WHITE); //display.startscrollright(0x00, 0x0F); display.setTextSize(2); display.setCursor(0,5); display.print(" Clock "); display.display(); delay(3000); } void loop() { DateTime now = rtc.now(); display.clearDisplay(); display.setTextSize(2); display.setCursor(75,0); display.println(now.second(), DEC); display.setTextSize(2); display.setCursor(25,0); display.println(":"); display.setTextSize(2); display.setCursor(65,0); display.println(":"); display.setTextSize(2); display.setCursor(40,0); display.println(now.minute(), DEC); display.setTextSize(2); display.setCursor(0,0); display.println(now.hour(), DEC); display.setTextSize(2); display.setCursor(0,20); display.println(now.day(), DEC); display.setTextSize(2); display.setCursor(25,20); display.println("-"); display.setTextSize(2); display.setCursor(40,20); display.println(now.month(), DEC); display.setTextSize(2); display.setCursor(55,20); display.println("-"); display.setTextSize(2); display.setCursor(70,20); display.println(now.year(), DEC); display.setTextSize(2); display.setCursor(0,40); display.print(daysOfTheWeek[now.dayOfTheWeek()]); display.display(); } |
STM32 & DS3231 Based Real Time Clock
Once the code is uploaded the RTC Module will start working. The time and date will be displayed in OLED Display. There is no other setting required neither there is need of any extra buttons or switch.
1 Comment
This is not working. RTC gives Random Value. Please Help