Introduction to LCD (text mode) :
The LCDs (Liquid Crystal Displays) are highly used in Arduino projects for displaying required text message and status of the process. The price of the LCD is affordable and easily available. The standard LCDs can display text message with various colour LED background s.
The commonly available text sizes of LCDs are:
1) 1 row x 16 columns
2) 2 rows x 16 columns
3) 4 rows x 16 columns
4) 4 rows x 20 columns
with green or blue or while background colour light.
The 2 rows x 16 columns with green background is highly used in the Arduino projects.
For all the above listed LCDs, 16 pins are available to control and display the text. The pins 1 and 2, are for power supply of the circuit and 15 and 16 are background LED power supply. The pin 3 is for adjusting contrast of the display and pins 4, 5 and 6 are control pins i.e., RS (Register Select), RW (Read/Write) and Enable. The remaining 8 pins (byte) from 7 to 14 are data pins. (Refer pinouts of LCD below.)
The LCD have two options for its data bits operation. i.e., 8 bit mode and 4 bit mode. While in 8 bit mode, the data byte is read/write in one enable clock pulse, whereas in 4 bit mode, the data byte is divided in to two parts and read/write using two clock pulses of enable pin. So, in 4 bit mode, you may save number of pins connected to Arduino board, at an expense of a very little delay (double time) for data transfer.
Normally, you may be writing the data to LCD. So, the R/W pin (no 5) is connected to ground (logic low). In case you want to read any register of LCD, the R/W pin should be set to logic high (hardly reads the LCD register).
Interfacing LCD to Arduino :
Any LCD (which are listed above), may be connected to any Arduino board with minimum 6 digital pins. i.e., 4 data pins and 2 control pins, RS & enable. A simple generalized circuit diagram for interfacing LCD with Arduino Uno board is shown below. You may observe that the pin 5 (R/W) is connected to ground and pin 3 (VEE) is connected to a trimpot's mid pin. The trimpot's end pins are connected to Vcc and ground. The trimpot should be adjusted for good contrast of display after displaying any text on LCD.
Now, open Arduino IDE, then select from menu, File->Examples->Liquid Crystal->Hello World.
Then, declare the pin connections of LCD to Arduino board, as shown below.
// include the library code
#include <LiquidCrystal.h>
// Pin connection sequence RS, Enble, D4, D5, D6, D7
LiquidCrystal my_lcd { 2, 3, 4, 5, 6, 7 };
Initially, set LCD's number of columns and rows in begin function.
Then, enter the required text to be displayed using print function.
The setCursor function locates the cursor on the LCD to display the next text using print function.
void setup()
{
// set up the LCD's number of columns and rows:
my_lcd.begin(16, 2); // std : 16,2 or 16,4 or 20,4
// Print a message to the LCD.
my_lcd.print("Simple");
my_lcd.setCursor (4, 1);
my_lcd.print ("MECHATRONICS");
}
Then, continue with the existing loop function, to display counting seconds at 1st col 2nd row
void loop() // for displaying number of seconds lapsed
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
my_lcd.setCursor(0, 1);
// print the number of seconds since reset:
my_lcd.print(millis() / 1000);
}
The, output of the sketch is shown here. You may adjust the trimpot for good contrast.
For 16 coloumns x 1 row also, it should be declared as my_lcd.begin(16, 2); only. The first row text will be displayed on left 8 columns and second row text will be displayed on right side 8 columns (starting from 9th column)
Similarly, declartion for 16 columns x 4 rows,
my_lcd.begin(16, 4);
and declartion for 20 columns x 4 rows,
my_lcd.begin(20, 4);
Simple LCD control functions:
Some simple functions, which are useful and frequently used are explained below:
// for clearing all the contents (text) on the LCD
my_lcd.clear();
// To display and hide the text displayed on the LCD
my_lcd.display(); // To SHOW contents of LCD
my_lcd.noDisplay(); // To HIDE contents of LCD
// for positioning the cursor at required position (x, y)
// (x,y) numbers starts from (0,0)
// so 0 means 1st, 1 means 2nd. . . and so on.
my_lcd.setCursor(5, 1);// Position at 2nd row, 6th column
// To display or hide a rectangular blinking cursor
// positioned by setCursor function.
my_lcd.blink(); // To SHOW rectangular blinking cursor
my_lcd.noBlink(); // To HIDE rectangular blinking cursor
// To display or hide an underline type cursor
// positioned by setCursor function.
my_lcd.cursor(); // To SHOW underline type cursor
my_lcd.noCursor(); // To HIDE underline type cursor
LCD text scroll functions:
Some simple scroll functions, which are used to scroll displayed text on LCD to either side are explained below:
// To scroll the displayed text to RIGHTside by one step
my_lcd.scrollDisplayRight();
// To scroll the displayed text to LEFT side by one step
my_lcd.scrollDisplayLeft();
// for automatically scroll a long text to left
// for reading long text sentences.
my_lcd.autoscroll();// scrolls the text to left by one step
Before using the scrolling functions, the required text should be displayed using my_lcd.print function. Then, for continuous scrolling effect of the text sentence (text string), the above functions should be used in a for loop function.