Arduino 11: A Character LCD

A character LCD makes user interaction with your projects a lot easier, but programming one can be a bit difficult. Luckily release 17 of the Arduino software has an excellent library, LiquidCrystal.h, which simplifies matters. The LCD must have a driver compatible with the Hitachi HD44780 for the library to work, so check your spec sheet. Such a display will have a 14-pin interface (16 if it is backlit). It’s nice to have the holes for the interface pins all in a line as in the diagram below, so that you can solder a 14 pin header straight across, and the display will neatly pop into a breadboard.

Pin Sym Function Pin Sym function
1 Vss Ground 9 D2 Data bit
2 Vdd +5V 10 D3 “”
3 VL Contrast 11 D4 “”
4 RS Data input 12 D5 “”
5 R/W Read/write-low 13 D6 “”
6 E Enable 14 D7 “”
7 D0 Data bit 15 backlight
8 D1 “” 16 backlight

Check your spec sheet for the pin numbering – the first hole isn’t always 1! Pins 1 and 2 go to ground and +5V to power the board. If your display has a backlight, pins 15 and 16 can go to ground and +5V as well. Pin 3 should go to the middle pin of a potentiometer, whose ends also go to ground and +5V, to control display contrast. All the other pins are directed to digital output pins on the Arduino. Pins 4, 5, and 6 are control pins, and 7 through 14 are data pins. The Arduino library can send data to the LCD in 4-bit mode, meaning it only needs LCD pins 11-14 (data bits 4,5,6 and 7). Therefore pins 7-10 on the LCD can be left unattached, freeing up Arduino pins for other uses.

Example

Note that in the schematic, pins D2, D3 etc refer to the digital output pins on the Arduino (not the data pins on the LCD as in the table above).  The only part used besides the LCD is a 10K potentiometer on pin 3 of the LCD for contrast control.

The program follows.

#include <LiquidCrystal.h>
int rs = 12;
int rw = 11; // or you could ground lcd pin11
int e = 10;
int d4 = 5;
int d5 = 4;
int d6 = 3;
int d7 = 2;
int row = 16;
int col = 2;
LiquidCrystal lcd(rs, rw, e, d4, d5, d6, d7);
void setup() {
   lcd.begin(col, row);
   lcd.print("hello, world!");
}
void loop() {

   for (int n=0; n<10; n++) {
      lcd.setCursor(0,1); //rows and columns start at 0
      lcd.print(n);
      delay(500);
   }
}

Programming Notes

The first line of your program is a call to the library:

#include <LiquidCrystal.h>

The following functions are in the LiquidCrystal library:

LiquidCrystal(rs, rw, e, d4, d5, d6, d7);

Where the arguments rs, rw, etc are the pin numbers on the Arduino that correspond to the specified pins on the LCD. This command goes right after the include statement, and before the void setup() statement. Note the rw pin must also be grounded.

lcd.begin(col, row);

sets the number of rows and columns on the display.

lcd.print(data);

prints the data to the LCD. Data can be a string in quotes, or a variable.

lcd.clear();

clears the lcd and moves the cursor to the top left corner.

lcd.setCursor(c,r);

moves the cursor to column c, row r (columns run 0-15, rows 0-1).

2 responses to “Arduino 11: A Character LCD

  1. Chonsy Baktafari

    Great set of tutorials, maybe a mistake in this one; in your code you set rows to 16, columns to 2 and toward the end specify the LCD operates the other way around.
    Also, I’m a little confused about something… During your loop after printing ‘n’ there is no ‘lcd.clear()’ and the loop replaces the cursor at (0,1), when it prints the next integer, will it overwrite the character in this position or will it fail to write and leave the original character?
    Thanks for all your help otherwise, great and fun introduction to learning Arduino.

  2. Thank you for the tutorial; it’s one of the easiest to read and understand tutorials with the arduino platform.

    I understand I been ask too much. If you can make more tutorials in different Libraries, such as Stepper motor, Wire, I2C Thanks!

Leave a reply to Chonsy Baktafari Cancel reply