User Tools

Site Tools


tutorials:learn:breakoutplus:ds1307rtc.html

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorials:learn:breakoutplus:ds1307rtc.html [2012/04/15 00:49]
brianernst Added pin connection to arduino
tutorials:learn:breakoutplus:ds1307rtc.html [2016/01/28 18:05] (current)
Line 1: Line 1:
-==== What is an RTC?  ​==== +====== ​Moved! ======
- +
- +
-A real time clock is basically just like a watch - it runs on a battery and keeps time for you even when there is a power outageUsing an RTC, you can keep track of long timelines, even if you reprogram your microcontroller or disconnect it from USB or a power plug. +
- +
- +
- +
-Most microcontrollers,​ including the Arduino, ​ have a built-in timekeeper called** millis()** and there are also timers built into the chip that can keep track of longer time periods like minutes or days. So why would you want to have a seperate RTC chip? Well, the biggest reason is that **millis()** only keeps track of time //since the Arduino was last powered** - **//. That means that when the power is turned on, the millisecond timer is set back to 0. The Arduino doesn'​t know that it's '​Tuesday'​ or 'March 8th', all it can tell is '​It'​s been 14,000 milliseconds since I was last turned on'.  +
- +
- +
- +
-OK so what if you wanted to set the time on the Arduino? You'd have to program in the date and time and you could have it count from that point on. But if it lost power, you'd have to reset the time. Much like very cheap alarm clocks: every time they lose power they blink **12:00** +
- +
- +
- +
-While this sort of basic timekeeping is OK for some projects, some projects such as data-loggers,​ clocks, etc will need to have **consistent timekeeping that doesn'​t reset when the Arduino battery dies or is reprogrammed**. Thus, we include a seperate RTC! The RTC chip is a specialized chip that just keeps track of time. It can count leap-years and knows how many days are in a month, but it doesn'​t take care of Daylight Savings Time (because it changes from place to place) +
- +
-[[http://​en.wikipedia.org/​wiki/​File:​Realtimeclock_Motherboard_Baby_AT_crop.jpg|{{ ​ http://​www.ladyada.net/​images/​logshield/​Realtimeclock_Motherboard_Baby_AT_crop.jpg?​nolink&​314x241 ​ |}}]]  +
-<class center> //This image shows a computer motherboard with a Real Time Clock called the [[http://​www.maxim-ic.com/​app-notes/​index.mvp/​id/​503|DS1387]]. There'​s a lithium battery in there which is why it's so big.//</​class>​ +
- +
- +
- +
-The RTC we'll be using is the[[http://​www.ladyada.net/​wiki/​partselector/​ic#​rtc| DS1307]]. It's low cost, easy to solder, and can run for years on a very small coin cell. +
- +
-[[http://​www.ladyada.net/​images/​parts/​ds1307.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​ds1307_t.jpg?​nolink&​150x120 ​ |}}]] +
- +
-As long as it has a coin cell to run it, the DS1307 will merrily tick along for a long time, even when the Arduino loses power, or is reprogrammed.  +
- +
-<class warning>​ +
-**You MUST have a coin cell installed for the RTC to work, if there is no coin cell, you should pull the battery pin low.** +
-</​class>​ +
- +
-==== Files  ==== +
- +
- +
-[[http://​github.com/​adafruit/​DS1307-breakout-board|Schematic and layout files can be found at GitHub - click Download Source to get the zip!]] +
- +
- +
-==== Parts  ==== +
-<class table> +
-^Image^Name^Description^Part information^Qty^ +
-|[[http://​www.ladyada.net/​images/​parts/​ds1307.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​ds1307_t.jpg?​nolink&​150x120 ​ |}}]]|**IC2**| Real time clock|[[http://​www.ladyada.net/​wiki/​partselector/​ic?​s[]=ds1307#​rtc|DS1307]]|1| +
-|[[http://​www.ladyada.net/​images/​parts/​crystalcyl.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​crystalcyl_t.jpg?​nolink&​199x130 ​ |}}]]|**Q1**|32.768 KHz, 12.5 pF watch crystal|[[http://​www.ladyada.net/​wiki/​partselector/​crystals?​s[]=32.768 khz#​crystals|Generic 32.768KHz crystal]]|1| +
-|{{ http://​www.ladyada.net/​images/​parts/​resleft_t.gif?​nolink&​61x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rRed_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rRed_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rRed_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​spacer_t.gif?​nolink&​8x87 |}}{{ http://​www.ladyada.net/​images/​parts/​rGold_t.gif?​nolink&​10x87 |}}{{ http://​www.ladyada.net/​images/​parts/​resright_t.gif?​nolink&​66x87 |}}|**R1, R2**|1/4W 5% 2.2K resistor \\ Red, Red, Red, Gold |[[http://​www.ladyada.net/​wiki/​partselector/​resistors#​w_5_carbon|Generic]]|2| +
-|[[http://​www.ladyada.net/​images/​parts/​104cerm.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​104cerm_t.jpg?​nolink&​154x200 ​ |}}]]|**C1**|0.1uF ceramic capacitor (104) |[[http://​www.ladyada.net/​wiki/​partselector/​caps?​s[]=0.1uf%20%2F%2050v#​ceramic_capacitors|Generic]]|1| +
-|[[http://​www.ladyada.net/​images/​parts/​headerm36.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​headerm36_t.jpg?​nolink&​200x84 ​ |}}]]||5 pin male header (1x5) |[[http://​www.ladyada.net/​wiki/​partselector/​header#​male_header|Generic]]|1| +
-|[[http://​www.ladyada.net/​images/​parts/​CR2032.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​CR2032_t.jpg?​nolink&​150x79 ​ |}}]]|**BATT**|12mm 3V lithium coin cell|[[http://​www.ladyada.net/​wiki/​partselector/​batteries#​coin| CR1220]]|1| +
-|[[http://​www.ladyada.net/​images/​parts/​cr1220thm.jpg|{{ ​ http://​www.ladyada.net/​images/​parts/​cr1220thm_t.jpg?​nolink&​130x105 ​ |}}]]|**BATT'​**|12mm coin cell holder|[[http://​www.ladyada.net/​wiki/​partselector/​powerconn#​battery_holders|Keystone 3001]]|1| +
-|[[http://​www.ladyada.net/​images/​ds1307/​ds1307bb.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​ds1307bb_t.jpg?​nolink&​169x130 ​ |}}]]|**PCB**|Circuit board|[[http://​www.adafruit.com|Adafruit Industries ]]|1| +
- +
- +
-</​class>​ +
- +
-==== Assemble! ​ ==== +
- +
-|[[http://​www.ladyada.net/​images/​ds1307/​ready.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​ready_t.jpg?​nolink&​500x385 ​ |}}]]|Prepare to assemble the kit by checking the parts list and verifying you have everything! \\ Next, heat up your soldering iron and clear off your desk. \\ Place the circuit board in a vise so that you can easily work on it| +
-|[[http://​www.ladyada.net/​images/​ds1307/​bumpsolder.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​bumpsolder_t.jpg?​nolink&​500x385 ​ |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​bumped.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​bumped_t.jpg?​nolink&​500x385 |}}]]|Begin by soldering a small bump onto the negative pad of the battery: this will make better contact!| +
-|[[http://​www.ladyada.net/​images/​ds1307/​resplace.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​resplace_t.jpg?​nolink&​500x385 ​ |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​allpalce.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​allpalce_t.jpg?​nolink&​500x385 |}}]]|place the two 2.2K resistors, and the ceramic capacitor. They are symmetric so no need to worry about direction. \\ Then place the crystal (also symmetric), the battery holder (goes on so that the battery can slip in the side) and the RTC chip.  \\ The RTC chip must be placed so that the notch/dot on the end match the silkscreen. Look at the photo on the left, the notch is pointing down. Double check this before soldering in the chip because its quite hard to undo!| +
-|[[http://​www.ladyada.net/​images/​ds1307/​tack.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​tack_t.jpg?​nolink&​500x385 ​ |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​flip.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​flip_t.jpg?​nolink&​500x385 |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​solder.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​solder_t.jpg?​nolink&​500x385 |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​soldered.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​soldered_t.jpg?​nolink&​500x385 |}}]]|To keep the battery holder from falling out, you may want to '​tack'​ solder it from the top \\ Then flip over the board and solder all the pins.| +
-|[[http://​www.ladyada.net/​images/​ds1307/​clip.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​clip_t.jpg?​nolink&​500x385 ​ |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​clipped.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​clipped_t.jpg?​nolink&​500x385 |}}]]|Clip the leads of the resistors, crystal and capacitor short.| +
-|[[http://​www.ladyada.net/​images/​ds1307/​header.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​header_t.jpg?​nolink&​500x385 ​ |}}]] \\ [[http://​www.ladyada.net/​images/​ds1307/​headered.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​headered_t.jpg?​nolink&​500x385 |}}]] |If you'd like to use the header to plug the breakout board into something, place the header in a breadboard, long side down and place the board so that the short pins stick thru the pads. \\ Solder them in place.| +
-|[[http://​www.ladyada.net/​images/​ds1307/​batt.jpg|{{ http://​www.ladyada.net/​images/​ds1307/​batt_t.jpg?​nolink&​500x385 |}}]]|Insert the battery so that the flat + side is UP. // The battery will last for many years, 5 or more, so no need to ever remove or replace it. // **You MUST have a coin cell installed for the RTC to work, if there is no coin cell, it will act strangly and possibly hang the Arduino so ALWAYS make SURE there'​s a battery installed, even if its a dead battery.**| +
- +
-==== Arduino library ​ ==== +
-Any 5V microcontroller with I2C built-in can easily use the DS1307. We will demonstrate how to use it with an Arduino since it is a popular microcontroller platform.  +
- +
-For the RTC library, we'll be using a fork of JeeLab'​s excellent RTC library [[http://​github.com/​adafruit/​RTClib|RTClib ]]- a library for getting and setting time from a DS1307 (originally written by JeeLab, our version is slightly different so please **only use ours **to make sure its compatible!) - download the .zip by clicking on **Download Source** (top right) and rename the uncompressed folder RTClib [[http://​www.ladyada.net/​library/​arduino/​libraries.html|Then install it in your Arduino directory]] in a folder called **RTClib** +
- +
- +
-==== Wiring it up!  ==== +
-There are only 5 pins: **5V GND SCL SDA SQW**.  +
- +
- +
-  ***5V **is used to power to the RTC chip when you want to query it for the time. If there is no 5V signal, the chip goes to sleep using the coin cell for backup.  +
-  ***GND** is common ground and is required +
-  ***SCL **is the i2c clock pin - its required to talk to the RTC +
-  ***SDA** is the i2c data pin - its required to talk to the RTC +
-  ***SQW** is the optional square-wave output you can get from the RTC if you have configured it to do so. Most people don't need or use this pin[[http://​www.ladyada.net/​images/​ds1307/​breadboard.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​breadboard_t.jpg?​nolink&​500x389 ​ |}}]] +
- +
-If you set analog pin 3 (digital 17) to an OUTPUT and HIGH and analog pin 2 (digital 16) to an OUTPUT and LOW you can power the RTC directly from the pins! +
- +
-Connect Arduino analog pin 4 to SDA. Connect Arduino analog pin 5 to SCL. +
- +
-[[http://​www.ladyada.net/​images/​ds1307/​quickplug.jpg|{{ ​ http://​www.ladyada.net/​images/​ds1307/​quickplug_t.jpg?​nolink&​500x385 ​ |}}]] +
- +
- +
-==== First RTC test  ==== +
- +
- +
-The first thing we'll demonstrate is a test sketch that will read the time from the RTC once a 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. Now load up the following sketch (which is also found in** Examples->​RTClib->​ds1307**) and upload it to your Arduino with the datalogger shield on! +
- +
- +
-<code C> +
-// Date and time functions using a DS1307 RTC connected via I2C and Wire lib +
- +
-#include <​Wire.h>​ +
-#include "​RTClib.h"​ +
- +
-RTC_DS1307 RTC; +
- +
-void setup () { +
-    Serial.begin(57600);​ +
-    Wire.begin();​ +
-    RTC.begin();​ +
- +
-  if (! RTC.isrunning()) { +
-    Serial.println("​RTC is NOT running!"​);​ +
-    // following line sets the RTC to the date & time this sketch was compiled +
-    //​RTC.adjust(DateTime(__DATE__,​ __TIME__));​ +
-  } +
- +
-+
- +
-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(now.hour(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(now.minute(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(now.second(),​ DEC); +
-    Serial.println();​ +
-     +
-    Serial.print("​ since 1970 = "); +
-    Serial.print(now.unixtime());​ +
-    Serial.print("​s = "); +
-    Serial.print(now.unixtime() / 86400L); +
-    Serial.println("​d"​);​ +
-     +
-    // calculate a date which is 7 days and 30 seconds into the future +
-    DateTime future (now.unixtime() + 7 * 86400L + 30); +
-     +
-    Serial.print("​ now + 7d + 30s: "); +
-    Serial.print(future.year(),​ DEC); +
-    Serial.print('/'​);​ +
-    Serial.print(future.month(),​ DEC); +
-    Serial.print('/'​);​ +
-    Serial.print(future.day(),​ DEC); +
-    Serial.print('​ '); +
-    Serial.print(future.hour(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(future.minute(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(future.second(),​ DEC); +
-    Serial.println();​ +
-     +
-    Serial.println();​ +
-    delay(3000);​ +
-}</​code>​ +
- +
-Now run the Serial terminal and make sure the baud rate is set correctly at 57600 bps +
- +
-you should see the following:​{{ ​ http://​www.ladyada.net/​images/​logshield/​RTCnotrun.gif?​nolink&​454x389 ​ |}} +
- +
-Whenever the RTC chip loses all power (including the backup battery) it will report the time as 0:0:0 and it won't count seconds (its stopped). Whenever you set the time, this will kick start the clock ticking. So basically the upshot here is that you should never ever remove the battery once you've set the time. You shouldn'​t have to and the battery holder is very snug so unless the board is crushed, the battery wont 'fall out' +
- +
- +
-==== Setting the time  ==== +
- +
- +
-With the same sketch loaded, uncomment the line that starts with **RTC.adjust** like so: +
- +
-<code C> +
-  // following line sets the RTC to the date & time this sketch was compiled +
-  RTC.adjust(DateTime(__DATE__,​ __TIME__));​ +
-    </​code>​ +
- +
-Thisline is very cute, what it does is take the Date and Time according the computer you're using (right when you compile the code) and uses that to program the RTC. If your computer time is not set right you should fix that first. Then you must press the **Upload** button to compile and then immediately upload. If you compile and then upload later, the clock will be off by that amount of time. +
- +
-Then open up the Serial monitor window to show that the time has been set    {{  http://​www.ladyada.net/​images/​logshield/​RTCsettimegif.gif?​nolink&​459x398 ​ |}}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 +
- +
- +
-<code C> +
- +
- +
-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(now.hour(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(now.minute(),​ DEC); +
-    Serial.print(':'​);​ +
-    Serial.print(now.second(),​ DEC); +
-    Serial.println();​ +
-    </​code>​ +
- +
-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 +
- +
-<code C> +
-    Serial.print("​ since 1970 = "); +
-    Serial.print(now.unixtime());​ +
-    Serial.print("​s ​"); +
-    Serial.print(now.unixtime() / 86400L); +
-    Serial.println("​d"​);​ +
-    </​code>​ +
- +
-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) +
  
 +This tutorial has moved to [[http://​learn.adafruit.com/​ds1307-real-time-clock-breakout-board-kit|http://​learn.adafruit.com/​ds1307-real-time-clock-breakout-board-kit]]
/home/ladyada/public_html/wiki/data/attic/tutorials/learn/breakoutplus/ds1307rtc.html.1334450980.txt.gz · Last modified: 2016/01/28 18:05 (external edit)