switch to backup power when the main power fails.
Setting and Reading the Time
Time keeps marching on — which makes it
difficult to read or set the time over an I2C
interface. You need to find a way to “freeze” time
in order to read it or set it. If you don’t freeze time,
you risk time “rolling over.” Let’s say you start
reading the time at 11:59: 59 pm on 15 June, and
finish reading the date at 12:00 pm 16 June — an
operation that could take milliseconds. Your
resultant time will look like 11: 59 pm on 16 June —
a whole day out!
When reading the time, the RTC notices you’re
accessing the registers and copies the instantaneous
time into a buffer. To make sure that you read a
“frozen” copy of time from this buffer, you must
read all the time elements you need in one
operation (i.e., before you issue a Stop condition).
The same risk exists when setting the time.
When setting the time, you must first halt the
oscillator (setting ST on RTCSEC to “0”). Now that
the clock has stopped, you can set the time before
starting the oscillator ticking again (setting ST on
RTCSEC to 1). Always check it has started up again
(read the RTCWKDAY register and check the
A Lot to Take In
We’ve covered a lot of ground in this article,
and at quite a pace — not only the basics of I2C, but
also the workings (and quirks) of working with
EEPROM and RTC modules. I hope it has been
useful. We’ve developed a fairly substantial little
project that could be expanded in any number of
ways. Thanks for joining me again, keep the
feedback coming, and I look forward to some more
exploration next time. NV
Behind the Scenes
I have to confess that projects of this size test my
breadboarding patience! Too many jumper wires crossing, counting
the MCU pins incorrectly too often, troubleshooting bad
connections, finding jumpers mysteriously pulled out, and generally
spending more time guarding my project than developing it!
Figures A and B show how I worked on this project to get it
running quickly — a series of boards I developed to make my life
easier (and, of course, fun). The entire temperature logger is
contained in the three PCBs and the few components on the
breadboard. I was chatting with the team at Nuts & Volts, and we
thought that these boards could also make reader’s lives easier.
Keep an eye on the Nuts & Volts store where we’ll put a few up to
see if there’s interest. The main board is called a “Toadstool” and
the two plug-in modules are called “Caps” — the Toadstool
contains the ATmega328P, and the two removable caps you see
here contain the MCP79400 and the 24LC128.
Let us know your thoughts!
Figure A: Temperature Logger using a Toadstool.
Figure B: Stacking the caps: EEPROM and RTC
on the Toadstool.
September 2015 61
Projects here are based on the build
from "Beyond Arduino #1" in the March
edition of Nuts & Volts. The following
additional components are needed for
the Temperature Logger project:
U1 LM35 Temperature Sensor
U2 Microchip 24LC128
U3 Microchip MCP79400 RTC
R1, R2, R3 10K Resistor
R4, R5 2.2K Resistor
Y1 32.768 kHz Tuning
C1, C2 8. 2 pF Ceramic Capacitor
C3 100 nF Ceramic Capacitor
kit to go with
or call our