Position: Index > Display Circuit >

Interfacing LCD to Arduino – Display Text and Char

2016-12-20 11:57  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

A Liquid Crystal Display commonly abbreviated as LCD is basically a display unit built using Liquid Crystal technology. When we build real life/real world electronics based projects, we need a medium/device to display output values and messages. The most basic form of electronic display available is 7 Segment display – which has its own limitations. The next best available option is Liquid Crystal Displays which comes in different size specifications. Out of all available LCD modules in market, the most commonly used one is 16×2 LCD Module which can display 32 ASCII characters in 2 lines (16 characters in 1 line). Other commonly used LCD displays are 20×4 Character LCD, Nokia 5110 LCD module, 128×64 Graphical LCD Display and 2.4 inch TFT Touch screen LCD display.

In this article, we are going to learn how to interface lcd to arduino with 2 examples – one being interfacing a 16×2 LCD module to Arduino and the other being interfacing a 20×4 LCD module to Arduino.

Interfacing  16×2 LCD to Arduino uno

LCD modules form a very important part in many arduino based embedded system designs. So the knowledge on interfacing LCD module to arduino is very essential in designing embedded systems. This section of the article is about interfacing an Arduino to 16×2 LCD. JHD162A is the LCD module used here. JHD162A is a 16×2 LCD module based on the HD44780 driver from Hitachi. The JHD162A has 16 pins and can be operated in 4-bit mode (using only 4 data lines) or 8-bit mode (using all 8 data lines). Here we are using the LCD module in 4-bit mode. First, I will show you how to display a plain text messages on the LCD module using arduino and then  I have designed a useful project using LCD and arduino – a digital thermometer. Before going in to the details of the project, let’s have a look at the JHD162A LCD module.

16×2 LCD Module Pin Out Diagram

 The JHD162A lcd module has 16 pins and can be operated in 4-bit mode or 8-bit mode. Here we are using the LCD module in 4-bit mode. Before going in to the details of the project, let’s have a look at the JHD162A LCD module.The schematic of a JHD162A LCD pin diagram is given below.

JHD162ALCD module arduinoThe name and functions of each pin of the 16×2 LCD module is given below.

Pin1(Vss):Ground pin of the LCD module.

Pin2(Vcc): Power to LCD module (+5V supply is given to this pin)

Pin3(VEE):Contrast adjustment pin. This is done by connecting the ends of a 10K potentimeter to +5V and ground and then connecting the slider pin to the VEE pin. The voltage at the VEE pin defines the contrast. The normal setting is between 0.4 and 0.9V.

Pin4(RS):Register select pin.The JHD162A has two registers namely command register and data register. Logic HIGH at RS pin selects data register and logic LOW at RS pin selects command register. If we make the RS pin HIGH and feed an input to the data lines (DB0 to DB7), this input will be treated as data to display on LCD screen. If we make the RS pin LOW and feed an input to the data lines, then this will be treated as a command ( a command to be written to LCD controller – like positioning cursor or clear screen or scroll).

Pin5(R/W): Read/Write modes. This pin is used for selecting between read and write modes. Logic HIGH at this pin activates read mode and logic LOW at this pin activates write mode.

Pin6(E): This pin is meant for enabling the LCD module. A HIGH to LOW signal at this pin will enable the module.

Pin7(DB0) to Pin14(DB7):  These are data pins. The commands and data are fed to the LCD module though these pins.

Pin15(LED+): Anode of the back light LED. When operated on 5V, a 560 ohm resistor should be connected in series to this pin. In arduino based projects the back light LED can be powered from the 3.3V source on the arduino board.

Pin16(LED-): Cathode of the back light LED.

For knowing more about LCD module JHD162A and its pin functions, read this article: Interfacing 16×2 LCD and 8051 microcontroller. The circuit diagram of interfacing LCD to arduino for displaying a text message is shown below.

Circuit diagram – Arduino to 16×2 LCD Module

interfacing LCD and arduinoRS pin of the LCD module is connected to digital pin 12 of the arduino. R/W pin of the LCD is grounded. Enable pin of the LCD module is connected to digital pin 11 of the arduino. In this project, the LCD module and arduino are interfaced in the 4-bit mode. This means only four of the digital input lines( DB4 to DB7)  of the LCD are used. This method is very simple, requires less connections and you can almost utilize the full potential of the LCD module. Digital lines DB4, DB5, DB6 and DB7 are interfaced to digital pins 5, 4, 3 and 2 of the Arduino. The 10K potentiometer is used for adjusting the contrast of the display. 560 ohm resistor R1 limits the current through the back light LED. The arduino can be powered through the external power jack provided on the board. +5V required in some other parts of the circuit can be tapped from the 5V source on the arduino board. The arduino can be also powered from the PC through the USB port. The full program for interfacing LCD to arduino is shown below.

Program – Arduino to LCD
#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // sets the interfacing pins

void setup()
{
 lcd.begin(16, 2);  // initializes the 16x2 LCD
}

void loop()
{
  lcd.setCursor(0,0);           //sets the cursor at row 0 column 0
  lcd.print("16x2 LCD MODULE"); // prints 16x2 LCD MODULE
  lcd.setCursor(2,1);           //sets the cursor at row 1 column 2
  lcd.print("HELLO WORLD");     // prints HELLO WORLD
}
About the program.

To facilitate communication between Arduino and LCD module, we make use of a built in library in Arduino <LiquidCrystal.h> – which is written for LCD modules making use of the Hitachi HD44780 chipset (or a compatible chipset). This library can handle both 4 bit mode and 8 bit mode wiring of LCD.

Refer the – documentation of LiquidCrystal Library – before you continue down!

Library  “LiquidCrystal.h” is used for easily controlling the LCD module using Arduino board with the help of built in methods defined inside the library For example, data string can be printed on the LCD module by merely calling a method lcd.print(). If you want to print “Hello World” at row 1, starting from column 3; first set the cursor at the desired position using method lcd.setCursor(1,3) and then write the command to print the characters as lcd.print(“Hello World”); – got it? The library is readily available with the Arduino IDE (as its a pre installed standard library).  Any library can be accessed manually through the “Import library” in the “sketch” tab in the main menu bar. The LiquidCrystal.h library provides functions/methods for almost all applications like printing a string, setting the cursor, initializing the LCD, scrolling the display, auto scroll, clear LCD, blink cursor etc.

Other Important aspects of Program

LiquidCrystal lcd() – is  a constructor used to declare a variable of its type. Here ‘lcd’ is the variable declared using the constructor and is used to invoke methods defined inside the library LiquidCrystal.h (Example – lcd.print(); lcd.setCursor() and other methods)

lcd.begin() – is called to initialize the lcd screen and to pass the dimension of lcd screen (columns, rows) as parameters of the invoked method.

Program for scrolling the LCD screen using Arduino.

A simple program for scrolling a text message on the LCD screen using arduino is shown here. This is done using the “scroll()” method defined inside LiquidCrystal.h library. For example the method “lcd.scrollDisplayRight()” will scroll the display to right and the method”lcd.scrollDisplayLeft()” will scroll the display to left. A “for” loop is used for selecting the number of positions to scroll at a time. In the program shown below, it is chosen to be 2 because the text to be displayed is comparatively long. For shorter texts more number of positions must be scrolled at a time to get a smooth display.

#include <LiquidCrystal.h>
int pos=0; // variable to hold cursor position

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
 lcd.begin(16, 2);                              //initializes 16x2 LCD
 lcd.print("16x2 LCD MODULE & ARDUINO-UNO");   //text to display
}

void loop()
{
  for(pos=0; pos<2; pos++)  
  {
lcd.scrollDisplayLeft();    //scrolls display left by two positions
  }  
  delay(800);              //sets the speed at which display moves
}

Note:- This interfacing tutorial is good enough to learn interfacing of other dimensions of line LCD screens (say 8×1, 8×2, 16×1, 16×3, 16×4, 20×4, 20×2, 40×2, 40×4 etc) which are manufactured using the Hitachi HD44780 chipset. The pin configuration of all the line LCD screens (based on HD44780 chipset) is very same. This means the same circuit diagram is enough to interface other size lcd screens to arduino. We have explained more about this in the section given below – on 20×4 LCD to Arduino Interfacing

Digital thermometer with LCD display using arduino.

This is just a practical implementation of the interfacing of LCD and Arduino. A simple digital thermometer using arduino and 3 digit seven segment display had been already published here. You can find that article here: Digital thermometer using arduino. Read this article before attempting the LCD version. LM35 is the temperature sensor used in this project. It is a three terminal linear analog temperature sensor. The output voltage of the LM35 increases 10mV per °C rise in temperature and the range is from -55°C to +155°C. The circuit diagram of the LCD thermometer using arduino is shown in the figure below.

Circuit diagram: LCD thermometer.

LCD thermometer using arduinoThe LM35 temperature sensor is interfaced to the analog input pins of the arduino. Vcc pin (pin 1) of the LM35 is connected to A0 pin of the arduino. Output pin (pin 2) of the LM35 is connected to A1 pin of the arduino. GND pin (pin 3) of the LM35 is connected to A2 pin of the arduino. The complete program of the LCD thermometer using arduino is given below.

Program: LCD thermometer.
#include <LiquidCrystal.h>

int vcc=A0;
int sensor=A1;
int gnd=A2;
float temp;
float tempf;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  pinMode(vcc,OUTPUT);
  pinMode(sensor,INPUT);
  pinMode(gnd,OUTPUT);
  digitalWrite(vcc,HIGH);   // Vcc for LM35
  digitalWrite(gnd,LOW);    // Ground for LM35
  lcd.begin(16, 2);         // initializes the 16x2 LCD
  lcd.setCursor(2,0);       // sets the cursor at column 2 row 0
  lcd.print("TEMPERATURE"); // prints temperature
}

void loop()
{

  temp=analogRead(sensor); // reads the sensor output
  temp=temp*5;             // converts the sensor reading to temperature
  temp=temp/10;            // adds the decimal point
  tempf=(temp*1.8)+32;     // converts to Fahrenheit 

  lcd.setCursor(0,1);   // sets cursor at column 0 row 1
  lcd.print(temp);      // prints temperature in degree Celsius
  lcd.print((char)223); // prints degree sign
  lcd.print("C");       // prints letter c
  lcd.setCursor(8,1);   // sets cursor at column 8 row 1
  lcd.print(tempf);     // prints temperature in degree Fahrenheit
  lcd.print((char)223); // prints degree sign
  lcd.print("F");       // prints letter F
  delay(1000);          // 1 second delay
}

Okay! We have seen how to interface a 16×2 LCD Module to Arduino and we have also learned how to build a practical application like Digital Thermometer using Arduino and LCD module. Now lets move on to interface a 20×4 LCD module with Arduino!

Interfacing Arduino and 20×4 LCD Module

Lets first analyse the pin out diagram of 20×4 LCD module, as given in the image below.

Pin Out Diagram - LCD Module

The 20×4 LCD module pin out diagram is very much same as the 16×2 LCD module pin out diagram. It is same with the number of pins, order of pins and the purpose of pins. So the interfacing circuit diagram is also very same as the 16×2 LCD module with Arduino.

Note:- The only changes you might need to make in the circuit diagram is with the current limiting resistor connected to backlight LED at pin 15 and with the potentiometer setting connected to VEE (the contrast levels of 16×2 and 20×4 modules might vary with a select potentiometer value). Rest all are very similar to interfacing a 16×2 LCD to Arduino.

Circuit Diagram – LCD to Arduino

Since the pin out structure of many popular line LCD modules like 16×2, 16×1, 20×4, 20×2, 40×2 using the Hitachi driver are similar, the circuit diagram to interface these line LCD modules to Arduino remains common. To interface 20×4 LCD to Arduino, we can use the exactly same circuit diagram of interfacing 16×2 LCD to Arduino.

Note:- The back light LED and its current requirement may vary with different types of LCD modules. The brightness you get for 16×2 LCD using a 560 ohm current limiting resistor will not be the same you get for a 20×4 LCD (or other variants like 20×2 or 40×2 or 16×1 etc). So you will have to adjust the values of current limiting resistor to suit the brightness you desire. Another change you might need to make is with the potentiometer setting connected at VEE pin which determines the contrast of LCD. The contrast you get for 16×2 LCD with a particular pot setting may not be the same for 20×4 LCD or other line LCD types (say 16×1 or 20×3 or 40×2)

So there are no changes required to make in the circuit diagram other than what is mentioned in the ‘note’ above. Let’s get to the coding part. The commands are very same as what we have written for 16×2 LCD. The only difference is in the setup() part of the arduino program, where we declare the number of columns and rows (lines) of LCD module. This declaration is what makes the program to understand the type of LCD module (number of columns and lines of modules) used in hardware.

Code – LCD to Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // sets the interfacing pins

void setup()
{
 lcd.begin(20, 4);  // initializes the 20x4 LCD
}

void loop()
{
  lcd.setCursor(0,0);           //sets the cursor at line 0 column 0
  lcd.print("20x4 LCD MODULE"); // prints characters - 20x4 LCD MODULE
  lcd.setCursor(5,3);           //sets the cursor at  line 3 column 5
  lcd.print("HELLO WORLD");     // prints HELLO WORLD
}

Okay! We have finished our interfacing tutorial and we learned how to interface arduino to LCD. If you have any doubts or you come across any problems while interfacing, please ask in comments section. In the meantime, we have the following tutorials – which you may like to read.

Interface Arduino to 7 Segment Display – learn how to interface 7 segment display to arduino with examples on interfacing 1 digit seven segment display (common cathode and anode versions) and 4 digit seven segment display (common cathode and anode versions).

Interface LCD to 8051 – learn how to interface LCD module to 8051 micro controller and display text messages on lcd screen.