Wiring

Wiring it upwardly is easy, connect it up as shown below.

  • GND to GND (black wire on STEMMA QT version) on your lath
  • VCC (red wire on STEMMA QT version) to the logic level ability of your lath (on classic Arduinos & Metros utilize 5V, on iii.3V devices utilize 3.3V)
  • SDA to the SDA (blueish wire on STEMMA QT version) i2c data pin
  • SCL to the SCL (yellow wire on STEMMA QT version) i2c clock pin

At that place are internal 10K pull-ups on the PCF8523 on SDA and SCL to the VCC voltage

Talking to the RTC

The RTC is an i2c device, which means it uses ii wires to to communicate. These two wires are used to set up the time and recall information technology.

For the RTC library, we'll be using a fork of JeeLab'due south first-class RTC library, which is available on GitHub. Yous tin can do that by visiting the github repo and manually downloading or, easier become to the Arduino Library Director

circuitpython_libmanage.png

Type in RTClib - and find the one that is by Adafruit and click Install

circuitpython_lib.png

There are a few different 'forks' of RTClib, make sure you are using the ADAFRUIT i!

We also take a great tutorial on Arduino library installation at:
http://larn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Once done, restart the IDE

Outset RTC test

The offset thing we'll demonstrate is a examination sketch that will read the time from the RTC in one case a second. We'll as well show what happens if you remove the battery and replace it since that causes the RTC to halt. So to start, remove the bombardment from the holder while the Plume is not powered or plugged into USB. Wait three seconds and and then supercede the bombardment. This resets the RTC bit. Now load up the matching sketch for your RTC

Open Examples->RTClib->pcf8523

Upload it to your lath with the PCF8523 breakout lath or FeatherWing connected

circuitpython_feather_example.png

Now open up up the Serial Console and make sure the baud rate is set correctly at 57600 baud you lot should meet the following:

circuitpython_feather_rtcnotrunning.png

Whenever the RTC flake loses all power (including the backup battery) it will reset to an earlier date and report the time as 0:0:0 or similar. Whenever you set the time, this will kickstart the clock ticking.

And then, basically, the upshot here is that you should never ever remove the battery in one case you've set up the time. You shouldn't take to and the bombardment holder is very snug so unless the lath is crushed, the battery won't 'fall out'

Setting the time

With the aforementioned sketch loaded, uncomment the line that starts with RTC.adjust like so:

                  if (! rtc.initialized()) {     Series.println("RTC is Not running!");     // post-obit line sets the RTC to the date & time this sketch was compiled     rtc.adapt(DateTime(F(__DATE__), F(__TIME__)));
                  if (! rtc.initialized()) {     Serial.println("RTC is NOT running!");     // post-obit line sets the RTC to the appointment & time this sketch was compiled     rtc.accommodate(DateTime(F(__DATE__), F(__TIME__)));

This line is very cute, what it does is take the Date and Time according the computer you're using (right when you compile the lawmaking) and uses that to plan the RTC. If your computer time is not set right you should set up that start. Then you must press the Upload button to compile and and so immediately upload. If you compile and so upload later on, the clock will be off by that amount of time.

So open up the Serial monitor window to bear witness that the time has been set

circuitpython_feather_rtcset.png

From now on, you lot won't have to ever set the time again: the battery will last 5 or more years

Reading the time

Now that the RTC is merrily ticking away, we'll want to query it for the time. Allow's look at the sketch again to run into how this is done

void loop () {     DateTime at present = rtc.now();          Serial.print(now.year(), DEC);     Series.print('/');     Serial.print(at present.calendar month(), Dec);     Serial.print('/');     Serial.print(now.day(), December);     Serial.print(" (");     Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);     Serial.impress(") ");     Serial.impress(now.hour(), December);     Serial.print(':');     Serial.print(now.infinitesimal(), DEC);     Serial.print(':');     Serial.impress(at present.second(), December);     Serial.println();
void loop () {     DateTime now = rtc.now();          Serial.print(now.year(), December);     Serial.print('/');     Series.print(now.calendar month(), DEC);     Serial.print('/');     Series.impress(at present.day(), December);     Serial.print(" (");     Serial.print(daysOfTheWeek[at present.dayOfTheWeek()]);     Serial.print(") ");     Serial.impress(at present.hour(), DEC);     Serial.print(':');     Series.print(now.minute(), December);     Serial.print(':');     Series.print(at present.second(), DEC);     Serial.println();

In that location's pretty much merely 1 mode to get the time using the RTClib, which is to call now(), a role that returns a DateTime object that describes the twelvemonth, month, twenty-four hours, hour, minute and second when you called now().

There are some RTC libraries that instead have you call something like RTC.year() and RTC.hour() to go the current year and hour. However, there'due south one problem where if you happen to ask for the minute right at three:14:59 just before the next minute rolls over, and and then the second right afterwards the minute rolls over (then at iii:fifteen:00) you'll see the time every bit 3:14:00 which is a minute off. If yous did it the other way around y'all could get 3:fifteen:59 - then one minute off in the other direction.

Because this is not an especially unlikely occurance - particularly if you're querying the time pretty often - we take a 'snapshot' of the time from the RTC all at once and then nosotros can pull it apart into 24-hour interval() or second() as seen higher up. It's a tiny chip more effort merely we think its worth information technology to avoid mistakes!

We tin can also get a 'timestamp' out of the DateTime object by calling unixtime which counts the number of seconds (not counting leapseconds) since midnight, January 1st 1970

                  Series.print(" since 2000 = ");     Serial.print(now.unixtime());     Series.print("s = ");     Serial.print(now.unixtime() / 86400L);     Serial.println("d");
                  Serial.print(" since 2000 = ");     Serial.impress(now.unixtime());     Serial.print("southward = ");     Serial.print(now.unixtime() / 86400L);     Serial.println("d");

Since at that place are lx*threescore*24 = 86400 seconds in a day, we can easily count days since then too. This might be useful when you want to continue track of how much time has passed since the concluding query, making some math a lot easier (like checking if it's been 5 minutes afterward, just see if unixtime() has increased past 300, you dont have to worry virtually 60 minutes changes)

This guide was start published on Oct 29, 2017. Information technology was terminal updated on Oct 29, 2017.

This page (RTC with Arduino) was last updated on Apr 15, 2022.

Text editor powered by tinymce.