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 this tutorial, I'm going to show you how to connect your PiicoDev real-time clock to a micro bit. We'll get the correct time set on the real-time clock and observe how it can keep time even when power is removed, and then we'll experiment with some more advanced features like setting alarms and schedules. To follow along, you'll need a Micro:bit v2, a PiicoDev real-time clock and adapter for micro bit, and a PiicoDev cable to connect everything together.

Plug your Micro:bit into the adapter, making sure the buttons are facing up. Connect the cable to the adapter and connect the other end to your real-time clock. I've just mounted everything to a PiicoDev platform to keep it nice and stable. Connect to your computer via USB. We're going to program in Thonny for this tutorial. If you need help getting started, there's a guide above and if you prefer, you can use python.MicroBit.org if you prefer to use a web-based editor.

Before we get programming, we need to download some drivers for the real-time clock. Find the download section in the article and select the correct dev board. Right click PiicoDev Unified and Save Link As. I'll save that to a PiicoDev folder in My Documents. Do the same for the next link. I've got Thonny open and I've navigated to where I saved those files. I'll connect to my micro bit, select the first file, hold down shift and select the second file, and right click and select install.

We start by importing the device driver and we also import a sleep function so that we can create a delay. Next we initialize the real time clock and call that RTC. So anytime we see RTC we're referring to the real time clock.

Next we have these properties which will allow us to set the time and date. At the time of filming it's the 18th of May 2022 and it's 4.14 in the afternoon. So I'll set the day to 18. The month is fine. The year is fine. The hour is 4 in the afternoon which is 16 in 24 hour time and the minute is 14 probably coming up on 15. There it is. I'll leave the format in 24 hour time. You could use AM or PM if you wish. And now for the weekday number. Today is a Wednesday which is the third day in the week and if we count from zero assuming zero is Monday that means that this would be a two.

I'm going to click the run current script button which will prompt me to save it to my Micro:bit and I'll just save it as main.py. And there we have it. In the shell we have the time scrolling up. Let's just stop the script and take a closer look. So we have a couple of lines. The first line says the time is 16.15 which is 4.15 PM and then a line saying today is Wednesday. And then we just have these timestamps being printed. It looks like once every second. This is a standard format of year, month, day and then hour, minute and seconds. After we set these date time properties we call the RTC.sync() function which will sync the time and date to the micro bit.

The Set Date Time method takes all of the properties and commits them onto the clock. We can then use the Get Date Time method which does the opposite, taking the current date time on the clock and loading it into the properties so that we can access them. We call Get Date Time and then do our print statements. The first line says "The time is 16.15" and that is this first print statement here where we have the time is. We can access the properties individually if we want to, and the same can be done with the date or the weekday. The next line prints "Today is" and then uses a new property called Weekday Name. This is the text name for the current weekday, assuming that zero is Monday.

It is worth noting that any time we want to access the current time, we have to call the Get Date Time function. This queries the clock for the current date and time. If we don't call this, then when we print the time we would probably be seeing some old values.

Next up is the infinite loop which calls a new method called Timestamp. This automatically calls Get Date Time and formats the result in a formatted date time string with year, month, day, hours, minutes, and seconds. This can be useful for writing to a data logger or creating graphs.

The PiicoDev RTC does have power backup that should be able to keep accurate time during a power cycle. This is useful for battery projects that may only be powered periodically.
every minute.

We have the running on backup power section and I'll copy that example into Thonny. I'll just replace everything in main and save that. This is basically a stripped down version of the previous example. We're just printing the timestamp here. We don't want to run the previous script again because if we did it would reset the time back to those parameters that we programmed in. Now the clock has the time and we just want it to run. So if I run this script with ctrl R we can see the time is still accurate. It's 1620 and counting. And now if I unplug my micro bit, I'll wait a couple of seconds and then plug it back in. I'll connect and reboot with ctrl D. You can see we still have the accurate date and time. If the supercapacitor were completely flat then we would be seeing the default date and time which would read the 1st of January 2000. If you started with a completely flat supercapacitor, you would need to be powered for at least a few minutes before you could reliably handle a power outage.

Now for a more advanced example, it's possible to program an alarm onto the RTC. This could be used for like a simple alarm that gets you out of bed in the morning all the way up to more complex scheduling where you could schedule an action to occur say on the first day of the month every month. Head to the advanced example section and take a look at alarms and scheduling. We'll copy this example and paste it into Thonny. I'll just paste over everything in main. And this should look pretty similar at the start. We have the same imports and the same initialization. We're assuming that we already have accurate time running on the clock. And then the next action is to call alarm setup and this is where we program when we want the alarm to trigger. In this case, we're passing in the parameter minutes equals zero. So that would mean to trigger once every minute.

We can set up an alarm to trigger every hour at the start of the hour. I can see that we're currently 425 and 13 seconds. So I'll set this to the next minute which is 26. I'll run that script and now we're waiting for the alarm event.

Let's take a look at the rest of the code. After we call the setup for the alarm to trigger on the 26th minute, there's an infinite loop and all we're doing here is printing the timestamp as usual and then calling the check alarm function. This checks whether we've crossed the next alarm time. If we do cross the time, we'll print the message "alarm triggered" and then we'll carry on with the rest of the script.

And we've just crossed the minute into 26 and you can see we have the "alarm triggered" condition. Now it's worth noting that the alarm will only trigger once. You can see here the alarm triggered the first time that the alarm condition was met and that condition was for minute number 26 and that's when our alarm triggers. The minute is still 26 a couple of seconds later but it's only that first time we cross the alarm condition that we get a trigger.

This is really useful because it means we can keep checking the alarm but we're not going to run the same action for every time we check it within that minute. Now you can get much more sophisticated with this. If we comment out that alarm setup and uncomment the next one, we can see we're passing in week day 2, hours 13 and minutes 23. So what this means is trigger on the third day of the week, say Wednesday, we're counting from zero remember, trigger every Wednesday at 23 minutes past 1 in the afternoon. This is in 24 hour time. This is a once per week action that will trigger on Wednesday at 1.23 in the afternoon. We can also use date as a parameter so we can pass in date 1 and this will trigger on the first day of the month.

If we're checking alarm quite frequently, it'll probably be more efficient to use the date parameter.

It's time to take a look at the PiicoDev Real-Time Clock (RTC). This device is great for basic timekeeping, keeping time during a power outage, and even for some pretty advanced scheduling. With this starter project, you can make a timer-controlled light or an alarm.

The PiicoDev RTC is easy to use. All you have to do is set the time and date, and you can schedule actions to happen at some point in the future. This could be anything from a light turning on to a notification being sent. It can even happen pretty close to midnight.

If you have any questions about the PiicoDev RTC, please do let us know on the forums. We're 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.