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.


A real-time clock or rtc is a device that accurately tracks the passage of time a staple for time keeping projects or projects that need to do some kind of scheduling the pikadev rtc loses less than a second per year and features supercapacitor backup that means that it can tolerate power outages of about a week when connected to power the supercapacitor charges and if power is removed that super capacitor will keep the clock running so that it doesn't lose the time I'm going to show you how to get started with your picodev real-time clock and a Raspberry Pi we'll get these together and get some example code working so that we can set and read the time we'll also look at how to set an alarm or create a schedule for an event to happen at some point in the future let's get started to follow along you'll need a Raspberry Pi setup to use like a desktop computer we have a guide for that as well if you need some help you'll need a PiicoDev real-time clock and adapter for Raspberry Pi and a PiicoDev cable 100 millimeters or longer are best for Raspberry Pi projects connect your pikadev adapter to your Raspberry Pi making sure that the ethernet arrow points towards the ethernet connector on a Raspberry Pi 4. If you're working with a Raspberry Pi 3 the ethernet and USB sockets are swapped connect your pikadev cable to one of the sockets on the adapter and connect the other end to your real-time clock and I've mounted everything to a picodev platform to keep it nice and stable connect a keyboard and mouse make sure you have an internet connection I'll use a physical ethernet port today connect to your monitor and finally power up my Pi is booted up and I need to check two things before I can begin working with picodev first go to preferences Raspberry Pi configuration and make sure that the I squared c is enabled in the interfaces tab here it is and that is enabled next go to programming and funny we need to make sure that the picodev package is installed go to tools manage packages and search for picodev that's with two eyes here it is and I will install it install or upgrade as necessary we're ready to begin programming in the article for this tutorial find the set and read time and date example and copy all of that code into thoni I'll click drag ctrl c for copy and I'll paste that into my untitled script in thumi and I'll save this as set read time dot py you can choose any name you like let's take a quick look at what's going on before we run we import the device driver and a sleep function so we can create a delay next we initialize the PiicoDev real-time clock and call it rtc so anytime you see rtc in this script we're referring to this real-time clock next up we're setting the date and time properties for the real-time clock you can see here there's a day a month a year property hour minutes seconds and a few others at the time of filming it is thursday the 19th of may 2022 and it's about 1 30 in the afternoon it is so I'll set day to 19 5 is correct for may and the hour is 13 and the minutes are at the moment 34. We're probably coming up on 35. I'll leave the seconds as zero and you can see we have an option to work in 24 hour time like I am here or we can use am and pm if we like the weekday is thursday which is the fourth day but we count days from zero so I'm going to set this to three now that we have these properties set up we call the set date time function which will commit this information to the real-time clock there's some other stuff going on but let's just give this script a run okay and we can see in the shell we have some activity I'll stop the script and we'll take a look at what's going on when we run our script the first thing we see is a message saying the time is 13 35 and at the time of running that's correct we also have a line saying today is thursday after a gap we then get this repeating timestamp coming across the screen and you can see that's being printed once per second so we have this timestamp being printed in year month day hour minutes seconds picking up where we left off in the script we set the date and time and then immediately after we get the date and time and that is the opposite of this action this is where the Raspberry Pi is querying the time from the clock and loading it into these properties so now in the next print statement we can say the time is and we print rtc hour which gives us that 13. So we've pulled the time off the clock and now we're displaying it from the same property that we were able to set it with and likewise we print a colon and rtc.minute which is here the next print statement we see a new property here we have weekday name weekday name assumes that we count zero as monday and so we can just say today is and if we print weekday name then the line that results is today is thursday for these two print statements to make sense and be current they need to be preceded by a call to getdatetime it is at that moment that all the information is pulled off the real-time clock and loaded into our script if there was a long delay between the last get date time and when you actually use that information then you could be out of sync finally we have an infinite loop that is running and all that is doing is printing something called rtc.timestamp that's what's printing this message here and timestamp is special every time timestamp is called it will automatically execute getdatetime and so this infinite loop will just print once per second printing that timestamp in a nice formatted string which can be really useful for data logging or for creating nice graphs now I mentioned that the PiicoDev real time clock has a super capacitive backup that means that power can be removed and the time will still be retained let's have a look at how that works back in the article find the section for running on backup power and copy that example copy that with ctrl c and I'll open a new script in funny and paste that in I'm going to save this as read time dot py now this is just a strip back version of our previous script the reason we're doing this is because we don't want to be calling set date time again that's going to override the time on the clock with the time that is now old so when I call read time we just get rtc.timestamp over and over like we did at the end of the first script and you can see the time according to the clock is 1 39 in the afternoon I'm going to stop the script and unplug my picodev real-time clock you could reboot your Pi during this time if you want as well I'm going to wait a few seconds and then plug it back in so we completely removed external power from the real-time clock now it's powered back up and if I run the script again the time is still correct so we've proven that our clock has a working power backup the supercapacitor is set up to charge every time we initialize the real-time clock that means that if you're running from a completely flat super capacitor it would need to be powered for at least a few minutes before it's capable of handling short power outages let's take a look at using the built-in alarm and scheduling feature of the real-time clock find the advanced example section and look for the alarms and scheduling example we're going to copy all of this code and paste it into a new script just like before I'll make a new script paste that in and save this as alarms are how we can tell the clock to look at for a specific time so that we don't have to constantly check for that time ourselves we initialize the real-time clock just like we have before and then the next method is alarm setup and here we're setting minutes equals zero what that means is we're telling the clock to check for the next instance that minutes is equal to zero that's like saying set an alarm for every hour on the hour I'm a little way off because it's 1 44 and 35 seconds so I'm going to set this to 45 and run this script this infinite loop is calling rtc check alarm and that's checking if the alarm condition is true we've just crossed 45 and there it is the alarm is triggered I'll stop the script so every time we call check alarm we're checking if that alarm condition was true if it was then we'll execute whatever action we have for that alarm going off in this case we're just printing a message you'll notice that we're not triggering an alarm for every instance of 45 minutes thereafter check alarm only returns true the first time the condition is met from here on we would have to wait until the 45th minute of the next hour until check alarm would return true so that's a very simple way of setting like an hourly alarm now we can set much more sophisticated alarms than this I'll comment out that call to alarm setup and uncomment the next one I'm using alt 3 and alt 4 to comment and uncomment we can see this one has a weekday argument as well as hours and minutes so this alarm is going to trigger on a wednesday because we have zero is monday tuesday wednesday two for wednesday at 1 23 in the afternoon this will trigger every wednesday at 1 23 in the afternoons that's a once per week schedule and looking at the final example you can see that we have date equals one so this alarm will trigger on the first day of every month and if we're calling check alarm pretty regularly then this will happen about midnight so there you have it using the pikadev real-time clock to keep accurate time while there's a power outage even and setting up some complex scheduling if we need to we now have all the tools we need to do projects as simple as say alarm clocks all the way up to scheduling complex actions if you have any questions about this tutorial just leave us a comment at the bottom of the article we're full-time makers and happy to help until next time thanks for watching


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.