Sometimes you want to give more information to the user of your devices than what can be conveyed with a couple of LEDs. For that an alphanumeric LCD is a very convenient solution. These displayes can show text and very simple graphics. There are many different sizes and colors but the most common ones have 2 lines with 16 characters , or 4 lines 20 characters and are blue/white or green/black. Some also have built-in background light making it possible to read the display when it’s dark. These displays usually have a large number of connectors for controlling them, but there exists very cheap modules with a PCF8574-chip that can be soldered to the back of the display giving you a I²C-interface instead so that the while display can be controlled using only to wires.

Code library

In this example we use a display with 16×2 characters and background light with a I²C-module, but the code is easily adaptable for other displays. The only requirement is that the display uses a control chip that us compatible with the very popular Hitachi HD44780-chip. The code library we use is not included in the default Arduino environment, but it’s very easy to install using only a few mouse cliks. Start the Arduino development environment program and go to the menu Sketch > Include library > Manage Libraries… This opens the library manager and here you can search for the library “LiquidCrystal_PCF8574”. Once you’ve found it you click on it and click the install button. Once the library has been downloaded an installed you can close the library manager. You only need to do this once, and the library will then always be available on this computer.

The circuit

Connect the display to the Arduino board as below:

Arduino-project 006 – Display text on a LCD

Description of the code

The code doesn’t contain any real surprises. First we include the code libraryfor controlling the display and create an object for the display that keeps track of settings and provides all functions needed, such as write text, erase text and activating the backlight. In setup() we start the display and make all preparations, such as clearing the display, turning on the backlight and write a first line of text. The main program consists of a for-loop that defines a variabel i that is set to 10. It then executes its code and decreases i with 1 (i--) and if i is still greater than zero the cde is executed once more. This means that the code inside the for-loopen will run 11 times and count from 10 to 0. Each time around the for-loopen we erase the second line on the display and print out the value of the counter, i.e. the i variable and then wait for one second. When the for-loopen is done we clear the display completely and print out a new message. The last thing happening is that we enter a while-loop with a constant true value and without any code. This is something of a special construction which makes the micro controller hang and prevents it from doing anything else until you reset it using the reset button or disconnect the power.

The code

// Include the library for controlling ASCII-displays via I2C
#include <LiquidCrystal_PCF8574.h>

// Create object for controlling the display.
// Use I2C-address 0x27 (standard)
LiquidCrystal_PCF8574 display(0x27);

void setup() {
  // Start the display and specify the size - 16 characters in 2 lines.
  display.begin(16, 2);
  // Clear all old text (if any).
  display.clear();
  // Place the cursor in the upper left corner.
  display.home();
  // Turn on the backlight of the display and set it to max brightness.
  display.setBacklight(255);
  // Print text on the first line
  display.print("Countdown...");
}

void loop() {
  for (int i = 10; i > 0; i--) {
    // Move the cursor to the beginning of the second line (character 0, line 1)
    display.setCursor(0, 1);
    // Clear the line by writing 16 spaces.
    display.print("                ");
    // Move the cursor back to the beginning of the line.
    display.setCursor(0, 1);
    // Print the counter value
    display.print(i);
    // Wait one second
    delay(1000);
  }
  // Clear the display
  display.clear();
  // Set the cursor a small way into the first line (character 4, line 0)
  display.setCursor(4, 0);
  // Print out the final text.
  display.print("KA-BOOM!");
  // Halt the program
  while (1);
}

Pin It on Pinterest

Share This