Blue Pill clock with a DS3231
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the clock and display
SDA to PB7, SCL to PB6, power at 3.3 V.
Wire the button
PB12 to GND.
Install the libraries
RTClib, Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The time appears.
Press the button
The minutes go up.
Power off and on
The clock keeps its time.
How do you build a blue Pill clock with a DS3231?
A Blue Pill reads a DS3231 clock chip over I2C and shows the time and date on an OLED, with one button to set the minutes. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DS3231 RTC module, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.
- Level
- Intermediate
- Time
- About 140 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Blue Pill GND | Ground |
| ST-Link SWDIO | Blue Pill DIO | Debug data |
| ST-Link SWCLK | Blue Pill DCLK | Debug clock |
| ST-Link 3.3V | Blue Pill 3.3 pin | Power from the programmer (do not also plug in USB) |
| DS3231 VCC and OLED VCC | Blue Pill 3.3 pin | Power at 3.3 V |
| DS3231 GND and OLED GND | Blue Pill GND | Common ground |
| DS3231 SDA and OLED SDA | Blue Pill PB7 | Shared I2C data |
| DS3231 SCL and OLED SCL | Blue Pill PB6 | Shared I2C clock |
| Set button | Blue Pill PB12 and GND | Each press adds a minute |
Code
The DS3231 is a clock chip with its own crystal and a temperature-compensated oscillator, so it keeps time well and carries on from a coin cell when the main power is off. It sits on an I2C bus with its own address, 0x68, and the OLED sits on the same two wires at 0x3C. I2C works because each device listens for its own address, so two chips can share one data and one clock line. The Blue Pill's first I2C port uses PB7 for data and PB6 for clock, and the module's pull-up resistors hold the lines high. The chip stores the time as binary-coded decimal in its registers, which the RTClib library converts for you. On a first start the sketch loads the compile time, then the button adds a minute at a time. The coin cell must be fitted the right way round. If your module has a charging circuit built for a rechargeable cell, use the cell type it was made for, and never try to recharge a standard coin cell. Everything here runs at 3.3 V.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
RTC_DS3231 rtc;
const int BTN = PB12;
unsigned long lastDraw = 0, lastStep = 0;
void setup() {
pinMode(BTN, INPUT_PULLUP);
Wire.begin(); // I2C1 uses PB7 for SDA and PB6 for SCL
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
if (!rtc.begin()) { while (true) {} }
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // start from the compile time
}
}
void loop() {
if (digitalRead(BTN) == LOW && millis() - lastStep > 250) {
lastStep = millis(); // press or hold to add a minute
rtc.adjust(rtc.now() + TimeSpan(60));
}
if (millis() - lastDraw >= 200) {
lastDraw = millis();
DateTime t = rtc.now();
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(3);
oled.setCursor(4, 8);
if (t.hour() < 10) oled.print('0');
oled.print(t.hour()); oled.print(':');
if (t.minute() < 10) oled.print('0');
oled.print(t.minute());
oled.setTextSize(1);
oled.setCursor(4, 44);
oled.print(t.day()); oled.print('/'); oled.print(t.month()); oled.print('/'); oled.print(t.year());
oled.setCursor(4, 56);
oled.print("Chip temp "); oled.print(rtc.getTemperature(), 1); oled.print(" C");
oled.display();
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| First start | Time close to when you compiled |
| Press the button once | Minutes go up by one |
| Hold the button | Minutes keep rising |
| Unplug and replug power | Time is kept by the coin cell |
| Chip temperature | Close to the room temperature |
If it does not work
Blank screen
Check the OLED address and the 3.3 V supply.
Stuck at start
Check SDA and SCL on PB7 and PB6 and the module address 0x68.
Time resets at power loss
Check the coin cell and its polarity.
Time is wrong
Press the button to set it, or re-upload to reload the compile time.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill clock with a DS3231?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DS3231 RTC module, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.
How long does the blue Pill clock with a DS3231 take to build?
About 140 minutes for a intermediate maker, in 7 steps.
How do I know the blue Pill clock with a DS3231 works?
First start: Time close to when you compiled. Press the button once: Minutes go up by one. Hold the button: Minutes keep rising.
Blank screen: what should I check?
Check the OLED address and the 3.3 V supply.
Stuck at start: what should I check?
Check SDA and SCL on PB7 and PB6 and the module address 0x68.
Time resets at power loss: what should I check?
Check the coin cell and its polarity.
Time is wrong: what should I check?
Press the button to set it, or re-upload to reload the compile time.
