Digital Clock - Part 1
I want to make a digital clock. Not from the scratch, I’ll use whatever modules I already have. The easiest way I know is by using an Arduino Nano and a MH Real Time Clock Module 2, which is a module based on DS1302 chips. I choose Arduino Nano because it can be plugged directly on a breadboard. Arduino Nano doesn’t have RTC, hence the necessity of the DS1302 module.
This post is the first in a series. This post is only about testing that the Nano and DS1302 module plays well together. I’m not going to bother about the display for now, that’ll be addressed in other post in this series. In this part 1, for the hardware I used the following:
- Arduino Nano (clone).
- MH Real Time Clock Module 2 (the DS1302 module), with a CR2032 cell. By using the CR2032 cell, the DS1302 chip can keep track of time even when power is cut to the Nano.
Everything will be done on a solderless breadboard, so use of jumper wires is implied. Arduino Nano uses Mini-B USB connector, so use of that cable is also implied for programming and serial communication with a computer.
For the software I used:
- Arduino IDE to program the Nano clone. The IDE serial monitor will be used to show the time from the DS1302 module.
- Rtc library by Makuna. This library knows the communication protocol used by the DS1302 chip. This is how the Nano set the date/time and how it pulls the date/time from the RTC to be displayed in the serial monitor. There are other libraries available in the Arduino IDE for the DS1302 chips, try those if you want.
- DS1302_Simple example sketch from the Rtc library. Once the Rtc library is installed in the Arduino IDE, the DS1302_Simple example sketch can be accessed from: File > Examples > Rtc by Makuna > DS1302_Simple. Or you can get it from the library’s GitHub page. I made no modification to the example sketch and used it as is.
Because for the software I used the example sketch with no modification at all, I only need to heed the comments at the beginning of the sketch:
// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
So I breadboarded everything as told by that comment: VCC to 5V, GND to GND, A2 to RST, A4 to DAT, and A5 to CLK. As usual, I got it wrong. The correct pins are not the analog pins, but rather the digital pins:
- VCC to 5V
- GND to GND
- RST to D2
- DAT to D4
- CLK to D5
In Arduino IDE I set the following:
- Tools > Board > Arduino AVR Boards > Arduino Nano
- Tools > Processor > ATmega328P (Old Bootloader)
- Ports > /dev/ttyUSB0
After that’s done, I clicked the Verify button. No error, Great Success!! Then I clicked the Upload button. No error, Great Success!! Then I clicked the Serial Monitor button. The Serial Monitor window opened showing communication with /dev/ttyUSB0, but it is filled with garbage, an indication that the serial communication speed between the computer and the Nano is not matching. At the bottom right of the Serial Monitor, I set the communication speed to 57600 baud, because that’s the value used in the example sketch. The Serial Monitor began showing messages, so I pressed the reset button on the Nano. After that this is what shows in the Serial Monitor:
compiled: May 1 202121:53:30
05/01/2021 21:53:30
RTC is newer than compile time. (this is expected)
05/01/2021 21:55:47
05/01/2021 21:55:58
05/01/2021 21:56:08
The RTC is working, the DS1302 module is working, the sketch is working. Great Success!!
Now, time to test if the RTC is keeping time even when not receiving power from the Arduino Nano. I disconnected the USB cable from the Arduino Nano, waited some time, and reconnected the cable.
Astute reader will notice that the compile time in the screenshot above is different from the previous output of the Serial Monitor. This is because I’ve recompiled the sketch.
For the next post in this series, I’ll try making the digital clock display the time on an LCD display.