top of page
Time and Date Using Raspberry Pi Pico built-in RTC
e65ee55c3e7d0b9b7380c17764a16c78.jpg

Introduction


Knowing how to add time and date functionality into our project is a skill that really comes in handy. A typical example is a data logging project where we need to collect and store sensor data alongside the time the data was recorded.

​

Usually, with most embedded boards like Arduino, we achieve this by including an external RTC module in our circuit. But with the Pi Pico, we boycott this hassle by just making use of the built-in RTC on the RP2040 chip.

What we'll learn


In this tutorial, we'll learn how we can integrate time and date functionality into our project using the Raspberry Pi Pico built-in Real-Time Clock.

What we'll need

 

Real-Time Clock (RTC)

​

A real-time clock (RTC) is a computer clock, usually in the form of an integrated circuit that is solely built for keeping time. Naturally, it counts hours, minutes, seconds, months, days, and even years. RTC ICs regulate time with the use of a crystal oscillator and do not rely on clock signals like most hardware clocks. 

​

RTCs can be found running in personal computers, embedded systems, and servers, and are present in any electronic device that may require accurate timekeeping. Usually, RTCs come as somewhat stand-alone systems with their own battery to enable them to still function even when the computer is powered down.

​

Further read on RTC...

​

Luckily for us, the Raspberry Pi Pico has an RTC module builtin into the RP2040 chip. So we do not need an external module to be able to integrate the time and date function into our project (meaning reduced project footprint and cost).
 

Hardware Assembly

​

All we need here is to connect our Pi Pico to the computer we'll be programming with (e.g PC)

Programming

​

Launch your Thonny IDE then type in the following code:

from machine import RTC

​

rtc = RTC()


date_time = rtc.datetime()


print(date_time)

You can save your code as "pico_RTC.py", then clock on the green play button at the top of Thonny IDE to run.

How the code works

​

  • Import the RTC module from the machine library.

from machine import RTC

  • Initialize RTC class as rtc

rtc = RTC()

  • declare a variable named date_time and assign it the rtc.datetime() method.

date_time = rtc.datetime()

  • print date_time

print(date_time)

This will print (in a tuple) the current date and time as shown below:

format: (year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

Actual result: (2022, 10, 21, 4, 10, 10, 48, 0)

Simplified result:

​

We can see from the above result that the date and time is merged into the same lime. We can actually separate these two and even determine the result we want to print with few code lines below:

from machine import RTC


rtc = RTC()


date = rtc.datetime()[0:3]
time = rtc.datetime()[4:7]


print("Date: ", *date)
print("Time: ", *time)

Result:

Date:  2022 10 21
Time:  10 20 12

Code explanation:

​

  • Simply declare the date and time variables separately.

  • Accessed the elements in the tuple using the index number of choice. 

​

[0:3] to get year, month, and day
[4:7] to get hour, minute,s and seconds

The above result prints the date and time with empty spaces in between. We can fill those spaces with any character of choice. Here. we'll use a hyphen and colon for date and time respectively.

print("Date: ", '-'.join(str(x) for x in date))
print("Time: ", ':'.join(str(x) for x in time))

Result:

Date:  2022-10-21
Time:  10:35:41

bottom of page