Tuesday 8 March 2016

Arduino Real Time Clock

I needed an accurate real time clock for an Arduino based home automation project; many are I2C controlled and based on Maxim's DS3231 which is achieves an accuracy of about 1 minute per year (2ppm) through temperature compensation - a potentially useful side effect of this is that the part provides a temperature reading. Most boards also include an I2C EEPROM. I found a seller on eBay offering 3 units for under 5 GBP, including next day postage:

Real Time Clock and EEPROM board
Real Time Clock and EEPROM board

The board provides a header with 6 pins:
  • GND - ground
  • VCC - power (3.3V or 5V)
  • SCL - I2C clock
  • SDA - I2C data
  • SQW - programmable interrupt/square wave output
  • 32K - free running 32kHz clock output
There's a holder for a CR2032 coin cell on the reverse, to provide battery backup. Note that these are typically not included with the boards (there are international restrictions on shipping lithium batteries).

I hooked it up to my Arduino Mega 2560 with male-female flying leads: I wired GND and VCC to GND and 5V on the power header, SCL and SDA to their counterparts on the communication header, and the SQW output to the INT3 input.

I then wrote a quick program in to test the board (I prefer to code in C using Atmel's Studio for AVR microcontroller development, for more on this see here.) This used the nice, stable and simple I2C master driver written by Peter Fleury. Note that I ignored the board's EEPROM.

The program prints the current time, day, date and temperature once a second:

       
RTC test
time: 12:14.44   day: TUE   date: 08/03/2016   temperature: +24.00
time: 12:14.45   day: TUE   date: 08/03/2016   temperature: +24.00
time: 12:14.46   day: TUE   date: 08/03/2016   temperature: +24.00


The program uses the Arduino's USB serial port for console I/O, based on Mika Tuupola's recipe here. I use TeraTerm on a desktop Windows PC for this, connected to the Arduino's COM port at 115200 baud.

You can download a ZIP of the Atmel Studio 7 project from here.

No comments:

Post a Comment