RTC (Real Time Clock)
This page explains how to connect an RTC module to the Pico board.
About RTC Module
An RTC module is hardware that keeps track of real-time date and time.
If you read RP2040 Datasheet, you will find that the RP2040 microcontroller has a built-in RTC module. However, this built-in RTC does not have a backup battery, so it loses the date and time when power is lost. Therefore, it is not practical for use in applications where maintaining accurate time is important.
The practical way to use RTC is to connect an RTC module with a backup battery to the Pico board. This allows the date and time to be retained regardless of the Pico board's power state.
The DS3231 is a commonly used RTC module for electronics projects, as it can be connected via I2C. There is also a cheaper DS1307, but it can drift by several seconds per day, so the DS3231 is recommended for its higher accuracy.
The DS3231 module I purchased from Amazon looks like this:

It comes with a backup battery pre-installed and has a compact shape. The signal names printed on the board can be confusing, but they correspond as follows:
| Board Label | Signal Name |
|---|---|
+ |
VCC (3.3V) |
D |
I2C SDA |
C |
I2C SCL |
- |
GND |
There is also a cheaper RTC module named DS1307, but it can drift by several seconds per day, so the more accurate DS3231 is recommended. The DS1307 also shares the same I2C address and data format as the DS3231, so it should work similarly when connected to the Pico board.
Wiring
The breadboard wiring image is as follows:

Building and Flashing the Program
Create a new Pico SDK project named rtctest. Check Console over UART and/or Console over USB in Stdio support.
Create Pico SDK Project
- In VSCode, run
>Raspberry Pi Pico: New Pico Projectin the command palette. -
In the dialog below, select
C/C++.
-
Create a project with the following settings:
- Name ... Enter the project name.
- Board type ... Select your board type.
- Location ... Select the parent directory where the project directory will be created.
- Stdio support ... Leave
Console over USBunchecked when you use LABOPlatform or other USB features because they conflict with each other. You can enable it by editing theCMakeLists.txtfile later. - Code generation options ... Check
Generate C++ code.

Open Existing Pico SDK Project
Open the project folder in VSCode using one of the following methods:
- In a command prompt, change the current directory to the project folder and execute
code .. -
In a Explorer, choose the project folder, push
Alt+Dto focus the address bar, and executecode ..
If the folder is already prepared as a Pico SDK project, just proceed with editing and building the project.
If not, you will see the following message at the bottom right corner of VSCode.

Click Yes and you will see the following window.

Click Import and the project will be prepared as a Pico SDK project.
When the Do you want to import this project as Raspberry Pi Pico project? message disappears before you click Yes, you can reveal it by clicking the icon
at the bottom right corner of VSCode.
Clone the pico-jxglib repository from GitHub so the direcory structure looks like this:
Clone the Repository
Change the current directory to the parent directory where you want to clone the pico-jxglib repository and run the following commands:
$ git clone https://github.com/ypsitau/pico-jxglib.git
$ cd pico-jxglib
$ git submodule update --init --recursive
pico-jxglib is updated almost daily. If you've already cloned it, run the following command in the pico-jxglib directory to get the latest version:
A directory from a Git repository can safely be moved to another location even after cloning.
Add the following lines to the end of CMakeLists.txt:
| CMakeLists.txt | |
|---|---|
Edit rtctest.cpp as follows:
Build and flash the program to the board.
Build and Flash
The simplest way to build and flash the program is to use the UF2 file generated by the build process. Pico board, a USB cable, and a computer are all you need to get started. Here are the steps to build and flash the program:
-
Pressing
F7on VSCode will build the project. If this is the first time you build the project, you will see the dialog shown below. SelectPico Using compilers: ...and the build process will start.
-
After building, you can find the generated UF2 file in the
builddirectory. - Connect your Pico to the computer using a USB cable while holding the BOOTSEL button, and it will appear as a mass storage device. Copy the generated UF2 file to this device to flash it. No need to mind the destination directory, just copy it to the root directory of the device.
If you have a debug probe like this, you can also flash the program using OpenOCD and GDB. This is the recommended method for development, as it allows you to debug the program while running it on the board!
Running the Program
The program prints the current date and time every second. The output looks like this: