LESSON 19: Arduino LCD Display

In this lesson we are going to learn to use an LCD display. This really allows us to take our Arduino projects to that next level! We will first get the LCD hooked up and show we can display a simple welcome message. Then we will use it to display the distance measurement being made from the ultrasonic sensor. So, do not take your project apart from LESSON 18.

Arduino LCD
This circuit displays the distance you measure on a cool LCD.

This table shows you how to connect your Sparkfun Inventor Kit LCD to your arduino. (If you do not have the kit, and want an LCD like the one in this tutorial you can order it HERE.) When oriented like the photograph above, pin one is the pin in the upper left corner, and they are numbered sequentially from left to right. The table below shows the connections needed to allow the arduino to work with this LCD using the code we will write in this lesson. The pinout of other LCD might be different, but if you connect the LCD named pins in column 2 to the Arduino pins in column 3 in the table below, you should be able to get many of the 16X2 LCD’s to work.

Connections for Sparkfun Inventor Kit LCD
(for others column 1 might be different)
LCD Pin # LCD PIN NAME Arduino Pin
1 VSS GND
2 VDD 5V
3 V0 Pot Center Pin
4 RS 10
5 RW GND
6 E 9
7 DB0 NOT CONNECTED
8 DB1 NOT CONNECTED
9 DB2 NOT CONNECTED
10 DB3 NOT CONNECTED
11 DB4 Pin 5
12 DB5 Pin 4
13 DB6 Pin 3
14 DB7 Pin 2
15 Backlight LED +V 5V
16 Backlight LED GND GND

For this project, you can use the table above to connect your LCD to the Arduino.  The diagram below is a graphical representation of the connections for LCD like mine.

 

LCD Arduino
This diagram shows how to connect my LCD to the Arduino.

These LCD are tricky to hook up because there are so many wires. You have to be very careful,  and you have to make sure your LCD is oriented properly. Check the spec sheet that comes with you LCD carefully to verify connections are correct.

Once the LCD is wired up, it is fairly straightforward to use.

 At the top of your code, you will want to make sure that you load the LCD library. This is a standard library that comes with your arduino software.  You load the library by putting the following code at the top of your program:

Also at the top of the code, you want to create you LCD object, which also tells Arduino how you are connected to it:

The numbers tell Arduino that we have RS hooked to pin 10, E hooked to pin 9, DB4-DB7 connected to Arduino pins 5-2, as shown in the table above.

In the void setup, you will want to tell the Arduino that your LCD has 16 columns and 2 rows.  You should put this code in your void setup, since you only need to do it one time:

You are now just about ready to start sending messages to the LCD. You need to start by telling the Arduino where on the LCD to begin the message. Remember that it always wants column first, then row, and that it starts with ‘0’. Therefore, the upper left character would be column 0, row 0, or (0,0). To set the cursor to the upper left corner, you would send command:

So now lets make a simple Counter. We will count off seconds. We start by printing “My Timer” on the first row. We do that with the command:

Now we would go to the second row and print a counter. Lets bring all the code together in the following working program.

Run this program. If you have your LCD hooked up correctly, you should see the counter working. If it is not showing, you probably have miswired the circuit. Go back and check your circuit carefully. You also might need to play with the setting on the potentiometer to get the contrast set correctly.

Now, watch the counter count up and down carefully. You should notice something peculiar as the counter counts backwards. What do you see that is not good? What you should see is that as you count down from 10 to 9, you end up with an extra ‘s’ on the word ‘Seconds’. You end up with ‘Secondss’. This is a vexing problem which occurs just about every time you try and use an LCD. The reason is that when you go from 10 to 9,  printing the number goes from needing two digits to one digit. Then, when you print ‘Seconds’ it is shifted to the left by one character, and you are still left with the last ‘s’ from the previous time you printed ‘Seconds’. Hence you are left with a mess.

There are two ways to clear this up. One way would be to specifically set the cursor to a correct position before printing ‘Seconds’. This would put it in the same place every time, and alleviate the problem. The other possibility is to print a blank line each time through the loop to clear the second line of the LCD. This is something you need to play around with and understand, because it seems to come up every time I try and use an LCD. Look at this code, and try it, and make sure you understand why it fixes the problem.

OK, your assignment is to play around with the LCD and become comfortable with it. Show me that you can make it do some new and interesting things. Write a program of your choosing that uses the LCD as a display. You might consider making it the display for one of the projects you did in one of our earlier lessons.

 

31 thoughts on “LESSON 19: Arduino LCD Display”

    1. tanks for this

      used it foe a greenhouse project.
      when sensors messures soil is dry than a solenoid is opened and a pump starts after 30 secounds it shuts down

  1. i do not connect sensor with arduino for ultrasonic distance meter and display on LCD

  2. Another brilliantly presented lesson.
    A neat trick by printing blanks, works well.!!!
    Starting Lesson 20 tomorrow.
    Thanks Paul .
    Victor

  3. Thank you very much Mr Paul for this tutorials
    Can you please talk about Wifi and how to connect and program the Wifi shield to the Arduino?

    1. MMSW, I hope to do a series on Xbee radio with arduino next, and then talk about WiFi. I will probably not be able to make more videos till this summer, as I am busy teaching the students.

  4. I completely agree with MMSW. We will be waiting for Xbees and Arduino Wifi-shields lessons…
    Please do your best to make it before summer! We need them.
    You are such a great teacher but most of all, a wonderful human being!

    Thank u, again.

  5. Hi if possible I would like to add a LCD display to this code but I’m kind of new to arduino I do have a good understanding of it but don’t know where to begin I’m not asking for anyone to do it for me just to be pointed in the correct direction. const int buttonPin = 2; // input pin for pushbutton
    int previousButtonState = HIGH; // for checking the state of a pushButton
    int counter = 0; // button push counter
    int check = 0;

    void setup() {

    pinMode(buttonPin, INPUT);

    Keyboard.begin();
    }

    void loop() {

    int buttonState = digitalRead(buttonPin);
    if ((buttonState != previousButtonState) && (buttonState == HIGH)) {
    Mouse.move(25, 50, 0);
    String three = “000”;
    String two = “00”;
    String one = “0”;

    while(counter < 10000){
    delay(1000);
    while (check < 1){
    Keyboard.println("1234");
    delay(500);
    Keyboard.println("1111");
    delay(500);
    Keyboard.println("0000");
    delay(500);
    Keyboard.println("1212");
    delay(500);
    Keyboard.println("7777");
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    Keyboard.println("1004");
    delay(500);
    Keyboard.println("2000");
    delay(500);
    Keyboard.println("4444");
    delay(500);
    Keyboard.println("2222");
    delay(500);
    Keyboard.println("6969");
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    Mouse.move(25, 50, 0);
    Keyboard.println("9999");
    delay(500);
    Keyboard.println("3333");
    delay(500);
    Keyboard.println("5555");
    delay(500);
    Keyboard.println("6666");
    delay(500);
    Keyboard.println("1122");
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    Keyboard.println("1313");
    delay(500);
    Keyboard.println("8888");
    delay(500);
    Keyboard.println("4321");
    delay(500);
    Keyboard.println("2001");
    delay(500);
    Keyboard.println("1010");
    delay(500);
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    check++;}
    if (counter < 10 && check == 1) {

    Keyboard.println(three + counter);
    delay(500);
    counter++;
    Keyboard.println(three + counter);
    delay(500);
    counter++;
    Keyboard.println(three + counter);
    delay(500);
    counter++;
    Keyboard.println(three + counter);
    delay(500);
    counter++;
    Keyboard.println(three + counter);
    delay(500);
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    }
    else if (counter < 100){
    Keyboard.println(two + counter);
    delay(500);
    counter++;
    Keyboard.println(two + counter);
    delay(500);
    counter++;
    Keyboard.println(two + counter);
    delay(500);
    counter++;
    Keyboard.println(two + counter);
    delay(500);
    counter++;
    Keyboard.println(two + counter);
    delay(500);
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    }
    else if (counter < 1000){
    Keyboard.println(one + counter);
    delay(500);
    counter++;
    Keyboard.println(one + counter);
    delay(500);
    counter++;
    Keyboard.println(one + counter);
    delay(500);
    counter++;
    Keyboard.println(one + counter);
    delay(500);
    counter++;
    Keyboard.println(one + counter);
    delay(500);
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    }
    else {
    Keyboard.println(counter);
    delay(500);
    counter++;
    Keyboard.println(counter);
    delay(500);
    counter++;
    Keyboard.println(counter);
    delay(500);
    counter++;
    Keyboard.println(counter);
    delay(500);
    counter++;
    Keyboard.println(counter);
    delay(500);
    Keyboard.println("");
    Keyboard.println("");
    delay(30000);
    Mouse.move(25, 50, 0);
    }
    }
    }
    previousButtonState = buttonState;
    }

  6. Sorry I was not very clear in my post I would like the LCD to display the current 4 digit code the arduino is attempting

  7. Excellent Project Sir !! And its explained in a simple and crystal clear manner. I have a small doubt regarding the Potentiometer used here. Could u specify its range ?

  8. Yes it works!
    What a satisfying project and brilliantly well explained as always. Is there a scroll feature with the Lcd library if you go over the sixteen characters? Also what are the other, not connected pins on the LCD for?
    Many thanks
    David

  9. I’m khanfr , I can understand everything what u say but when un go to that math stuff….u are going to the level of high class ,I can’t understand those things because I’m only 11 years……so can you go to a little bit low class plz……

    YOUR A GREAT TEACHER
    I wish I had a teacher like that at my school 🙂

  10. GSM Display System
    I am implementing GSM display System. Everything in the code is okay except reading data serially and printing it on lcd.
    The message sent from mobile is displaying on serial monitor but not on 16*2 LCD. LCD is working

    #include

    #include
    LiquidCrystal lcd(12,11,5,4,3,2);
    SoftwareSerial mySerial(7,8);
    int led=13;
    int temp=0,i=0,x=0,k=0;
    char str[100],msg[32];
    void setup()
    {
    lcd.begin(16,2);
    Serial.begin(9600);
    mySerial.begin(9600);
    pinMode(led, OUTPUT);
    digitalWrite(led, HIGH);
    lcd.setCursor(0,0);
    lcd.print(“GSM Initilizing…”);
    gsm_init();

    lcd.print(“Wireless Notice”);
    lcd.setCursor(0,1);
    lcd.print(” Board “);
    delay(2000);
    lcd.clear();
    lcd.print(“Forklift”);
    delay(1000);
    lcd.setCursor(0,1);
    lcd.print(“Communication”);
    mySerial.println(“AT+CNMI=2,2,0,0,0”); // AT Command to recieve a live SMS
    delay(1000);

    mySerial.println(“AT+CMGF=1”); // AT Command to recieve a live SMS
    delay(1000);

    mySerial.println(“AT+CPMS=’SM’ “); // AT Command to recieve a live SMS
    delay(1000);

    mySerial.println(“AT+CMGL= ‘ALL’ “); // AT Command to recieve a live SMS
    delay(1000);

    mySerial.println(“AT+CMGR=2”); // AT Command to recieve a live SMS
    delay(1000);

    mySerial.println(“AT+CMGD=2”); // AT Command to recieve a live SMS
    delay(1000);

    digitalWrite(led, LOW);
    }
    void loop()
    {
    for(unsigned int t=0;t<10000;t++)
    {
    serialEvent();
    if(temp==1)
    {
    x=0,k=0,temp=0;
    while(x<i)
    {
    while(str[x]=='#')
    {
    x++;
    while(str[x]!='*')
    {
    msg[k++]=str[x++];
    Serial.println( msg[k++]);
    }
    }
    x++;

    msg[k]='\0';
    }
    lcd.clear();
    lcd.print(msg[k++]);
    delay(1000);
    temp=0;
    i=0;
    x=0;
    k=0;
    }
    }

    if (mySerial.available())
    Serial.write(mySerial.read());

    lcd.scrollDisplayLeft();
    }
    void serialEvent()
    {
    while(Serial.available())
    {
    char ch=(char)Serial.read();
    str[i++]=ch;
    if(ch == '*')
    {
    temp=1;
    lcd.clear();
    lcd.print("Message Received");
    delay(1000);
    }
    }

    if (mySerial.available())
    Serial.write(mySerial.read());
    }
    void gsm_init()
    {
    lcd.clear();
    lcd.print("Finding Module..");
    boolean at_flag=1;
    if (mySerial.available())
    Serial.write(mySerial.read());
    mySerial.println("AT");

    delay(1000);

    lcd.clear();
    lcd.print("Module Connected..");
    delay(1000);
    lcd.clear();
    lcd.print("Disabling ECHO");
    boolean echo_flag=1;

    if (mySerial.available())
    Serial.write(mySerial.read());

    mySerial.println("ATE0");
    delay(1000);

    lcd.clear();
    lcd.print("Echo OFF");
    delay(1000);
    lcd.clear();
    lcd.print("Finding Network..");
    boolean net_flag=1;

    if (mySerial.available())
    Serial.write(mySerial.read());

    mySerial.println("AT+CPIN?");

    delay(1000);

    lcd.clear();
    lcd.print("Network Found..");
    delay(1000);
    lcd.clear();
    }

  11. Hi, i’m making a water level control sytem with ultrasonic sensor, LCD , 4 channel relay module , 2 AC water pumps.Even if i dont connect the pump to relay module when the system starts after a few seconds or some time later the LCD display shows some special character and turn mad. it keeps on untill i reset the board.
    any suggestions on display going mad. thnx

  12. I added in an extra ‘for’ statement to sit on 10 for 1 secon because in the original code it sat on 10 for 2 seconds. Then I changed the <= to 9 on the count up and count down 'for'…Now it counts up and down smoothly and evenly…

    #include //Load Liquid Crysatal Library
    LiquidCrystal LCD (10,9,5,4,3,2); //Creating the LCD object named LCD and how we hooked up the pins
    int myCounter = 0; // declare counter

    void setup() {

    }

    void loop() {
    LCD.begin(16,2); //Tell arduino the LCD has 16 col 2 rows
    LCD.setCursor(0,0); //Set LCD cursor to upper left
    LCD.print(“My Timer: “); //Print first line

    for (myCounter=1; myCounter =10; myCounter=myCounter-1) {

    LCD.setCursor(0,1);//set cursor to first col and second row
    LCD.print(” “); //clears the row
    LCD.setCursor(0,1);//set cursor to first col and second row
    LCD.print (myCounter); //prints units
    LCD.print (” Seconds”);//prints seconds
    }

    for (myCounter=10; myCounter >=0; myCounter=myCounter-1) {

    LCD.setCursor(0,1);//set cursor to first col and second row
    LCD.print(” “); //clears the row

    LCD.setCursor(0,1);//set cursor to first col and second row
    LCD.print (myCounter); //prints units
    LCD.print (” Seconds”);//prints seconds
    delay(1000);//units appear in seconds
    }
    }

  13. Sir,

    Please let me know why we use only data lines DB4, DB3, DB2, DB1, DB0. This is only capable of transmitting a 4-bit entity and characters are atleast 8-bit data. This is a seeming discrepancy.

    Please kindly address my query. Thank you

  14. How would I add buzzer before the timer start ,when it stop and delay for 10 seconds before it repeats over and over?
    What if I wanna use picaxe instead of arduino?
    Witch picaxe would be?

  15. Hi There,
    I’ve got a 1602 LCD for Arduino without i2c backpack. However i have purchased a PCF 8574 Waveshare pin extender. I would like to connect this with my lcd. Can you share your idea on connecting it together and make it work?

Leave a Reply

Your email address will not be published. Required fields are marked *