Skip to content

RTC (Real Time Clock) Support

In this page, we will connect a DS3231 RTC module to the board and check the timestamp is recorded correctly when creating files in the flash file system.

Wiring

The breadboard wiring image is as follows:

circuit-rtc

Building and Flashing the Program

Create a new Pico SDK project named fs-timestamp. Check Console over UART and/or Console over USB in Stdio support.

Create Pico SDK Project
  1. In VSCode, run >Raspberry Pi Pico: New Pico Project in the command palette.
  2. In the dialog below, select C/C++. new-project-dialog

  3. 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 USB unchecked when you use LABOPlatform or other USB features because they conflict with each other. You can enable it by editing the CMakeLists.txt file later.
    • Code generation options ... Check Generate C++ code.

new-project

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+D to focus the address bar, and execute code ..

    vscode-from-explorer

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.

do-you-want-to-import

Click Yes and you will see the following window.

import-project

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 notify-icon at the bottom right corner of VSCode.

Clone the pico-jxglib repository from GitHub so the direcory structure looks like this:

├── pico-jxglib/
└── fs-timestamp/
    ├── CMakeLists.txt
    ├── fs-timestamp.cpp
    └── ...
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:

$ git pull

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
1
2
3
4
5
6
target_link_libraries(fs-timestamp
    jxglib_Shell jxglib_Serial jxglib_ShellCmd_Basic
    jxglib_LFS_Flash jxglib_FAT_Flash jxglib_ShellCmd_FS
    jxglib_RTC_DS3231 jxglib_ShellCmd_RTC)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../pico-jxglib pico-jxglib)
jxglib_configure_FAT(fs-timestamp FF_VOLUMES 2)

Edit fs-timestamp.cpp as follows:

fs-timestamp.cpp
#include <stdio.h>
#include "pico/stdlib.h"
#include "jxglib/Serial.h"
#include "jxglib/Shell.h"
#include "jxglib/LFS/Flash.h"
#include "jxglib/FAT/Flash.h"
#include "jxglib/RTC/DS3231.h"

using namespace jxglib;

int main()
{
    ::stdio_init_all();
    // Prepare the Shell with Stdio
    Serial::Terminal terminal;
    Shell::AttachTerminal(terminal.Initialize());
    // Prepare the RTC
    ::i2c_init(i2c0, 400'000);
    GPIO16.set_function_I2C0_SDA().pull_up();
    GPIO17.set_function_I2C0_SCL().pull_up();
    RTC::DS3231 rtc(i2c0);    
    // Declare the flash drive with a name and size (must be a multiple of 4096)
    LFS::Flash driveLFS1("J:", 0x1010'0000, 0x0004'0000);  // 256kB
    LFS::Flash driveLFS2("*K:", 0x1014'0000, 0x0004'0000); // 256kB .. primary drive
    FAT::Flash driveFAT1("L:", 0x1018'0000, 0x0004'0000);  // 256kB
    FAT::Flash driveFAT2("M:", 0x101c'0000, 0x0004'0000);  // 256kB
    for (;;) {
        Tickable::Tick();
    }
}

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:

  1. Pressing F7 on VSCode will build the project. If this is the first time you build the project, you will see the dialog shown below. Select Pico Using compilers: ... and the build process will start.

    select-a-kit

  2. After building, you can find the generated UF2 file in the build directory.

  3. 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

Check the current status of drives with ls-drive command:

K:?>ls-drive
 Drive  Format        Total
 J:     none              0
*K:     none              0
 L:     none              0
 M:     none              0
K:?>

Format the flash drive and check the drive information again:

K:?>format j: k: l: m:
drive j: formatted in LFS
drive k: formatted in LFS
drive l: formatted in FAT12
drive m: formatted in FAT12
K:/>ls-drive
 Drive  Format        Total
 J:     LFS          262144
*K:     LFS          262144
 L:     FAT12        262144
 M:     FAT12        262144

Check if the RTC is working by executing rtc command:

K:/>rtc
2026-05-01 13:36:47.000

Create a file in K: drive, which is formatted in LFS, and check the timestamp:

K:/>touch file1
K:/>dir
-a--- 2026-05-01 13:37:52      0 file1

Create a file in L: drive, which is formatted in FAT12, and check the timestamp:

K:/>L:
L:/>dir
L:/>touch file2
L:/>dir
-a--- 2026-05-01 13:39:16      0 file2