Arduino and the LiquidCrystal Library

Last Updated: October 20th, 2010 by Chris
Filed under: Physical Computing
Tags:

Synopsis: The Arduino software comes with a built in library for interfacing with Liquid Crystal Displays (LCD). The LiquidCrystal library lets you drive a 4-bit or 8-bit parallel LCD display. This example shows how to wire and program an Arduino and a 16×2 4-bit LCD display (GDM1602K from SparkFun).

Arduino with Adafruit Protoshield and 4Bit LCD from Sparkfun

Arduino with Adafruit Protoshield and 4Bit LCD from Sparkfun

SparkFun carries the cheap (cost, not necessarily quality) GDM1602K 16×2 LCD display (16 characters on 2 lines) with an LED backlight. It has multiple color options (red/black, white/blue, etc.) and runs at 5V (they also have 3.3V versions under a different model number). You need 6 pins on the Arduino to write to the display using 4-bit mode.

As always, be sure to check the datasheet for your display. The pertinent information from the GDM1602K data sheet, and how it can be wired to an Arduino, is in the table below:

LCD Function LCD Pin Arduino
VSS 1 GND
VDD 2 +5V
VO 3 10k – 20k Potentiometer
RS 4 Pin 11
RW 5 GND
Enable 6 Pin 12
D0 7 Not needed for 4-Bit
D1 8 Not needed for 4-Bit
D2 9 Not needed for 4-Bit
D3 10 Not needed for 4-Bit
D4 11 Pin 7
D5 12 Pin 8
D6 13 Pin 9
D7 14 Pin 10
A (Backlight +) 15 +4.2V
K (Backlight -) 16 GND

Wiring Diagram

Note the position of Pin 1 on the LCD. The LCD is upside-down in the diagram below (the pins run along the top of the LCD).

You do need to wire in a potentiometer. If you don’t then the screen does not display any characters. You can get some very faint characters on the screen by pulling down pin 3 on the LCD with a 15k resistor to ground, but the characters are very dim and can only be viewed at odd angles.

The backlight LED calls for +4.2V. You should put a resistor in between pin 15 on the LCD and +5V to reduce the voltage. The LED on the GDM1602k has about 140mA of forward current. So, using trusty Ohm’s law (5V – 4.2V)/.140A we get a 5.7 Ohm resistor, black-blue-black-gold is 6 Ohms @ 5% (or just run it off of the +3.3V rail, it is a little dim, but it works).

Arduino Wired to a 16x2 LCD Display

Using the Liquid Crystal Library

To use the LiquidCrystal library you first define a variable of type LiquidCrystal. The contents of the variable are the pins on the Arduino to which you have wired the corresponding pins on the LCD (rs, rw, enable, etc.). Since we are using the 4 bit version of this function, we do not need to define d1 – d4 (and we don’t need to wire those pins on the LCD either).

LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)

As wired above this translates to:

LiquidCrystal lcd(11, NULL, 12, 7, 8, 9, 10);

Note that the NULL above is for the pin used to defined the Read/Write function of the LCD. Since we are only writing to the LCD, and not reading from it, the RW pin (pin 5 in the LCD diagram above) is wired to ground and we just define that pin as null when we create the LiquidCrystal variable.

Here is the classic example using print() to display Hello World!:

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, NULL, 12, 7, 8, 9, 10);

void setup() {
  lcd.print("Hello World!");
}

void loop() {
}

The LiquidCrystal library also provides these functions:

  • clear() – clear all text on both lines of the LCD
  • home() – move the cursor to the to left of the display
  • setCursor(col, row) – place the cursor at col, row (0,0 is col 1, row 1 and 0,1 is col 1, row 2)
  • write(x) – write a single character
  • print(string) – print a string, long, int, etc.

Tip The cursor stays wherever the last command finished printing/writing. If you use successive print statements, it will just print the content one after another on the LCD. The LCD is only 16 characters wide. But the first line is really 40 characters wide(!). It doesn’t “wrap” to the second line until you have hit the 41st character of the first line. So you can easily print “off the side” of the LCD and the library has no functions that allow for scrolling of the display. That is why setCursor() is your friend :). Use it to wrap the text.

Code Example

Here is a silly program that prints out the first few lines of the Daft Punk song “Stronger”:

#include <LiquidCrystal.h>

LiquidCrystal lcd(11, NULL, 12, 7, 8, 9, 10); //create the lcd variable

void setup() {
  lcd.clear();                    //clear the LCD during setup
  lcd.begin(16,2);                //define the columns (16) and rows (2)
}

void loop() {
  lcd.print("Work It");          //print...
  delay(1000);                   //wait...
  lcd.setCursor(0,1);            //move the cursor to the 2nd line
  lcd.print("Make It");          //print...
  delay(1000);                   //wait...
  lcd.home();                    //set the cursor to top left
  lcd.print("Do it   ");         //print with extra spaces to overwrite
  delay(1000);                   //wait...
  lcd.setCursor(4,1);            //move the cursor to 2nd line, 4th column
  lcd.print("s Us");             //Make It becomes Makes Us
  delay(3000);                   //bust a groove for 3 seconds
  lcd.home();                    //cursor back home
  lcd.print("Harder  ");         //print...
  delay(1000);                   //wait...
  lcd.setCursor(0,1);            //cursor to 2nd line
  lcd.print("Better   ");        //print with extra spaces to overwrite
  delay(1000);                   //wait...
  lcd.home();                    //cursor back home
  lcd.print("Fast");             //print Fast (er still on screen from before)
  delay(1000);                   //wait...
  lcd.setCursor(0,1);            //cursor to 2nd line
  lcd.print("Stronger");         //print...
    delay(1000);                 //wait...
  for(int i = 0; i < 3; i++) {   //print out ... with 1 second delay
    lcd.write('.');
    delay(1000);
  }
  lcd.clear();                   //clear LCD, since we are still on 2nd line...
  lcd.home();                    //set the cursor the top left
}
                    

Additional Resources

As always, if you have a question, comment, correction, etc., then please utilize the comments form below.


Leave a Reply

Login with one of the following services:


(you must login to post comments. Otherwise we get lots of Spammers!