The arduino does not have an RTC (Real Time Clock). However, it is trivial to add one. And if you have the time, you can do a number of interesting things with your arduino, limited only by your imagination.
I am using the DS1307 RTC module which looks like this:
It uses a CR2032 3V Lithium coin battery to keep the date and time when there is no power source. It is connected to an arduino via 5V, GND, SCL and SDA pins.
Here is the Fritzing diagram which shows how to wire it up:
Note: This diagram shows the DS1307 module from Sparkfun.com, however the pin wiring is the same (5V, GND, SCL and SDA) for the module I have. You can download the fritzing source HERE.
When this module is first put into use, the date and time needs set. Rather than write your own code to control the DS1307, it is easier to use an existing library. I am using the RTC library from Adafruit. You can download it HERE.
The sample sketches in the RTC libraries usually have a means of initially setting the clock to a specific date and time. And usually it is set to the date and time of the computer which loads its sketches. It is wise to first make sure that computer is synced via NTP to an NTP server.
Also, that time setting code is initially commented out. You need to uncomment (or change) that code to set the specific time desired then upload the sketch. Once the date and time is set you must remember to uncomment the code and upload the sketch a 2nd time. Otherwise, if the original sketch is run each time you run your arduino, the time is reset to the time set in the sketch (not the current time). For example, this line of code:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
gets the date and time from the computer the sketch is compiled on which becomes a hard coded value in the sketch. These values (__DATE__ and __TIME__) are inserted at compile time and not when the sketch is running. Since it takes a few seconds to compile this code, burn it into the arduino and verify it.. the time will already not exactly match the real exact time. Also, since most of these sketches display the time on the serial console, they are prefaced with this line:
while (!Serial); // for Leonardo/Micro/Zero
which means that if you don’t start the serial console right away the time is going to be off further. My solution to these problems is to have a simple sketch that just sets the time and nothing else, like this:
#include <Wire.h> #include "RTClib.h" RTC_DS1307 rtc; void setup () { rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } void loop () { }
Then load the sketch from your imagination that uses the RTC.
Recent Comments