Real Time Clock¶

Written by Grace Lo & Alina Wang

The Real Time Clock (RTC) is directly implemented in flux_chamber.c to accurately keep track of time and make note of when data is logged. The RP2040 has built-in RTC functionality, so additional hardware is not necessary.

API¶

The date and time is defined manually. Otherwise, the RP2040 requires an internet connection to synchronize to the NTP server which results in extra setup steps that are not needed for the chamber's functionality.

For flux_chamber.c, the initial date and time are defined under the CUSTOMIZABLE PARAMETERS section.

For terminal_interface.c, the initial date and time are user-defined in the serial terminal interface using: setdate <yyyy-mm-dd> <hh:mm:ss>. The default is set to Sunday 01 January 00:00:00 2000.

Code¶

All code is in the CornellFluxChamber Github repository and is incorporated into flux_chamber.c.

Includes¶

The first lines of code in the C source file include header files. Don't forget to link these in the CMakeLists.txt file!

The following files are required to use the Real Time Clock.

#include <stdio.h>
#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"

Initializations¶

The Real Time Clock module is initialized and a datetime_t struct is defined as the initial date and time.

rtc_init();
datetime_t t = {
    .year = 2000,
    .month = 01,
    .day = 01,
    .hour = 00,
    .min = 00,
    .sec = 00};
rtc_set_datetime(&t);

Read¶

At the start of each data logging loop, the current date/time is read and converted to an Excel-compatible string.

rtc_get_datetime(&t);
char datetime_str[20] = {0};
snprintf(datetime_str, sizeof(datetime_str), "%02d/%02d/%02d %02d:%02d:%02d", t.month, t.day, t.year % 100, t.hour, t.min, t.sec);

Note that when first opening the .csv file, the date/time cells may show up as ##### (shown below). Simply expand the 1st column to see the text.

CSV File