Pico clock with temperature
Build it step by step
0 of 7 doneWire the clock chip
VCC to 3V3(OUT), GND to GND, SDA to GP0, SCL to GP1.
Wire the display
VCC and GND, CLK to GP14, DIO to GP15.
Wire the button
GP13 to GND.
Check the backup cell
Fitted the right way round, and a type the module is made for.
Run the program
The display shows 12:00 the first time.
Press the button
Each press adds one minute.
Save as main.py
So it starts on power-up.
How do you build a pico clock with temperature?
A Pico reads a DS3231 clock chip and shows the time on a TM1637 display, then every few seconds the chip's own temperature, with a button to set the minutes. You need Raspberry Pi Pico H, DS3231 RTC module, TM1637 4-digit 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
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| DS3231 VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| DS3231 GND | Pico GND pin 38 | Common ground |
| DS3231 SDA | Pico GP0 pin 1 | I2C0 data |
| DS3231 SCL | Pico GP1 pin 2 | I2C0 clock |
| TM1637 VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| TM1637 GND | Pico GND pin 38 | Common ground |
| TM1637 CLK | Pico GP14 pin 19 | Display clock |
| TM1637 DIO | Pico GP15 pin 20 | Display data |
| Minute button | Pico GP13 pin 17 and GND | Adds one minute each press |
Code
The DS3231 is a clock chip with its own crystal and a small backup cell, so it keeps good time when the Pico is unplugged. It also has a temperature sensor inside, because the crystal's speed changes slightly with heat and the chip uses the reading to correct itself. The Pico reads the time over I2C: the chip stores seconds, minutes and hours as binary-coded decimal, where each decimal digit takes four bits, so the program converts them. The temperature is held in two registers, a whole number of degrees and a quarter-degree part. The TM1637 display is driven by the small routine in the program, which sends the four digit patterns one bit at a time. For five seconds the display shows the time with a blinking colon, and for three seconds it shows the temperature with a degree sign and the letter C. The chip's reading is of the chip itself, so it is a little above the room temperature, and it is indicative only. If the clock lost power, the program sets it to 12:00 and each button press adds one minute. Many DS3231 modules have a charging circuit meant for a rechargeable LIR2032 cell. Never put a CR2032 in such a module unless its charging resistor has been removed, because a coin cell must never be charged. Fit the cell with the plus side the right way up.
import time
from machine import Pin, I2C
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
RTC = 0x68
button = Pin(13, Pin.IN, Pin.PULL_UP)
SEGMENTS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]
DEGREE = 0x63
LETTER_C = 0x39
def bcd2dec(b):
return (b >> 4) * 10 + (b & 0x0F)
def dec2bcd(d):
return ((d // 10) << 4) | (d % 10)
class TM1637:
def __init__(self, clk, dio, brightness=3):
self.clk = Pin(clk, Pin.OUT, value=1)
self.dio = Pin(dio, Pin.OUT, value=1)
self.brightness = brightness
def _d(self):
time.sleep_us(5)
def _start(self):
self.dio.init(Pin.OUT, value=1)
self.clk(1); self._d()
self.dio(0); self._d()
self.clk(0)
def _stop(self):
self.clk(0); self.dio.init(Pin.OUT, value=0); self._d()
self.clk(1); self._d()
self.dio(1); self._d()
def _write_byte(self, b):
for i in range(8):
self.clk(0)
self.dio((b >> i) & 1)
self._d()
self.clk(1)
self._d()
self.clk(0)
self.dio.init(Pin.IN, Pin.PULL_UP) # the chip pulls the line low to acknowledge
self._d()
self.clk(1); self._d()
self.clk(0)
self.dio.init(Pin.OUT, value=0)
def show_raw(self, segs):
self._start(); self._write_byte(0x40); self._stop()
self._start(); self._write_byte(0xC0)
for s in segs:
self._write_byte(s)
self._stop()
self._start(); self._write_byte(0x88 | self.brightness); self._stop()
display = TM1637(14, 15)
def read_time():
d = i2c.readfrom_mem(RTC, 0x00, 3)
return bcd2dec(d[2] & 0x3F), bcd2dec(d[1]), bcd2dec(d[0] & 0x7F) # hours, minutes, seconds
def set_time(h, m, s):
i2c.writeto_mem(RTC, 0x00, bytes([dec2bcd(s), dec2bcd(m), dec2bcd(h)]))
def read_temperature():
d = i2c.readfrom_mem(RTC, 0x11, 2)
whole = d[0] - 256 if d[0] > 127 else d[0]
return whole + (d[1] >> 6) * 0.25
def show_time(h, m, colon):
segs = [SEGMENTS[h // 10], SEGMENTS[h % 10], SEGMENTS[m // 10], SEGMENTS[m % 10]]
if colon:
segs[1] |= 0x80 # the colon sits on the second digit
display.show_raw(segs)
def show_temperature(t):
n = int(round(t))
if n < 0:
n = 0 # this simple display shows 0 to 99 degrees
if n > 99:
n = 99
display.show_raw([SEGMENTS[n // 10], SEGMENTS[n % 10], DEGREE, LETTER_C])
status = i2c.readfrom_mem(RTC, 0x0F, 1)[0]
if status & 0x80: # the clock lost power, so its time is not trusted
set_time(12, 0, 0)
i2c.writeto_mem(RTC, 0x0F, bytes([status & 0x7F]))
CYCLE_MS = 8000 # 5 seconds of time, then 3 seconds of temperature
start = time.ticks_ms()
while True:
if button.value() == 0:
h, m, s = read_time()
m = (m + 1) % 60
if m == 0:
h = (h + 1) % 24
set_time(h, m, 0)
while button.value() == 0:
time.sleep_ms(20)
time.sleep_ms(80)
phase = time.ticks_diff(time.ticks_ms(), start) % CYCLE_MS
h, m, s = read_time()
if phase < 5000:
show_time(h, m, s % 2 == 0)
else:
show_temperature(read_temperature())
time.sleep_ms(100)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | Time with a blinking colon |
| Wait five seconds | Temperature with degree sign and C |
| Press the button | Minutes go up by one |
| Hold a finger on the chip | Temperature rises a little |
| Unplug for a minute and plug in | The time is still right |
If it does not work
OSError
Check SDA and SCL on GP0 and GP1 and that the module has power.
Display blank
Check CLK on GP14, DIO on GP15 and the power wires.
Time resets to 12:00
Fit or replace the backup cell, then set the minutes again.
Temperature is high
The chip reads a little above the room, this is normal.
Digits look wrong
Check CLK and DIO are not swapped.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico clock with temperature?
Raspberry Pi Pico H, DS3231 RTC module, TM1637 4-digit display, Tactile push button 6 mm and Breadboard 830 Tie Points.
How long does the pico clock with temperature take to build?
About 140 minutes for a intermediate maker, in 7 steps.
How do I know the pico clock with temperature works?
Start: Time with a blinking colon. Wait five seconds: Temperature with degree sign and C. Press the button: Minutes go up by one.
OSError: what should I check?
Check SDA and SCL on GP0 and GP1 and that the module has power.
Display blank: what should I check?
Check CLK on GP14, DIO on GP15 and the power wires.
Time resets to 12:00: what should I check?
Fit or replace the backup cell, then set the minutes again.
Temperature is high: what should I check?
The chip reads a little above the room, this is normal.
