Projects / Blue Pill clock with a DS3231

Blue Pill clock with a DS3231

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32

    Wire the clock and display

    SDA to PB7, SCL to PB6, power at 3.3 V.

  3. STM32

    Wire the button

    PB12 to GND.

  4. STM32DS323120:34SSD1306

    Install the libraries

    RTClib, Adafruit SSD1306 and Adafruit GFX.

  5. STM32

    Upload the sketch

    The time appears.

  6. STM32

    Press the button

    The minutes go up.

  7. STM32

    Power off and on

    The clock keeps its time.

In short

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

FromToWhy
ST-Link GNDBlue Pill GNDGround
ST-Link SWDIOBlue Pill DIODebug data
ST-Link SWCLKBlue Pill DCLKDebug clock
ST-Link 3.3VBlue Pill 3.3 pinPower from the programmer (do not also plug in USB)
DS3231 VCC and OLED VCCBlue Pill 3.3 pinPower at 3.3 V
DS3231 GND and OLED GNDBlue Pill GNDCommon ground
DS3231 SDA and OLED SDABlue Pill PB7Shared I2C data
DS3231 SCL and OLED SCLBlue Pill PB6Shared I2C clock
Set buttonBlue Pill PB12 and GNDEach 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.

CheckExpected
First startTime close to when you compiled
Press the button onceMinutes go up by one
Hold the buttonMinutes keep rising
Unplug and replug powerTime is kept by the coin cell
Chip temperatureClose 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 →

Ask Lark AI

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.