In many  Arduino or Raspberry pi projects we need to display some messages or text regarding the project’s behavior. Suppose you are making a ‘Digital Thermostat’ that can show both the Temperature and Humidity data from the DHT11 sensor, so in that case for digitally showing these data to the user we need a display that can fetch the data from Arduino and display it on its screen.

Here in this tutorial you will learn how to interface and use a 16X2 LCD with Arduino.

There are two types of 16X2 LCD currently available in the market. One is the native one that have 16 pins and other comes with I2C communication inbuilt. We will talk about them one by one in full details.

LCD Pinout:

 

There are total 16 Pins.

  • First one from left hand side is the Ground(GND) pin. The second pin is the Vcc which we connect to the 5 volts pin of Arduino.
  • Vo pin is for the brightness for the LCD display, on which we can attach a potentiometer for controlling the contrast of the display.
  • The RS pin( Register Select pin) is used for selecting whether we will send commands or data to the LCD. For example if the RS pin is set on High state or 5 volts we are sending data or characters to the LCD and if it is set on low state or zero volts, then we are sending commands to the LCD like: set the cursor to a specific given location, clear the display, turn on-off the display and so on.
  • R/W pin selects the mode whether we want to read or write on the LCD.
  • E pin enables the writing to the registers.
  • D0 to D7 : 8 data pins are use for sending the 8 bits data when we are writing to the registers. For example if we want to see the latter uppercase A on the display we send 0100 0001 to these 8 data pins for writing to the registers according to the ASCII table.
  • A and K pins are for LCD inbuilt back light i.e. Anode and cathode pins respectively.

Here, in this tutorial we will be using only the 4 data pins for writing to only 4 registers. So, we are going to use it in 4 bit Mode.

Connection with Arduino:

Here, Potentiometer is not compulsory to use, it is just for adjusting the brightness.

Basics of LCD Programming:

LCD Programming include a pre-installed LCD library i.e. LiquidCrystal.h that needs to be included in the program for the following LCD function to work.

Here are the list of some of the most common functions along with their uses and briefings.

1. LiquidCrystal()

The LiquidCrystal() function sets the pins which Arduino uses to connect with the LCD. The parameters passed inside the parentheses are in this order:

LiquidCrystal(RS, E, D4, D5, D6, D7)

For example, if we want LCD pin D4 to connect to Arduino pin 3. Just put “3” in place of D7 in the function like this: LiquidCrystal(RS, E, 3, D5, D6, D7). And do this similarly for the other corresponding pins.  This function needs to be placed before the void setup() section of the arduino program.

2. lcd.begin()

Since, here we are using 16X2 LCD screen that can support 32 characters on it in two rows with 16 characters in each of these 2 rows. So, while programing we need to specify the Arduino about the dimensions of the LCD. Therefore we use this function to set the dimensions. The number of rows and columns are specified as lcd.begin(columns, rows). For 16×2 LCD, we would use lcd.begin(16, 2).

3. lcd.setCursor()

It places the cursor (and any printed text) at any specified position on the screen. It can be used in the void setup() or void loop() section of your program.

The cursor position is defined with lcd.setCursor(column, row). The column and row coordinates start from zero (0-15 and 0-1 respectively). For example, using lcd.setCursor(4, 1) in the void setup() section of the “Roboshala India” program above prints “Roboshala India” to the lower line and shifts it to the right four spaces:

4.  lcd.Cursor()

This function is used for  creatng a visible cursor. For turning Off the cursorthe function to be used is  lcd.noCursor().Note that both the  lcd.cursor() and lcd.noCursor() functions can be used together in the void loop() section to make a blinking cursor similar to what you see in many text input fields.

5. lcd.display()

This function is used for turning on any text or cursors that have been printed to the LCD screen. The function lcd.noDisplay() turns off any text or cursors printed to the LCD, without clearing it from the LCD’s main memory.

6. lcd.print()

This function is used for printing message to the LCD and can be used in both the void setup() or the void loop() section of the program.

There are different ways  for printing messages which depends on its type.

For letters and words, place quotation marks (” “) around the text.
For example: lcd.print(“Roboshala!”)
For numbers, no quotation marks are needed. For example : lcd.print(123456789)
lcd.print() can print numbers in different forms like decimal, binary, hexadecimal, and octal bases also. For example:
  1. lcd.print(100, DEC) : prints “100”
  2. lcd.print(100, BIN) : prints “1100100”
  3. lcd.print(100, HEX) : prints “64”
  4. lcd.print(100, OCT) : prints “144”

 

There are many other functions also and you can learn about them by going through the examples code under LCD examples code in Arduino IDE.

Code Section:

/*
  Basic LCD tutorial for Arduino Beginners.
  Visit: www.youtube.com/roboshala for Video tutorials.
* www.roboshala.com
*
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Assign the connection pins to LCD | Parameters: (rs, enable, d4, d5, d6, d7) 
void setup() 
{ 
 lcd.begin(16,2); // Initializes the LCD interface , and specifies the dimensions (width and height) of the display 
}
void loop() { 
 lcd.print("www.roboshala.com"); // Prints "www.roboshala.com" on the LCD 
 delay(3000); // 3 seconds delay 
 lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed 
 lcd.print("Best Tutorial"); // Prints "Best Tutorial" on the LCD 
 delay(3000); 
 lcd.clear(); // Clears the display 
 lcd.blink(); //Displays the blinking LCD cursor 
 delay(4000); 
 lcd.setCursor(7,1); 
 delay(3000); 
 lcd.noBlink(); // Turns off the blinking LCD cursor 
 lcd.cursor(); // Displays an underscore (line) at the position to which the next character will be written 
 delay(4000); 
 lcd.noCursor(); // Hides the LCD cursor 
 lcd.clear(); // Clears the LCD screen 
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here