Pi Pico Real Time Clock DS3231 - Workout

by tonygo2 in Circuits > Microcontrollers

9862 Views, 9 Favorites, 0 Comments

Pi Pico Real Time Clock DS3231 - Workout

CLOCK-3.jpg

This is a relatively simple project which combines the Waveshare DS3231 RTC, a Raspberry Pi Pico and an Waveshare Pico 1.8" 160x128 pixel display. I've linked them up in the simplest way with a Pimoroni Pico Decker but you could join them together with cables or some strip board.

You will need to have installed Thonny on your computer and have a micro USB cable to power and program the Pico.

I've used this display with a Pico in a previous Instructable and this project builds on the previous Display Workout with some improvements and new code.

The Waveshare RTC is based on the DS3231 Chip which is an extremely accurate device with temperature compensation to keep it accurate and has the following features:

Highly Accurate RTC Completely Manages All Timekeeping Functions

• 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

• Accuracy ±2ppm from 0°C to +40°C

• Accuracy ±3.5ppm from -40°C to +85°C

• Digital Temp Sensor Output: ±3°C Accuracy

• Register for Aging Trim

• RST Output/Pushbutton Reset Debounce Input

• Two Time-of-Day Alarms

• Programmable Square-Wave Output Signal

This project uses the accurate RTC and temperature functions but does not cover alarms and square wave output.

Connecting Up the Devices

CLOCK-20.jpg

This was very simple; I just pushed the three boards onto the Pimoroni Pico Decker and plugged in the USB cable.

CLK Pinout.jpg

If you do not have a Pico Decker, or similar board use the connections shown above.

Identifying the Pins

Pico Pinout 300 dpi.jpg

To make things easier I scanned in an image of the Pico PinOut at 300 dpi. Cropped the strips as shown above and then printed labels to glue onto the sockets of the RTC. This worked very will and makes it very easy to identify the pins to use.

Documentation

ds3231 code.jpg

Waveshare provide links to a great deal of information and guidance and you can access it here:

https://waveshare.com/wiki/Pico-RTC-DS3231

The most useful items are the example MicroPython program and the Datasheet in the Resources tab. The datasheet is quite short and is an interesting read.

One thing to note is that the values we are getting from most of the registers are in Binary Coded Decimal (BCD), not integers. I've not used this format several decades! (A byte is split into 2 nibbles with each representing a denary digit with its binary equivalent. 1001 0011 represents 93 in base 10: = 8 + 1 and 2 + 1. In 'normal binary' 93 base 10 would be 01011101.)

The original program just printed a long string with the date and time:

2021/09/09 16:33:48 Thursday

It demonstrates the method but did not return any values. I've added code to supply each of the individual parts, which can easily be converted to integers if needed.

I've also added code to extract the temperature from the chip.

Setting the date and time:

Look carefully at line 131:

131 #rtc.set_time('02:21:20,Wednesday,2021-08-25') # 'hr:min:sec,weekday,year-month-day'

The leading zeros are essential!

You need to edit this line. Remove the # and keeping the spacing just as it is, edit it to the current date and the time about 15 seconds ahead. About 2 seconds before the time you have set, start the program running. This sets the time in the RTC and should only need doing once as the battery keeps the RTC running. Immediately comment out line 131 and resave the program. (If you leave it it will reset the RTC to the wrong/same time if you run it again.)

(You need to start running the program early because it takes a finite time for Thonny to sent the program to the Pico, import the libraries, start execution and get to the time setting command.)

Check the time being displayed is close to the correct time.

Downloads

​Looking at the Project

Raspberry Pi Pico Real Time Clock

The video shows how the project works. On running the program the Pico waits until the second value changes in the the RTC. It then extracts the full date, time and temperature values and displays them on the screen. Two type faces are used, the built in Framebuf font and Les Wright's defined font in different sizes. The full code is available below.

Improvement to Colour Setting Routine

CLKcode colour2 190-218.jpg

My first try at this, colour(R,G,B) worked properly but looks pretty terrible. I've done some more thinking and produced a neater looking version, colour2(R,G,B). Either work and should produce the same outcomes

Centred Text

CLKcode 373-389 centre string.jpg

As the number of letters in the weekdays and month names vary in length we need a routine to centre these lines.

RTC Code From WaveShare - Part 1

CLKcode 401-441.jpg

This should the start of the Waveshare code, I2C and register setup, sett a new RTC time and reading the current time.

RTC Code From WaveShare - Part 2

CLKcode441-483.jpg

Setting up and alarm - which I have not yet used and my code to read the temperature from 2 registers with INTEGER value not BCD!

The MAIN Loop

CLKcode 484-end.jpg

Reading the values and sending them to the display screen.