Since the Raspberry Pi Pico is still quite new to me and I need to refresh my knowledge of Python, I always use small "standard projects" to help me get to grips with new hardware or software. You have already seen this in my last blog Pico as a weather station and I would like to continue here by building an analogue clock using a RealtTimeClock and an OLED display.
I split the blog into two parts:
- One RealtTimeClock type DS3231 on the Pico Commissioning
- Show the time of the RealTimeClock on OLED display
The clock should be able to simultaneously distinguish between summer and winter time. An RTC component is required, as the Raspberry Pi Pico does not have an internal RTC.
Hardware and software required
The hardware for this experimental setup is simple, see Table 1.
|1||Raspberry Pi Pico|
|1||0.96 inch OLED display I2C SSD1306|
|1||Breadboard and Jump Wire|
|1||Real Time Clock RTC DS3231 I2C Real Time Clock
Alternatively: DS1302 RTC
Table 1: Hardware parts for analogue clock
For the software, since it will be a programme with MicroPython, use Thonny Python IDE, which is already available with the Raspbian image and can be installed for all common operating systems.
In addition, once the Thonny Python IDE is installed and the Pico is connected, you need the urtc and microypthon-oled libraries, which are then transferred to the Pico. You can also find out how to install libraries in the article on the Pico weather station.
Putting the RTC DS3231 into operation
Once you get a new RTC DS3231 from our shop or if the RTC battery should have become empty, then the RTC DS3231 needs to be reset. This is covered in our first part of this blog. To do this, you first need to wire the Raspberry Pi Pico to the RCT DS3231, see Figure 1.
Figure 1: Wiring RTC DS3231 with Pico
The whole setup basically requires four wires and the two previously mentioned components. Then, if you already have Thonny Python IDE installed, connect the Raspberry Pi Pico to the PC or the Raspberry Pi and download the required libraries. At this point I assume that the firmware is present on the Pico. Then download code 1 to the Pico. You can read how to install the firmware in this Quickstart Guide.
Code 1: Setting the RTC DS3231
To ensure that the RTC DS3231 is also set, you must delete the preceding """ in the line """#SET TIME FROM RTC and """#End settime and enter a new date and time. Weekday is a little confusing at this point because is used here the American time format and the week starts with the Sunday day. Therefore, Sunday is 1 and Wednesday is 4 in the example. When the script is executed, the RTC DS3231 is reset. Immediately afterwards, you will see the current time and date of the RTC every second in the command line, see Figure 2.
Figure 2: Current date and time of the RTC DS3231
Start the Pico again, then in the current case the time will be reset again, so you should insert the """ again in the appropriate places and let the code upload.
As long as the button cell is inserted in the RTC DS 3231 and is not empty, the clock will continue to run.
Analogue clock with OLED and RTC DS3231
Here comes the real project, the analogue clock on the OLED display with all the other time information. The wiring, since basically only one more component has been added, has not become much more complicated, see figure 3.
Figure 3: Wiring OLED display and RTC DS3231 with Pico
Since the RTC DS3231 and the OLED display both communicate with i2c but have different addresses, they can be put on one bus line. Once the wiring work is done, you can transfer code 2 to the pico.
Code 2: Analogue clock with OLED and RTC DS3231
I would like to explain this source code briefly in a few points. First, I added a diag flag to my source code bPrintDiag. Since the complete time is printed every second in the terminal, the pico runs a bit sluggish with it. Since the diag outputs are only needed for debugging, I made them switchable on and off. This is followed by the definition of the function Calc_Summer_Winter_Time, which checks whether it is winter or summer time. This returns the shift of the hour, which is used in the while loop as a correction to the RTC DS3231 hour. In addition, to keep the code clearer, I have outsourced the drawing of the analogue clock and the writing of the text on the OLED display to the functions Draw_Clock and Print_Date_Time. The first function is only called if there has been a change in the minutes, the latter if there has been a change in the seconds.
Draw_Clock includes the mathematics to draw the hands and the clock face. Since I am a fan of everything working exactly like a real analogue clock, the behaviour of the hour hand is also correctly reproduced. In the initial source code, the hour hand was rigid on the current hour. This is not the case with a real analogue clock, but moves bit by bit to the next hour. I realised this by calculating the angle for the hour hand with the current minute. Since some drawing functions are missing in the standard library for the OLED, I use the GFX library for drawing. This always calls the function from the pixel of oled object for drawing. The definition can be found at the beginning of the source code, see behind the comment #Definitions and init for OLED-Display.
Print_Date_Time then writes both the time and the date to the right side of the OLED display. As already mentioned, this is done every second. Nevertheless, there is still a small gimmick. The default font of the OLED is quite large, so there is the object font, which is created at the beginning of the source code. This loads the font ubuntu_mono_12, which is supplied in the library microypthon-oled.
The While loop at the end of the source code then does the rest. It takes over the corresponding checks. You can see the result in Figure 4.
Figure 4: The current date and time with the Pico
The Pico is still quite new in our range, so many "old" projects can be rebuilt. Especially in terms of microcontrollers for little money, the Pico is a real alternative to a Nano V3.0 or microcontroller board ATmega328. The difference is "only" 2 euros, but the Raspberry Pi Pico is in no way inferior to the original.
Personally, I am more and more enthusiastic about the Pico, not least because of the MicroPython scripting language. The libraries for the Pico are not yet as extensive as those for the Arduino IDE, but you have to bear in mind that the Raspberry Pi Pico was only introduced on 21 January 2021 and it took a long time for the hardware to be distributed worldwide. The Nano V3.0 or the ATmega328 microcontroller board have been on the market much longer and accordingly more has already happened in the area of library development.
To personalise the project even further, you could, for example, add a second hand on the clock face, but this will also have to be redrawn every second. You can also further modify the hands with a bit of mathematics, as you wish.
You can find more projects for Az-Delivery by me at https://github.com/M3taKn1ght/Blog-Repo