element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Arduino
  • Products
  • More
Arduino
Blog Real Time Clock on the Raspberry Pi Pico
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Workshopshed
  • Date Created: 25 Jun 2022 10:56 AM Date Created
  • Views 3789 views
  • Likes 8 likes
  • Comments 3 comments
  • rtc
  • pi pico
Related
Recommended

Real Time Clock on the Raspberry Pi Pico

Workshopshed
Workshopshed
25 Jun 2022

I've been working on a Raspberry Pi Pico project that I wanted to run off batteries. I found a "low power" example in the pico-extras which uses the Real Time Clock to wake the device up after an interval. So I set about porting it to the Arduino platform and had a bunch of issues with it compiling and linking, mostly to do with the real time clock.

I found a library RP2040_RTC by Khoi Hoang but that didn't seem to want to install and when I manually installed it, it didn't want to compile.

Help was at hand however in the Arduino core discussion forum - https://github.com/arduino/ArduinoCore-mbed/issues/461 this gave me a few ideas to try.

I eventually stumbled upon the rtc_api part of the code provided with the "Pi Pico Board" in Arduino. It had some calls I could use to initialise the clock and check it was running. I found that it took a few ms to run and could incorrectly report 1970 as the date if you didn't wait for the enabled status to change. Khoi's code also helped me out in recommending Paul Stoffregen's time library to do some of the basic conversions.

This led to the following code:

#include "rtc_api.h"
#include <TimeLib.h> //https://github.com/PaulStoffregen/Time

tmElements_t tm;
time_t t;

void setup() {
tm.Year = CalendarYrToTm(2022);
tm.Month = 06;
tm.Day = 25;
tm.Wday = 6; // 0 is Sunday, so 6 is Saturday
tm.Hour = 8;
tm.Minute = 23;
tm.Second = 00;

t = makeTime(tm);
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);

sleep_ms(5000);

// Start the RTC
Serial.println("Initialising RTC");
rtc_init();

//Write initial date
rtc_write(t);

while(!rtc_isenabled())
{ Serial.print(".");}
Serial.println("");

Serial.println("RTC Running");
}

void loop() {
t = rtc_read();

Serial.print(year(t));
Serial.print("-");
Serial.print(month(t));
Serial.print("-");
Serial.print(day(t));
Serial.print(" ");
Serial.print(hour(t));
Serial.print(":");
Serial.print(minute(t));
Serial.print(":");
Serial.println(second(t));
Serial.println();
sleep_ms(1000);
}

With a bit of tuning I also go the alarm function to work you can see both examples at: https://github.com/Workshopshed/PiPico

  • Sign in to reply
  • Workshopshed
    Workshopshed 11 months ago

    DAB not tested for long delays but have seen comments about the PICO RTC not being very accurate.

    Reply

    More

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB 11 months ago

    Have you conducted any long time accuracy tests with this set up?

    I am curious if it might develop time drift due to the sleep mode.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed 11 months ago

    Just tried to reproduce the error with Khoi's library and that's working just fine now. Not quite sure what else changed.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube