This is only a preview of the October 2016 issue of Silicon Chip. You can view 39 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "El Cheapo Modules From Asia - Part 1":
Items relevant to "Lure & Liquidate Lovelorn Zika Virus Mozzies":
Items relevant to "A New Transformer For The Currawong Valve Amplifier":
Items relevant to "Touchscreen Appliance Energy Meter, Pt.3":
Items relevant to "Two Micropower LED Flasher Modules":
Items relevant to "Voltage/Current Reference With Touchscreen, Pt.1":
Items relevant to "Micromite Plus Explore 100 Module, Pt.2":
Purchase a printed copy of this issue for $10.00. |
Low Cost Asian Electronic Modules
Now they are just standard components!
This is the first of a series of small articles which will help you take full advantage of the wide range
of handy pre-built electronic modules that are now available from Asia. In this article, we review the
DS3231 real time clock (RTC) module.
I
F YOU’VE been reading Silicon
Chip for a while now, you'll have
noticed that small electronic modules
have been creeping into our projects
and the reader circuits published in
Circuit Notebook.
These are not just Micromite, Arduino or Raspberry Pi boards either
but really small and low cost modules
including real time clocks/calendars
(RTC), USB-to-UART serial “bridges”,
UHF data transmitters and receivers,
DDS signal generators, OLED/LCD
panels, touch-screen TFT LCDs, temperature/humidity sensors, microSD
card interfaces and many more. They
seem to be breeding like rabbits!
Many of these modules have sprung
into life initially as “peripherals”
for baby micros like the Arduino (ie,
shields) and Raspberry Pi. But most
of them have a lot of other applications in circuits and designs using
standard TTL or CMOS ICs, and even
in designs using olde-worlde discrete
transistors.
But the really big advantage of this
new generation of pre-built modules
is that most of them are surprisingly
low in cost. In fact, with many of them,
you'll find that the cost of a complete
32 Silicon Chip
Pt.1: By JIM ROWE
module is much less than the price
you'd pay for the main IC chip used
in them.
A prime example is the popular real
time clock/calendar module using
Maxim's very accurate DS3231 RTC
chip — plus a 24C32 4KB EEPROM,
in most cases. Although the module
is usually advertised as intended to
be used with an Arduino, it has a
standard I²C (“Inter-IC”) interface and
can actually be used with most other
micros (we used it with the Micromite
in our Touchscreen Super Clock and
Appliance Energy Meter projects, for
example), as well as in a host of other
designs.
So that's the rationale behind this series of articles on the new “el cheapo”
modules. They're readily available,
often have many applications and
they're usually much cheaper than
building up the same circuits for yourself. As a result, they've now reached
the status of being just standard circuit
components. The Electronic Modules
As Components or “EMAC” revolution
has begun!
Let's start the ball rolling by taking a look at real time clock/calendar
modules.
RTC modules
Probably the first low-cost RTC
modules to appear were those based
on the Philips/NXP PCF8563 chip, a
low-power 8-pin CMOS device which
has an I²C interface but needs an external 32.768kHz crystal. Modules based
on the PCF8563 are still available at
low cost from eBay or AliExpress,
but they tend to be less popular than
modules based on one of two newer
Maxim chips: either the DS1307 or
the DS3231.
Like the PCF8563, the DS1307
needs an external 32kHz crystal.
However, it does have a built-in
power sense circuit which switches
to a backup battery when it detects
a power failure. It also has 56 bytes
of internal non-volatile SRAM and
a standard I²C interface, making it
compatible with just about every type
of microcontroller module such as the
Arduino or the Micromite.
It does have one shortcoming,
though: the time-keeping accuracy
is inclined to drift a little with temsiliconchip.com.au
perature and so it can vary by a few
minutes a month.
Clock/calendar modules using the
DS1307 tend to cost more than those
using the PCF8563, but they often
include extras like a DS18B20 temperature sensor and a 24C32 serial
EEPROM (32Kbits = 4KB). This makes
them quite attractive for applications
where extreme accuracy isn't too
critical.
But modules based on the DS3231
chip are currently the most popular,
partly because the DS3231 has an onchip temperature-compensated crystal
oscillator and crystal.
It also includes an internal temperature-compensated voltage reference
and comparator, both to maintain its
own supply voltage and to automatically switch to a backup supply when
necessary.
These features allow it to provide
significantly higher timekeeping accuracy: better than ±2ppm between 0
and 40°C, or ±2 minutes per year for a
temperature range of -40°C to +85°C.
Its single shortcoming compared with
the DS1307 is that it lacks the internal
non-volatile SRAM.
Despite the advantages offered by
the DS3231, modules using it tend to
cost no more than those based on the
DS1307 or the PCF8563. And this applies for modules like the one shown
in the pictures, which also includes a
24C32 serial EEPROM.
As mentioned earlier, this is the
RTC module that has been used in
a number of recent projects like the
Touchscreen Super Clock, the Appliance Energy Meter and the Micromite
Explore 100, so it's the one we'll now
concentrate on.
DS3231 RTC
As shown in the circuit diagram of
Fig.1, there isn't a great deal in this
module apart from the DS3231 chip
itself (IC1), its 3.6V backup battery and
the 24C32 serial EEPROM (IC2). We'll
discuss the rest of the components and
circuitry shortly after we've looked at
what's inside the DS3231.
Its compact 16-pin small outline
(SO) SMD package contains an I²C data
bus interface, address decoding for
the 18 internal time, date and control
registers, a temperature sensor and a
power control circuit which can swing
over to the backup battery when the
supply voltage (VCC) fails. Its block
diagram is shown in Fig.2.
siliconchip.com.au
100nF
4x
4.7k
CON1
32k
16
SQW
15
SCL
4
SDA
3
VCC
1
GND
10 F
D1
1N4148
2
Vcc
VBAT
SCL
SDA
1k
200
A
A
K
K
LED1
14
IC1
DS3231
RST
3.6V
LI-ION
BATTERY
INT/SQW
32kHz
NC
5–12
GND
13
(RECHARGEABLE)
CON2
SCL
SDA
NOTE: I2C SLAVE
ADDRESS FOR DS3231 IS
D0 (HEX) FOR WRITING,
D1 (HEX) FOR READING
VCC
8
5
6
Vdd
SDA
SCL
IC2
24C32
Vss
4
NC
A2
A1
A0
7
GND
4x
4.7k
3
2
1
(TO SET SLAVE
ADDRESS OF IC2)
A0 A1 A2
I C SLAVE ADDRESSES (HEX) FOR 24C32 EEPROM
2
MSD (FIXED)
A2
A1
A0
WRITE
READ
A
1
1
1
AE
AF
A
1
1
0
AC
AD
A
1
0
1
AA
AB
A
1
0
0
A8
A9
A
0
1
1
A6
A7
A
0
1
0
A4
A5
A
0
0
1
A2
A3
A
0
0
0
A0
A1
DEFAULT ADDRESS
(NO LINKS ON PADS
FOR A0, A1 OR A2)
24C32 ADDRESS BYTE FORMAT
1
0
1
FIXED
0
A2 A1 A0
R
W
SET BY
LINKS
Fig.1: complete circuit for the DS3231-based RTC module. Both CON1 and
CON2 provide serial bus and power connections, allowing extra devices to be
connected. Note that the I2C bus should have only one set of pull-up resistors.
Then there's a complete temperature-compensated 32.768kHz crystal
oscillator (TCXO), followed by a frequency divider chain and all of the
time (seconds/minutes/hours), date
(day of week, day of month, month
and year), alarm, status and control
registers. Finally, there's reset circuitry
plus output buffers for both the 32kHz
TCXO oscillator and the square wave
output when it's enabled.
Note that since the module tracks
the date as well as the time, it is more
correctly described as a real time clock
& calendar (RTCC) module but we'll
stick with the more common RTC term.
As well as the time and date registers, the DS3231 also provides two
time-of-day alarm functions which are
programmable via two sets of dedicated
registers. These can generate an interrupt output signal via pin 3 (INT-bar/
SQW), for feeding directly back to a
micro.
When pin 3 is not being used to
provide this alarm interrupt function,
it can be used to provide square wave
timing signals derived from the 32kHz
TCXO. The square waves can be programmed for one of four frequencies:
1Hz, 1.024kHz, 4.096kHz or 8.192kHz.
These are in addition to the 32.768kHz
signal made available at pin 1.
All of the DS3231's function settings, along with the initial time and
date, can be programmed using the I²C
bus to write into the appropriate internal registers. Then the time, date and
status can be subsequently obtained
by using the I²C bus to read from the
same registers.
Pins 15 & 16 of the device are used
for the I²C bus connections: pin 15
for the SDA serial data line and pin
16 for the SCL serial clock line. On
the module shown, these are both
October 2016 33
32kHz
X1
OSCILLATOR AND
CAPACITOR ARRAY
N
CONTROL LOGIC/
DIVIDER
X2
SQUARE-WAVE BUFFER;
INT/SQW CONTROL
1Hz
VCC
VBAT
TEMPERATURE
SENSOR
POWER CONTROL
GND
INT/SQW
N
ALARM, STATUS, AND
CONTROL REGISTERS
1Hz
CLOCK AND CALENDAR
REGISTERS
SCL
SDA
I2C INTERFACE AND
ADDRESS REGISTER
DECODE
USER BUFFER
(7 BYTES)
VCC
sistors by default, which gives IC2 a
slave address of AE/AF hex (AEh for
writing, AFh for reading). But it also
provides three pairs of pads on the
PCB so that any of the three address
pins can be pulled low (to ground) by
soldering across the A0, A1 or A2 pads.
This allows the slave address of IC2
to be set to any of the eight possible
values, as shown.
So since the slave address of IC1
(the DS3231) is fixed at D0/1 hex (D0
for writing, D1 for reading), there is
no conflict. In fact, the main reason
for changing the slave address of IC2
via the wire links would be to avoid
a conflict with any other devices that
may be attached to the I²C bus.
How it's used
DS3231
VOLTAGE REFERENCE;
DEBOUNCE CIRCUIT;
PUSHBUTTON RESET
RST
N
Fig.2: block diagram for the DS3231. A comparator monitors both VCC and
VBAT and the DS3231 is powered from whichever is higher. The oscillator is
automatically temperature-compensated for accuracy.
provided with surface-mount 4.7kΩ
pull-up resistors to VCC, as are pin 1,
the 32.768kHz output and pin 3, the
INT-bar/squarewave output. (The latter two pins are open-drain outputs,
so they need the external pull-up
resistors.)
That's probably about all you need
to know about the DS3231 itself, apart
from the way that pin 14 (VBAT) is used
for the connection to the 3.6V lithiumion rechargeable backup battery. In the
module shown here, diode D1 and its
series 200Ω resistor are used to maintain the battery charge when VCC is
connected to the module. LED1 and its
series 1kΩ resistor are used to provide
a power-on indicator. We'll have more
to say about battery options later.
Note the two I/O headers, labelled
in Fig.1 as CON1 and CON2. CON1
provides pins for both the 32kHz and
SQW/INT-bar outputs as well as the
SCL/SDA/VCC/GND bus connections,
while CON2 provides only the latter
four connections, essentially to allow
daisy-chaining further devices to the
I²C bus - additional memory chips,
for example.
Now let's look at IC2, the 24C32 serial EEPROM chip which is something
of a bonus. The 24C32 is a 4KB (32Kb)
device, with a standard I²C serial interface. In this module, the SDA line (pin
5) and SCL line (pin 6) are connected
in parallel with those for IC1, to the
module's SDA and SCL lines at both
CON1 and CON2.
To allow IC2 to be addressed by the
micro without conflicting with commands or data sent to or received
from IC1, it has a different slave
address on the I²C bus. In fact, it
can have any of eight different
slave addresses, as set by the
voltage levels of pins 1, 2 and
3 (labelled A0, A1 and A2).
As shown in Fig.1, the
module pulls all three pins
up to VCC via the 4.7kΩ re-
Rear view of the DS3231 module showing
the 3.6V Li-ion backup battery (pin 14) which
powers the real time clock when the supply voltage (VCC) fails.
34 Silicon Chip
Since both the DS3231 and 24C32
devices on the module are intended
for use via the I²C bus, this makes it
easy to use with any micro or other
system provided with at least one I²C
interface. (Even if you don’t have such
an interface, you can use two GPIO
pins in “bit banging” mode, but that’s
outside the scope of this article.)
For example, to use it with an
Arduino Uno or similar all you need
to do is connect the SCL line on the
module to the AD5/SCL pin on the Arduino, the SDA line to the AD4/SDA
pin, the VCC pin to the +5V pin and
the GND pin to one of the Arduino's
GND pins.
It's just as easy with the Micromite.
In this case, the SCL pin connects to
pin 17 on the Micromite's main I/O
pin strip, while the SDA pin connects
to pin 18 next to it. Then the VCC and
GND pins connect to the +5V pin and
GND pins on the same pin strip.
Programming either chip on the
module should also be fairly straightforward, because of the I2C interfacing.
The main thing to remember is that
I2C transactions always begin with a
control byte sent by the master (the
micro), specifying the address of the
slave device it wishes to communicate
with and whether it wants to write to
or read from the device.
So, for example, the control byte to
initiate a write operation to one of the
registers in the DS3231 would be D0h,
while the control byte to read from one
of the addresses in the 24C32 would
be AFh (assuming it's at the default
address on your module).
After the slave device sends back
an "ACK" or acknowledge indication
siliconchip.com.au
(to show that it's present and ready for
a transaction), the micro then sends
the address of the register or memory
location in the device that it wants
to write data to or read it from. Then
when this has been acknowledged,
the actual write or read transactions
can take place.
If this sounds a bit complicated,
you'll be relieved to hear that if you're
using one of the popular micros like
the Arduino or Micromite, you probably don't need to worry about this
yourself. That's because this has usually been taken care of in small code
libraries, with functions specifically
written for I²C data communications.
In the case of the Micromite, in fact,
I²C communication is handled by the
MMBASIC interpreter.
For example, if are using an Arduino, the Arduino IDE application
already includes a "Wire" library, providing about nine different functions
for passing data between the micro
and an I²C device.
Similarly, if you're using a Micromite, you'll find that Geoff Graham's
MMBASIC already includes functions
like RTC SETTIME, RTC GETTIME,
RTC SETREG and RTC GETREG spe-
siliconchip.com.au
cifically for talking to the
DS1307 or DS3231 RTC
devices. And there are
other functions like I2C
OPEN, I2C WRITE, I2C
READ and I2C CLOSE
for data transactions with
other I2C devices (like the
24C32 EEPROM chip in
the current module).
Finally, there's also an automatic variable called MM.I2C, which
can be read after any I2C transaction
to find out the result status.
So all in all, the RTC module shown
with its DS3231 clock/calendar chip
(and bonus 24C32 EEPROM chip) is
relatively easy to use, and exceptional
value for money.
Below is a link to a useful web
tutorial by John Boxall of tronixlabs,
explaining how to use either the
DS1307 or DS3231 RTC modules with
an Arduino:
http://tronixlabs.com.au/news/
tutorial-using-ds1307-and-ds3231realtime-clock-modules-with-arduino
Silicon Chip has two versions of
the DS3231 RTC module available
via our on-line shop. Both come with
mounting hardware; four 6mm M2
Nylon screws and two 10mm M2
tapped spacers, and one comes with
an LIR2032 rechargeable cell already
installed. You can view them at www.
siliconchip.com.au/Shop/7
Note 1: the version supplied with
no cell is designed to use a rechargeable cell. You can use a CR2032 (or
similar) lithium button cell but in
this case, you MUST remove the
on-board SMD diode to prevent the
battery from being charged. See the
Super Clock article in the July 2016
issue for more details.
Note 2: as this module has onboard
pull-up resistors for the I²C bus, you
may need to remove them, or avoid
fitting pull-up resistors on the master, for it to share a bus with other
SC
peripherals.
October 2016 35
|