This guide will help you get started with a PiicoDev Real Time Clock. We'll set and read the time/date, observe the power-backup capability, and configure the RTC with an alarm.

Transcript

A real-time clock (RTC) is a device that accurately tracks the passage of time, making it a staple for timekeeping projects or projects that need to do some kind of scheduling. The PiicoDev RTC loses less than a second per year and features supercapacitor backup, meaning it can tolerate power outages of about a week. When connected to power, the supercapacitor charges and if power is removed, the supercapacitor will keep the clock running so that it doesn't lose the time.

In order to connect a PiicoDev RTC to a Raspberry Pi Pico, you will need a Raspberry Pi Pico with pins soldered facing down, a PiicoDev real-time clock and expansion for Pico, and a PiicoDev cable to connect everything together. Plug your Pico into the expansion board, making sure that the USB connector is on the same side as the 2-pin battery connector. Plug the PiicoDev cable into the bottom and connect the other end to your real-time clock. To keep it nice and stable, you can mount your hardware onto a PiicoDev platform and connect to your computer with USB.

Next, head to the download section in the article and download the source files. You will need the PiicoDev unified library and the device module PiicoDev RV3028. Save these to a PiicoDev directory in your documents. Open Thonny, navigate to where you downloaded your files, and connect to your Pico. Select both files, right click and upload to your Pico.

To set and read the time and date, find the first example in the article and copy the example.

We first import the device driver and a sleep function to create a delay, and then we initialize the RTC and store that as an object called RTC. So every time you see RTC we're referring to this clock. And now we can set the time and date. It is currently the 18th of May and it's 3.09 in the afternoon. I'll change day to 18. May is 5. And it's 3.09. That's 15.09. We can leave seconds as zero, that's fine. I'm going to leave the clock in 24 hour mode. We could use AM or PM here as well. And today is a Wednesday, which is the third day in the week, but that is two because we count from zero in the RTC. So assuming that Monday is zero, Wednesday will be two.

Next up we're going to call setDateTime, which will commit those values to the clock. Let's give the script a run. I'll click run, which will save and run the script. And you can see in the shell we have some information printing. I'll just press control C to stop that. And let's have a look at what we got. We have a message here saying the time is 15.09 and today is Wednesday. And then what follows looks like a bunch of timestamps in a standard Unix time format where we have year, month, day, and then hour, minute, second.

Going back into the code, we can see once we have set the date and the time, the very next thing we do is get the date and the time. That reads the date time variables off the real time clock and stores them back in these properties day, month, year, hour, minute, second. And then we can do whatever we like with that information. Here we have a print the time is, and then we have

We can also set alarms and schedule events. In this example, we'll set an alarm to go off every day at 3:18 PM. First, we'll convert the hours and minutes to strings. The first line explains why there is no leading zero in front of the nine, as we are just printing the minute which has the value nine, not zero nine. On the next line, we have "Today is Wednesday", which is taken care of by the print statement. We can access information about the weekday numerically (0, 1, 2, all the way to six) and also have a property called "weekday name" which will return a string of the weekday for nice prints.

We then have an infinite loop which is calling the timestamp method that returns a string. This is what is printing in the shell as a formatted string every second. To test the power backup capability, we'll grab a small example and replace everything in main with this code. This is a stripped down version of the previous example that only reads the time. We don't want to set the time here, as we should only have to do that once. After a power cycle, we can read the time and make sure that it still makes sense. After running the script, we can see that the time is accurate. After unplugging the Pico, waiting for a while, and plugging it back in, we can see that it has kept the time.

For one last example, we'll head down to the advanced section and look at alarms and scheduling. Not only can we program the date and time onto a real time clock, but we can also set alarms and schedule events. In this example, we'll set an alarm to go off every day at 3:18 PM.

We can give it a desired date and time for when we want to trigger an alarm. This can be useful for just a simple alarm, like you would set in the morning to get out of bed, but it's also useful for periodic scheduling. Go to the alarms and scheduling section, grab that example code and copy it into Thonny. In the main section, we have our normal imports and initialization. This time we're calling the alarm setup method, and it's the arguments that we pass into this method that determine when the alarm is going to be triggered.

I set the minutes to 22 and then ran the script. We can see we have the timestamp being printed here. It's only printing once every two seconds, and we'll get back to why that is. We crossed the moment that I set the alarm for and the alarm was triggered. Let's take a closer look at the code. In the infinite loop, we are printing the timestamp as we've already seen. This time we're calling RTC check alarm. This method checks if we've crossed the alarm threshold. It'll return false if we haven't crossed that threshold, and it'll return true the first time we do. That means that you won't get subsequent alarms, for example, if you check multiple times in the same minute. We only saw the message alarm triggered once, the first time that we called check alarm after the alarm had gone off.

Triggering an alarm every hour is easy. To do this, we comment out the first line with alt 3 and uncomment the next one with alt 4. This one has a few more arguments. For example, we can set it to trigger on Wednesday at 1:23 pm every week. We can also include a date as one of the arguments, so it will trigger on the first day of every month. The scheduled action can be anything we want it to be, such as a print statement.

Using the PiicoDev real-time clock, we can make a clock, an alarm clock, schedule actions for our remote project, and create a data logger with an accurate timestamp. If we need more functionality, there are advanced examples in the tutorial.

If you have any questions about this content, please let us know on the forums. We are full-time makers and happy to help. Until next time, happy making!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.