How to Add and Test the LCD Display for Your Arduino Clock Project

Once you’ve programmed and tested the beating heart of your Arduino clock, you need a way to display the time without using the serial monitor. This is where the LCD display comes in.

This one is fairly inexpensive and it uses the very common Hitachi HD44780 driver. These LCD modules are easily recognized because they have 16 pins in a single row and use what’s known as a parallel interface.

Because of this, the Arduino uses several digital pins to make the display work. This process is somewhat complicated, but luckily, there is an Arduino library for it already that makes it very easy to send text to the screen without worrying about the low-level commands that would otherwise be needed.

You use 4-bit mode to display text, which needs only seven Arduino digital pins to control the display. You also need power for the LCD itself, and for the backlight. Finally, you control the contrast of the display by using the potentiometer.

Connect the following:

  1. Add your LCD display and potentiometer to your breadboard.

  2. Connect the power and ground pins on your LCD, which are Pins 15 and 16, respectively.

  3. Connect the ground and power for your LCD’s backlight, which are Pins 1 and 2, respectively.

  4. Connect the control pins for your LCD to the digital pins on your Arduino.

    16x2 LCD Display Pin Arduino Digital Pin
    1 (to GND rail on breadboard)
    2 (to +5V rail on breadboard)
    3 2
    4 3
    5 4
    6 5
    7 (no connection)
    8 (no connection)
    9 (no connection)
    10 (no connection)
    11 11
    12 (to GND rail on breadboard)
    13 12
    14 (to potentiometer middle pin)
    15 (to +5V rail on breadboard)
    16 (to GND rail on breadboard)
  5. Now connect the potentiometer, which controls the display’s contrast.

    The center pin of the potentiometer should go to Pin 14 of the LCD display and the other two pins of the potentiometer are connected to power and ground, in any order.

    image0.jpg

Now that you have connected your LCD, it’s time to make it do something interesting! First you need to upload some code to make sure that the LCD is working properly. This code is the first part of your alarm clock sketch. You build upon it to add all the other functions for your clock.

You can copy the code for the clock all at once, but consider adding it in sections, as described here. That makes it easy to troubleshoot problems and test the clock in stages, as you build it.

Enter the following code into the IDE, or download it from the companion website and upload it to your Arduino:

// Chapter 7: Arduino Alarm Clock
// An alarm clock that uses the Adafruit Industries DS1307 RTC Breakout board
// and a 16x2 Parallel LCD Display
#include <Wire.h>  // I2C Wire Library for communicating with the DS1307 RTC
#include "RTClib.h" // Date and time functions for the DS1307 RTC connected
#include <LiquidCrystal.h> // Display functions for the LCD Display
RTC_DS1307 rtc;       // Create a realtime clock called rtc
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an LCD called lcd
void setup () {
 Wire.begin();  // Enables the communication for the LCD
 rtc.begin();  // Enables the RTC
 lcd.begin(16, 2); // Enables the LCD
 lcd.print(" It's Alive!"); // Print a message, centered, to the LCD to confirm it’s working
 delay(500);     // Wait a moment so we can read it
 lcd.clear();  // Clear the LCD
}
void loop(){
}

When this code is uploaded, you should see the message “It’s Alive!” displayed for a half-second on the LCD. If you don’t see anything, or if the display has garbled characters, you’ve connected something incorrectly. Go back to the wiring table.

The first three lines of this code include the libraries that are be used for your clock. The first includes the I2C library that enables communication with the RTC module. I2C, pronounced “eye-squared cee” or “eye-two-cee,” is a communication link for talking between integrated circuits,in this case your Arduino and the Dallas DS1307 chip.

It’s also useful for communicating with lots of other accessories, such as GPS modules. The useful thing about I2C is that it only requires two pins, plus power and ground. This library makes communication pretty easy with most I2C devices.

The next library is the RTCLib. It’s a version of a library written by JeeLab and modified by Adafruit Industries for communicating with the RTC module. It’s used for getting the time from the RTC module and uses the I2C library to negotiate that communication.

The last library is the LCD display library, which handles the parallel communication with your display. Unlike the RTC library that you added manually, it’s included as a standard library in the Arduino software distribution.

After including the libraries, the code creates two objects: a clock object called rtc and a LiquidCrystal object called lcd. This object has parameters that determine which digital pins the Arduino uses to communicate with the LCD.

After creating those objects, the setup() function gets things going. The I2C, RTCLib, and the lcd, all have to be enabled, which is done by the begin() function. The lcd.begin() function takes two parameters, the number of columns and the number of rows, which on your display are 16 and 2. After this has been set, you can write messages to the screen simply by using the lcd.print() function:

lcd.print(“ It’s Alive!”);

There are two spaces at the beginning of this text, which centers the 11-character message within the 16-character space on the top line. You normally control the position of text with the setCursor() function, but it’s not needed here — one less instruction to put into setup().

After a brief delay so that you can see that it has been printed to the screen, the lcd.clear() function wipes all of the text, ready to go for the main loop().

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.