- C 87.3%
- Python 8.1%
- CMake 4.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| include | ||
| src | ||
| CMakeLists.txt | ||
| conanfile.py | ||
| README.md | ||
lf-ds3231
Reusable DS3231 RTC driver library for the Raspberry Pi Pico C SDK, packaged as a Conan package. This is designed for the DS3231 high-precision Real-Time Clock (RTC) found on DS3231 + AT24C32 breakout boards.
This is part of the Lost + Found suite of projects.
🛠️ Usage with CMake
Copy the driver directory into your project as a subdirectory:
add_subdirectory(lib/lf-ds3231)
target_link_libraries(your_project PRIVATE
lf-ds3231
)
And in your C/C++ code:
#include "lf_ds3231.h"
🔌 Hardware Setup
Connect the DS3231/AT24C32 breakout I2C pins to your Raspberry Pi Pico:
- Pico GP4 (SDA) <-> Breakout SDA
- Pico GP5 (SCL) <-> Breakout SCL
- Pico GND <-> Breakout GND
- Pico 3V3 OUT <-> Breakout VCC
(Note: Pins can be configured on any available Pico I2C bus and GPIOs).
💡 Example
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "lf_ds3231.h"
#define I2C_PORT i2c0
#define PIN_SDA 4
#define PIN_SCL 5
int main() {
stdio_init_all();
sleep_ms(2000);
// Initialize I2C
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(PIN_SDA, GPIO_FUNC_I2C);
gpio_set_function(PIN_SCL, GPIO_FUNC_I2C);
gpio_pull_up(PIN_SDA);
gpio_pull_up(PIN_SCL);
// Create and initialize device context
lf_ds3231_t rtc;
if (!lf_ds3231_init(&rtc, I2C_PORT, LF_DS3231_DEFAULT_I2C_ADDR)) {
printf("Failed to initialize driver context!\n");
return -1;
}
// Ping device to verify it's connected
if (!lf_ds3231_ping(&rtc)) {
printf("DS3231 RTC not responding at address 0x%02X!\n", rtc.i2c_addr);
return -1;
}
printf("DS3231 RTC detected successfully.\n");
// Check if oscillator has stopped (power loss or first start)
if (lf_ds3231_is_oscillator_stopped(&rtc)) {
printf("Oscillator stopped! Setting calibration time...\n");
lf_ds3231_time_t calibration_time = {
.seconds = 0,
.minutes = 0,
.hours = 12,
.day_of_week = 1, // Sunday
.date = 2,
.month = 8,
.year = 2026
};
if (lf_ds3231_set_time(&rtc, &calibration_time)) {
printf("Successfully set time.\n");
if (lf_ds3231_clear_oscillator_stop_flag(&rtc)) {
printf("Cleared Oscillator Stop Flag.\n");
}
} else {
printf("Failed to set time!\n");
}
}
// Read time and temperature in a loop
while (true) {
lf_ds3231_time_t now;
if (lf_ds3231_get_time(&rtc, &now)) {
float temp = lf_ds3231_get_temperature(&rtc);
printf("[%04d-%02d-%02d %02d:%02d:%02d] (%s) | Temp: %.2f °C\n",
now.year, now.month, now.date,
now.hours, now.minutes, now.seconds,
lf_ds3231_day_to_str(now.day_of_week), temp);
} else {
printf("Failed to read from DS3231 RTC!\n");
}
sleep_ms(1000);
}
return 0;
}
⚙️ Prerequisites: Setting up Conan with uv
To install the Conan CLI package manager securely and in an isolated environment on your system, it is recommended to use uv:
# Install Conan globally as an isolated tool
uv tool install conan
This makes the conan executable available on your PATH immediately and keeps your system's Python packages clean.
📦 Consumption via Forgejo Conan Registry
The package is hosted and distributed through the Forgejo Conan Package Registry at git.clowderlabs.com.
1. Add the Remote Registry
Add the remote repository to your local Conan configuration:
conan remote add clowder https://git.clowderlabs.com/api/packages/blong/conan
2. Authenticate
Log in using your personal access token:
conan remote login -p <your_token> clowder <username>
3. Add to your Project
Add the dependency to your conanfile.txt (or conanfile.py):
[requires]
lf-ds3231/0.0.1
Then install the dependencies:
conan install . --output-folder=build --build=missing
Expected Output of conan install
When running conan install, Conan downloads the source package from your remote Forgejo registry, unpacks it locally, and generates the necessary target files for CMake:
======== Computing dependency graph ========
lf-ds3231/0.0.1: Not found in local cache, looking in remotes...
lf-ds3231/0.0.1: Checking remote: clowder
Connecting to remote 'clowder' with user 'blong'
lf-ds3231/0.0.1: Downloaded recipe revision 8809dc809c8e0261b8f0ab07c5e434dd
======== Computing necessary packages ========
Requirements
lf-ds3231/0.0.1#8809dc809c8e0261b8f0ab07c5e434dd:da39a3ee5e6b4b0d3255bfef95601890afd80709 - Build
======== Installing packages ========
lf-ds3231/0.0.1: Sources downloaded from 'clowder'
-------- Installing package lf-ds3231/0.0.1 (1 of 1) --------
lf-ds3231/0.0.1: Building from source
lf-ds3231/0.0.1: Package lf-ds3231/0.0.1:da39a3ee5e6b4b0d3255bfef95601890afd80709
>>> Source step
lf-ds3231/0.0.1: Copying sources to build folder
>>> Package step
Calling package()
package(): Packaged 1 '.c' file: lf_ds3231.c
package(): Packaged 1 '.txt' file: CMakeLists.txt
package(): Packaged 1 '.h' file: lf_ds3231.h
Package 'da39a3ee5e6b4b0d3255bfef95601890afd80709' created
======== Finalizing install (deploy, generators) ========
Generator 'CMakeDeps' calling 'generate()'
CMakeDeps necessary find_package() and targets for your CMakeLists.txt
find_package(lf-ds3231)
target_link_libraries(... lf-ds3231::lf-ds3231)
Install finished successfully
🛠️ Local Testing
To test building and exporting this Conan package locally without uploading, run:
conan export .
This will register lf-ds3231/0.0.1 in your local Conan cache for offline testing.