Adafruit DS3231 Precision RTC Breakout Created by lady ada Last updated on 2018-02-28 02:34:47 AM UTC
Guide Contents Guide Contents Overview Pinouts 2 3 6 Power Pins: I2C Logic pins: Other Pins: 6 6 6 Assembly 8 Prepare the header strip: Add the breakout board: And Solder! 8 9 9 Arduino Usage Download RTCLib First RTC Test Load Demo 12 12 13 13 Reading the Time 15 CircuitPython Downloads Datasheets &c Schematic Fabrication Print 17 21 21 21 21 © Adafruit Industries https://learn.adafruit.
Overview The datasheet for the DS3231 explains that this part is an "Extremely Accurate I²C-Integrated RTC/TCXO/Crystal". And, hey, it does exactly what it says on the tin! This Real Time Clock (RTC) is the most precise you can get in a small, low power package. © Adafruit Industries https://learn.adafruit.
Most RTC's use an external 32kHz timing crystal that is used to keep time with low current draw. And that's all well and good, but those crystals have slight drift, particularly when the temperature changes (the temperature changes the oscillation frequency very very very slightly but it does add up!) This RTC is in a beefy package because the crystal is inside the chip! And right next to the integrated crystal is a temperature sensor.
Comes as a fully assembled and tested breakout plus a small piece of header. You can solder header in to plug it into a breadboard, or solder wires directly. A coin cell is required to use the battery-backup capabilities! We don't include one by default, to make shipping easier for those abroad, but we do stock them so pick one up or use any CR1220 you have handy. © Adafruit Industries https://learn.adafruit.
Pinouts Power Pins: Vin - this is the power pin. Since the RTC can be powered from 2.3V to 5.5V power, you do not need a regulator or level shifter for 3.3V or 5V logic/power. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V GND - common ground for power and logic I2C Logic pins: SCL - I2C clock pin, connect to your microcontrollers I2C clock line.
© Adafruit Industries https://learn.adafruit.
Assembly Prepare the header strip: Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - long pins down © Adafruit Industries https://learn.adafruit.
Add the breakout board: Place the breakout board over the pins so that the short pins poke through the breakout pads © Adafruit Industries https://learn.adafruit.
And Solder! Be sure to solder all pins for reliable electrical contact. (For tips on soldering, be sure to check out our Guide to Excellent Soldering (https://adafru.it/aTk)). © Adafruit Industries https://learn.adafruit.
You're done! Check your solder joints visually and continue onto the next steps Don't forget that the Real Time Clock requires a battery backup. A CR1220 size battery goes in the back, make sure the + symbol on the battery is visible when you insert! © Adafruit Industries https://learn.adafruit.
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! Oops i removed the Power wire from 5V to the Vin rail before taking this pic, don't forget it! Connect Vin to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of.
https://adafru.it/cxm Rename the uncompressed folder RTCLib and check that the RTCLib folder contains RTCLib.cpp and RTCLib.h Place the RTCLib library folder your arduinosketchfolder/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. We also have a great tutorial on Arduino library installation at: http://learn.adafruit.
Upload to your Arduino and check the serial console @ 9600 baud. 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.
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. 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.
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 Serial.
CircuitPython It's easy to use the DS3231 RTC with CircuitPython too! There's a handy Adafruit CircuitPython DS3231 module you can load on a board and get started setting and reading the time with Python code! First wire up the DS3231 to your board as shown on the previous Arduino page. The DS3231 uses a simple I2C connection with: Vin connected to your board's 3.3V or 5V output. GND connected to your board's ground. SCL connected to your board's I2C SCL / clock line.
import adafruit_ds3231 ds3231 = adafruit_ds3231.DS3231(i2c) At this point you're read to read and even set the time of the clock. You just need to interact with the datetime property of the DS3231 instance. For example to read it you can run: ds3231.datetime Notice the time is returned as a special Python time structure.
That's all there is to using the DS3231 with CircuitPython! Simply import the DS3231 module, create an instance of the class, and interact with its datetime property to set and get the time! Here's a complete example program you can save as main.py on your board and see the time and date printed every second to the REPL: # # # # Simple demo of reading and writing the time for the DS3231 real-time clock.
© Adafruit Industries https://learn.adafruit.
Downloads Datasheets &c Maxim product page for the DS3231 Datasheet EagleCAD PCB files on GitHub Fritzing object available in the Adafruit Fritzing Library Schematic Fabrication Print Dims in inches © Adafruit Industries https://learn.adafruit.
© Adafruit Industries Last Updated: 2018-02-28 02:34:46 AM UTC Page 22 of 22