Arduino Usage | Adafruit DS3231 Precision RTC Breakout
Maybe your like
- Overview
- Pinouts
- Assembly
- Arduino Usage
- CircuitPython
- Python Docs
- Downloads
- Single page
- Feedback? Corrections?
- Text View
-
Adafruit DS3231 Precision RTC Breakout $17.50 Add to Cart -
CR1220 12mm Diameter - 3V Lithium Coin Cell Battery Out of Stock -
Adafruit DS3231 Precision RTC - STEMMA QT $13.95 Add to Cart
Arduino Usage
You can easily wire this breakout to any microcontroller, we'll be using an Arduino. For another kind of microcontroller, just make sure it has I2C, then port the code - its pretty simple stuff!
- Connect Vin (red wire) to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V
- Connect GND (black wire) to common power/data ground
- Connect the SCL (yellow wire) pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
- Connect the SDA (blue wire) pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
The DS3231 has a default I2C address of 0x68 and cannot be changed
Download RTCLibFor the RTC library, we'll be using a fork of JeeLab's excellent RTC library RTClib - a library for getting and setting time from an RTC (originally written by JeeLab, our version is slightly different so please only use ours to make sure its compatible!)
To begin reading data, you will need to download Adafruit's RTCLib from the Arduino library manager.
Open up the Arduino library manager:
Search for the RTCLib library and install the one by Adafruit
We also have a great tutorial on Arduino library installation at:http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
First RTC TestThe first thing we'll demonstrate is a test sketch that will read the time from the RTC once per second. We'll also show what happens if you remove the battery and replace it since that causes the RTC to halt. So to start, remove the battery from the holder while the Arduino is not powered or plugged into USB. Wait 3 seconds and then replace the battery. This resets the RTC chip.
Load Demo Open up File->Examples->RTClib->ds3231 and upload to your Arduino wired up to the RTC
Upload to your Arduino and check the serial console @ 57600 baud (this is a change from the photos). After a few seconds, you'll see the report that the Arduino noticed this is the first time the DS3231 has been powered up, and will set the time based on the Arduino sketch.
Unplug your Arduino plus RTC for a few seconds (or minutes, or hours, or weeks) and plug back in.
Next time you run it you won't get the same "RTC lost power" message, instead it will come immediately and let you know the correct time!
From now on, you wont 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. Lets look at the sketch again to see how this is done.
Download File Copy Code void loop () { DateTime now = rtc.now(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); void loop () { DateTime now = rtc.now(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println();There's pretty much only one way to get the time using the RTClib, which is to call now(), a function that returns a DateTime object that describes the year, month, day, 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 get the current year and hour. However, there's one problem where if you happen to ask for the minute right at 3:14:59 just before the next minute rolls over, and then the second right after the minute rolls over (so at 3:15:00) you'll see the time as 3:14:00 which is a minute off. If you did it the other way around you could get 3:15:59 - so one minute off in the other direction.
Because this is not an especially unlikely occurrence - particularly if you're querying the time pretty often - we take a 'snapshot' of the time from the RTC all at once and then we can pull it apart into day() or second() as seen above. Its a tiny bit more effort but we think its worth it to avoid mistakes!
We 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
Download File Copy Code Serial.print(" since midnight 1/1/1970 = "); Serial.print(now.unixtime()); Serial.print("s = "); Serial.print(now.unixtime() / 86400L); Serial.println("d"); Serial.print(" since midnight 1/1/1970 = "); Serial.print(now.unixtime()); Serial.print("s = "); Serial.print(now.unixtime() / 86400L); Serial.println("d");Since there are 60*60*24 = 86400 seconds in a day, we can easily count days since then as well. This might be useful when you want to keep track of how much time has passed since the last query, making some math a lot easier (like checking if its been 5 minutes later, just see if unixtime() has increased by 300, you dont have to worry about hour changes).
Page last edited March 20, 2024
Text editor powered by tinymce.
Assembly CircuitPython Related Guides Digital Clock with CircuitPython By Ruiz Brothers intermediate I2C Addresses and Troublesome Chips By lady ada beginner Your browser does not support the video tag. This links to the guide Mindfulness Clock OF DOOM. Mindfulness Clock OF DOOM By Phillip Burgess intermediate Adding a Real Time Clock to Raspberry Pi By lady ada intermediate Your browser does not support the video tag. This links to the guide Prop-Maker Feather Talking Adabot Clock. Prop-Maker Feather Talking Adabot Clock By Liz Clark beginner MacroPad 2FA TOTP Authentication Friend By Carter Nelson beginner QT Py CH32V203 eInk / ePaper Daily Calendar and Clock By Liz Clark intermediate No-Code, No-Solder Monitoring For Perfect Bread By Ben Everard beginner Plotting Offline Data - JSONL to CSV files, filters... By Tyeth Gundry beginner Metro Minimalist Clock By John Park intermediate Adafruit SPI FLASH Breakouts By Liz Clark beginner Your browser does not support the video tag. This links to the guide Pinball Controller for iPad. Pinball Controller for iPad By John Park intermediate Adafruit FT232H With SPI & I2C Devices By Tony DiCola intermediate MCUME Emulators on Fruit Jam By Tim C intermediate Your browser does not support the video tag. This links to the guide Mini Pan-Tilt Kit Assembly. Mini Pan-Tilt Kit Assembly By Bill Earl beginnerCreate Wishlist
× Title Description Close Search SearchCategories
Tag » Arduino Zs-042 Example
-
How To Use A Real-Time Clock Module (DS3231) - Arduino Cloud
-
Arduino Real Time Clock Module (ZS-042) - Eli The Computer Guy
-
Arduino And DS3231 Real Time Clock Tutorial - How To Mechatronics
-
Configuring The ZS-042 Real Time Clock Module - DS3231
-
Arduino And DS3231 Real Time Clock Tutorial - YouTube
-
Real Time Clock RTC Module Arduino - Random Nerd Tutorials
-
In-Depth: Interface DS3231 Precision RTC Module With Arduino
-
Arduino DS3231 RTC Module Tutorial - Real Time Clock
-
Real Time Clock Using DS3231 (EASY) : 4 Steps - Instructables
-
Sleemanj/DS3231_Simple - GitHub
-
Arduino & Horloge DS3231 (RTC = Real Time Clock) - /dev/tbo
-
ZS-042 RTC (Real Time Clock) Module - Electronics In Touch Co.
-
Bộ 2 Mô đun Arduino DS3231 ZS042 AT24C32 IIC Làm Bộ Nhớ Thời ...
Digital Clock with CircuitPython By
I2C Addresses and Troublesome Chips By
Adding a Real Time Clock to Raspberry Pi By
QT Py CH32V203 eInk / ePaper Daily Calendar and Clock By
Plotting Offline Data - JSONL to CSV files, filters... By
Metro Minimalist Clock By
Adafruit SPI FLASH Breakouts By
Adafruit FT232H With SPI & I2C Devices By
MCUME Emulators on Fruit Jam By