Real-time clock, DS1302

The DS1302 clock module with a coin cell and a watch crystal, on a three-wire interface of its own. Aliases: DS1302, RTC module, three-wire clock.

It keeps seconds, minutes, hours, date, month, day of the week and year in BCD, as the DS1307 does, plus 31 bytes of RAM, behind three pins: a clock, a data line and an enable.

Pins

Pin What it does
VCC Supply, 2 V to 5.5 V.
GND Ground.
CLK The serial clock (SCLK).
DAT The data line (I/O), both ways.
RST The enable (CE): high for a transfer.

Properties

start is the date and time it holds at power-up, YYYY-MM-DD HH:MM:SS. halted starts it with the clock-halt bit set.

While it runs

The face shows the time, or HALT. S sets it to this machine's clock and starts it.

What the model gets right

From the DS1302 datasheet:

The transfer. RST high starts it and low ends it. A command byte comes first, least significant bit first, each bit taken on a rising edge of CLK; bit 7 must be 1, bit 6 picks RAM, bits 5 to 1 the address and bit 0 a read.

Reads. The data comes out on the falling edges, starting with the falling edge that ends the command, so a sketch lets go of DAT before then.

Burst mode. Address 31 walks all eight clock registers, or all 31 RAM bytes, in one transfer; a clock burst write is taken only once all eight are in.

Write protect. Bit 7 of the control register, set, blocks every other write.

The hours. Bit 7 of the hours register is the 12-hour form (bit 6 on the DS1307), with bit 5 for PM.

Pull-downs. About 40 kohm on each of the three pins inside the chip.

What it does not model

The trickle charger (its register is stored and read back), the backup cell, and the undefined state of a brand-new chip: the model starts at start, with write protect off.

Libraries

Rtc by Makuna (ThreeWire.h and RtcDS1302.h): ThreeWire myWire(DAT, CLK, RST); RtcDS1302<ThreeWire> Rtc(myWire);

Common mistakes

Wiring it like an I2C clock. The DS1302 is not on the I2C pins; any three pins will do.

Mixing up the order in ThreeWire: it is the data pin, then the clock, then the enable.

See it in action

Alarm clock shows the time on a TM1637 display and plays an alarm on a piezo until the button is pressed. Open it at /templates.