Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

RTC Pi with Raspberry Pi Pico

Using the RTC Pi with Raspberry Pi Pico and MicroPython

The RTC Pi can be used with the Raspberry Pi Pico available at www.raspberrypi.com/products/raspberry-pi-pico/

We have a MicroPython library to use with the Raspberry Pi Pico at github.com/abelectronicsuk/ABElectronics_MicroPython_Libraries.

The example Python files can be found in /ABElectronics_MicroPython_Libraries/RTCPi/demos/

Connecting the RTC Pi to the Raspberry Pi Pico

The RTC Pi library uses the following Raspberry Pi Pico board pins.

Pico Pin Pico GPIO Function Pi Pin Pi GPIO
26 20 I2C SDA 3 GPIO 2
27 21 I2C SCL 5 GPIO 3
40 VBUS 5V 2 5V Power
38 GND GND 6 Ground
36 3V3(OUT) 3V3 1 3v3 Power

Wiring Diagram:

Downloading and Installing the library

To download to your Raspberry Pi, type in the terminal:

git clone https://github.com/abelectronicsuk/ABElectronics_MicroPython_Libraries.git

To install the MicroPython Library, use the Thonny Python IDE from https://thonny.org/

Create a file for your chosen board and copy the contents of the Python file into that board's directory. For example, for the RTC Pi, create a new file in thonny called RTCPi.py, copy contents from RTCPi.py into the new file and save it onto the Raspberry Pi Pico board.

Create a second file where your main program will reside and import the board library at the program's top.

from RTCPi import RTCPi

Run with "Run Current Command" or F5 in Thonny.


Class:

RTC(sda, scl)

Parameters:
sda (optional): I2C SDA pin. If no value is set, the class will default to pin 20.
scl (optional): I2C SCL pin. If no value is set, the class will default to pin 21.

Functions:

set_date(date) 

Set the date and time on the RTC in ISO 8601 format - YYYY-MM-DDTHH:MM:SS
Parameters: date
Returns: null

read_date() 

Returns the date from the RTC in ISO 8601 format - YYYY-MM-DDTHH:MM:SS
Returns: date

enable_output() 

Enable the square-wave output on the SQW pin.
Returns: null

disable_output()

Disable the square-wave output on the SQW pin.
Returns: null

set_frequency(frequency)

Set the frequency for the square-wave output on the SQW pin.
Parameters: frequency - options are: 1 = 1Hz, 2 = 4.096KHz, 3 = 8.192KHz, 4 = 32.768KHz
Returns: null

write_memory(address, valuearray)

Write to the memory on the ds1307. The ds1307 contains 56-byte, battery-backed RAM with Unlimited Writes
Parameters: address - 0x08 to 0x3F
valuearray - a byte array containing data to be written to memory
Returns: null

read_memory(address, length)

Read from memory on the ds1307
Parameters: address - 0x08 to 0x3F length - up to 32 bytes.
Length can not exceed the available address space.
Returns: array of bytes

Usage

To use the RTC Pi library in your code, you must first import the library:

from RTCPi import RTC

Next, you must initialise the RTC object:

rtc = RTC()

Set the current time in ISO 8601 format:

rtc.set_date("2013-04-23T12:32:11")

Enable the square-wave output at 8.192KHz on the SQW pin:

rtc.set_frequency(3)
rtc.enable_output()

Read the current date and time from the RTC at 1-second intervals:

while (True):
  print rtc.read_date()
  time.sleep(1)

Was this article helpful? Yes No

Please tell us how we can improve this article:

Submit
Created 27/10/2022 | Last Updated: 25/10/2023

Related Expansion Boards

Order a RTC Pi Photo of RTC Pi
RTC Pi

Real-time clock for the Raspberry Pi

£11.39 Ex VAT: £9.49
Add to Basket

Related Articles

RTC Pi setup on Raspberry Pi OS
A tutorial showing how to use the RTC Pi on a Raspberry Pi
I2C Switch with Raspberry Pi Pico
Using the I2C Switch with Raspberry Pi Pico and MicroPython
RTC Pi with Raspberry Pi Pico
Using the RTC Pi with Raspberry Pi Pico and MicroPython
RTC Pi storing integers in RTC memory
A tutorial showing how to use the internal memory for storing integers on the RTC Pi
RTC Pi storing double values in RTC memory
A tutorial showing how to use the internal memory for storing double values on the RTC Pi
RTC Pi and Arduino
Adding a Real Time clock to an Arduino