DS1307 RTC With Arduino
Instead of being sheepish and using an RTC shield, I decided I’d knock up an RTC module myself, its incredibly simple anyway – just a 32.768KHz crystal, a DS1307 chip and a 3v coin cell battery. It talks I2C so only two wires to connect to the Arduino (plus two for power):
If you’re using another I2C device, you should put a 10k pullup resistor on SDA and SCL, and a 0.1uF capacitor on the Vcc if you’re anal about power smoothing.
I used RTClib (not the Adafruit fork) as although the Teensy library works, it also required using their Time library. I haven’t tried the ds1307new library as it seems a bit dead with only a one-line fix since 2011.
The bit that took me the time (pun intended!) was figuring out that if you use a battery backup, once you set the time from the PC, you then have to re-upload the script with the RTC.adjust()
line commented out, otherwise as soon as you disconnect and reconnect the Arduino, the DS1307 returns the offset from the previous compile time rather than unplug time or re-upload time or something odd….
Here’s the sourcecode with the offending line highlighted:
|
|
Also the Makefile:
|
|