Digital Clock - Part 2

Introduction

This is part of the Arduino-based Digital Clock series. This post is the second post in the series. For the previous post in this series, go to Part 1.

In this post, I will add an LCD display to the previous post’s building blocks. First, I will try the LCD display without the RTC module, and after that I will combine the LCD display and the RTC module with the Arduino Nano.

The LCD Display

The LCD display that I used is a generic 16x2 character LCD display, based on Hitachi HD44780 or compatible chip. I have no way to know the one I am using is genuine Hitachi HD44780 or not, because the chip is bonded directly on the PCB (this is called COB), covered in a blob of black epoxy( this is called “glop-top”).

The hassle free way to control this LCD display with Arduino is by using a I²C module specifically made for it. The benefits of using this module are: it is easy to use and use only 2 pins for I²C communication with Arduino (4 pins if you also count the 5V and ground rails). However in this project I do not want to use it because it adds to the cost, and this is supposed to be a simple project so the inconvenience of extra wirings are tolerable to me.

Before combining the LCD display with the RTC module to the Arduino Nano, I want to try the LCD display first, to make sure that everyhing is fine with it.

For the library, I used Arduino IDE’s built in LiquidCrystal library. For the sketch, I used LiquidCrystal’s HelloWorld example sketch. This example sketch can be accessed from File > Examples > LiquidCrystal > HelloWorld. Or you can get it from the library’s GitHub page. Just like in the previous post, I make no modification and used it as is. I choose this example sketch because:

This sketch prints “Hello World!” to the LCD and shows the time.

What a coincidence.

The pinout is as described in the example sketch comments:

/* ...
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
... */

To adjust the contrast, I used a breadboard potentiometer, 10kΩ as suggested above. I find that the LCD is too dark to see without backlight, even after adjusting the contrast. So in addition to the pinout above we also need to power the LCD display’s backlight:

  • LCD A pin to 5V
  • LCD K pin to ground

This time I will use a breadboard power supply. I am afraid that if I just power everything from USB, too much current will be drawn by the LCD display out of the Nano’s 5V pin.

I set the Arduino IDE’s settings just like the previous post, click the Verify button, and then click the Upload button. No error so far. Great Success!!

For every second that passes, the LCD increments the time displayed by one.

Obviously, this only keep time as long as the Nano is powered. If power were to be cut, the time counter will be reset to 0, If the reset button on the Nano was to be pressed, same thing will happen. That’s why we need the RTC module. So up next: combining this with the RTC module.

RTC Module + LCD Display

There is no ready made example sketch for this. So I will need to arrange the pinouts and write the source code based on the two example sketches I used so far in this project (DS1302_Simple and HelloWorld). After trial and error, this is what I came up with:

#include <ThreeWire.h>  
#include <RtcDS1302.h>
#include <LiquidCrystal.h>

ThreeWire myWire(8, 9, 7); // DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  Rtc.Begin();
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);

  if (!Rtc.IsDateTimeValid()) {
    Rtc.SetDateTime(compiled);
  }
  if (Rtc.GetIsWriteProtected()) {
    Rtc.SetIsWriteProtected(false);
  }
  if (!Rtc.GetIsRunning()) {
    Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled) {
    Rtc.SetDateTime(compiled);
  }
  lcd.clear();
}

void loop() {
  RtcDateTime now = Rtc.GetDateTime();
  printDateTime(now);
  if (!now.IsValid()) {
    lcd.setCursor(0, 1);
    lcd.print("Lost confidence ");
  }
  delay(1000);
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt) {
  char datestring[16];

  snprintf_P(datestring, 
          countof(datestring),
          PSTR("%04u-%02u-%02u"),
          dt.Year(),
          dt.Month(),
          dt.Day());
  lcd.setCursor(3, 0);
  lcd.print(datestring);

  snprintf_P(datestring, 
          countof(datestring),
          PSTR("%02u:%02u:%02u"),
          dt.Hour(),
          dt.Minute(),
          dt.Second());
  lcd.setCursor(4, 1);
  lcd.print(datestring);
}

I took the photo above after pressing the reset button on the Nano. Time is still kept. Great success!! I also power cycled the breadboard power supply, time is still being kept. Great success!!

Note:

  • Rtc By Makuna library cannot use analog pins. No idea why. Update 2021-05-15: This was because I used A7 or A6 pins. Those pins are analog only, meanwhile the RTC module requires digital communication.
  • Fritzing part symbol and schematic symbol for the RTC module is from here.

Next I will add capability to set the date/time using buttons. See you later folks!