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 NAMEArduino Pin
1VSSGND
2VDD5V
3V0Pot Center Pin
4RS10
5RWGND
6E9
7DB0NOT CONNECTED
8DB1NOT CONNECTED
9DB2NOT CONNECTED
10DB3NOT CONNECTED
11DB4Pin 5
12DB5Pin 4
13DB6Pin 3
14DB7Pin 2
15Backlight LED +V5V
16Backlight LED GNDGND

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.