Add 16×2 LCD Display to your Arduino Projects

Back to Blog
16x2-LCD-Arduino

Add 16×2 LCD Display to your Arduino Projects

16×2 LCD is an alphanumeric display that can show up to 32 characters on single screen. You can display more characters by scrolling the texts one by one.

It can be used with all Microcontroller boards like 8051, AVR, Arduino, PIC, and ARM Microcontrollers.

Most projects require an LCD display to communicate with the user in a better way. Some projects may require displaying warnings, errors, Sensor values, State of the input and output device, Selecting different modes of operations, Time and date display, Alert message and many more. This will give the project a better view and its operation in a more visual way.

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in the 5×7 pixel matrix.

LCD Pinout:

There are totally 16 pins in an LCD Display. You can use directly all the pins in 8-bit mode with Arduino or 12 pins using 4-bit mode or you can use an I2C module for LCD and multiplex it into just 4 pins. The choice is depending upon the project’s pin requirements. The details of the pins are given below.

 

VSS/GND – Ground

VCC/VDD – Power Supply (5v)

V0 – Brightness Control using Potentiometer

RS – Register select. Specify what we are sending Command or Data. Sets to 0 for Command mode like setCursor, LCD Clear, TurnOFF LCD. Set 1 for data mode like sending Data/Characters.

R/W – Read/Write. Mostly we are writing data/characters to the registers.

E – Enable writing to Registers.

D0 to D7 – Data pins. Send 4bit/8bit data to display characters. The Arduino library provides 4bit and 8bit mode. The data will be in ASCII format.

A/LED+ – Anode (Backlight LED)

K/LED- – Cathode (Backlight LED)

 

Circuit diagram:

LCD with Arduino 4 bit_bbCircuit diagram for 4bit mode

 

LCD DisplayConnected to
VSS/GNDGND
VDD+5V
V0Potentiometer Output pin
RSArduino pin 2
RWGND
ENArduino pin 3
D0No Connection
D1No Connection
D2No Connection
D3No Connection
D4Arduino pin 4
D5Arduino pin 5
D6Arduino pin 6
D7Arduino pin 7
LED ++5v
LED –GND

 

Buy 16×2 LCD Display

The D4-D7 pins are connected to Pin 4,5,6 and 7th pin of Arduino Uno. Potentiometer output is connected to the V0 pin of LCD for brightness control. RS pins to Pin 2 and Enable pin to Pin 3 of Arduino. The power supply and LED have connected accordingly.

Using LiquidCrystal library with Hardware:

Arduino included the library for LCD display called LiquidCrystal.h in its IDE. This library can work in both 4 bit and 8-bit mode. In this example, we will see 4-bit mode and see how to use it with Arduino library. According to the connection, the pin numbers are changed according to different modes. The functions for corresponding modes are given below.

4-bit mode:

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

8-bit mode:

LiquidCrystal lcd(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7); 
LiquidCrystal lcd(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);

We need to replace the symbols with Arduino pins connected with LCD. As per the circuit made we have setup that matches the following function

LiquidCrystal lcd(rs, enable, d4, d5, d6, d7);

So we are replacing it as

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

Initialize the LCD:

After defining the pin numbers we need to initialize the LCD. For that we need to use the following function in setup loop.

lcd.begin(16,2);  //lcd.begin(columns,rows);

Here the 16×2 denotes the 16 Characters and 2 Rows.

Complete Code:

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Parameters (rs, enable, d4, d5, d6, d7)



void setup() {

 lcd.begin(16,2); // Initializes the 16x2 LCD screen

}



void loop() {

 lcd.print("Factory"); // Prints "Factory" on first row

 delay(2000); // 2 seconds delay

 lcd.setCursor(6,1); // Sets cursor(column,row) positions.

 lcd.print("Forward"); //Prints ‘Forward’ on 2nd row 6th column.

 delay(3000);

 lcd.clear(); // Clears the display

 lcd.blink(); //Displays the blinking LCD cursor

 delay(4000); //wait for 4 seconds

 lcd.setCursor(15,1); //Moves the blinking cursor to last position

 delay(3000);

 lcd.noBlink(); // Turns OFF the blinking LCD cursor

 lcd.cursor(); // Prints an underscore symbol at (15,11)

 delay(4000);

 lcd.noCursor(); // Clears the LCD cursor  only

 lcd.clear(); // Clears entire LCD screen

}

LCD Functions:

To print a string we use lcd.print() function with string in its parameters. This prints ‘Factory’ string in the 1st row and ‘Forward’ in the 2nd row.

Important Note: The columns and rows position starts with 0th location.

Example:  7th column and 2nd row is mentioned as (6, 1).

lcd.print():

This function is used to print a message on LCD.

lcd.print("Factory"); // Prints "Factory" on first row

delay(2000); // 2 seconds delay

lcd.setCursor(column,row):

This function sets the cursor on 7th column and 2nd row. Printing the string will gets displayed from this location on LCD.

 lcd.setCursor(6,1); // Sets cursor column and row position

 lcd.print("Forward"); //Prints ‘Forward’ on 7th column of 2nd row.

lcd.clear():

The LCD will hold the previous data on its registers. You need to manually clear it using the lcd.clear() function.

lcd.clear();

Blinking Full Cursor:

By Using lcd.blink() function we can make the cursor blinking on LCD. To turn off the blinking cursor we use lcd.noBlink() function.

lcd.blink(); //Blinking cursor

lcd.noBlink();  //Turns OFF blinking cursor

Underscore Cursor:

Use lcd.cursor() function for printing an underscore symbol. It is also used for notifying users to enter some values.

lcd.cursor(); // Prints an underscore symbol

lcd.noCursor(); Clears the underscore only.

Create your own Custom characters:

Each character in LCD is made up of 5×8 pixels. Using those pixels the characters and numbers are displayed. We know that the characters and numbers are made using ASCII numbers. In some cases, you might need to create some custom characters. You can create it by enabling the row/column pixels in binary format and stored in an array. If you have LiquidCrystal Version 1.0.7 library installed you can try a cool animated example included in it. To access it go to File->Examples->LiquidCrystal->CustomCharacter file. This is written by Tom Igoe which displays custom characters like heart shape, Smiley and a cool animated human waving his hands. The code is given below

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Parameters (rs, enable, d4, d5, d6, d7)

// make some custom characters:

byte heart[8] = {

  0b00000,

  0b01010,

  0b11111,

  0b11111,

  0b11111,

  0b01110,

  0b00100,

  0b00000

};



byte smiley[8] = {

  0b00000,

  0b00000,

  0b01010,

  0b00000,

  0b00000,

  0b10001,

  0b01110,

  0b00000

};



byte frownie[8] = {

  0b00000,

  0b00000,

  0b01010,

  0b00000,

  0b00000,

  0b00000,

  0b01110,

  0b10001

};



byte armsDown[8] = {

  0b00100,

  0b01010,

  0b00100,

  0b00100,

  0b01110,

  0b10101,

  0b00100,

  0b01010

};



byte armsUp[8] = {

  0b00100,

  0b01010,

  0b00100,

  0b10101,

  0b01110,

  0b00100,

  0b00100,

  0b01010

};



void setup() {

  // initialize LCD and set up the number of columns and rows:

  lcd.begin(16, 2);



  // create a new character

  lcd.createChar(0, heart);

  // create a new character

  lcd.createChar(1, smiley);

  // create a new character

  lcd.createChar(2, frownie);

  // create a new character

  lcd.createChar(3, armsDown);

  // create a new character

  lcd.createChar(4, armsUp);



  // set the cursor to the top left

  lcd.setCursor(0, 0);



  // Print a message to the lcd.

  lcd.print("I ");

  lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte

  lcd.print(" Arduino! ");

  lcd.write((byte)1);



}



void loop() {

  int sensorReading = analogRead(A0); // read the potentiometer on A0

  int delayTime = map(sensorReading, 0, 1023, 200, 1000); // map the result to 200 - 1000:



  lcd.setCursor(4, 1); // set the cursor to the bottom row, 5th position:



  /* draw the little man, arms down*/

  lcd.write(3);

  delay(delayTime);

  lcd.setCursor(4, 1);

  /* draw him arms up */

  lcd.write(4);

  delay(delayTime);

}

If we look at the code the characters are created in a byte array which has 8 bits. Our LCD pixels are 5 bit in a row so it is written as ‘0b00100’. The ‘b’ is used for parsing. Similarly, 8 rows are created to mention a pixel. Modifying this pixel value to 1 prints a dot on that pixel. By this way, we can create our custom characters. The created characters are stored in numbers from 0-7 (limited to 8 characters). When using 0 you must cast it as byte and print using ‘lcd.write((byte)0)’ function. Suppose you want to print the smiley which is stored in location 1 then you need to print using lcd.write(1);

Share this post

Comment (1)

  • nirav

    I love this article. Thanks for this Arduino projects.

    March 29, 2019 at 8:52 PM

Leave a Reply

Back to Blog