Image may be NSFW.
Clik here to view.
As you can see from the title, we are going to be learning how to interface a 16×2 LCD display with an Arduino in this post. The 16×2 LCD display is like the basic display you can get for any embedded or electronics project. It serves most of the purpose. And since the display has only 16 columns and 2 rows (hence the name), you are not the liberty of printing just anything you want, like in a software application project. Anyway, let us get into the details here.
A 16×2 LCD display usually comes with 16 pins, 14 of which are as shown in the circuit diagram below. There are two other pins, called LED+ and LED-. These are used to control the backlighting of the LCD display. And these pins are usually present only in displays which have the backlighting feature. Hence, it is not included in the circuit diagram.
So what do you connect these two pins to if your LCD display has them? Well, the LED+ pin usually goes to a potentiometer with which you are going to control the backlight of the display. The LED- goes to the Ground. And in case you are pretty sure that the backlight of your display is going to be fixed, you can just have a suitable resistor from Vcc and connect the other lead of the resistor to the LED+ pin.
Now, as you can see from the circuit diagram, we have connected the Vss pin to Ground and the Vdd pin to the Vcc. These two pins supply the power to the display. The third pin, which is the Vee pin, is used to control the contrast of the text being displayed. Usually, this pin is also connected to a potentiometer to be able to control the contrast. But very rarely does that come to any use. So you can just connect the pin to Vcc or to the Ground depending on the LCD display. Mine connects to the Ground to display the characters at full contrast, hence the circuit diagram.
Image may be NSFW.
Clik here to view.
The RS pin is the Register Select pin. This is used to select either the data register or the control register of the LCD display. This is used to either send commands to the LCD display or to send data that is to be printed on the display. The RW pin is the Read/Write pin. This pin will decide if you want to read data from the LCD display’s registers, or if you want to write to them. Very rarely do you read anything from a display. We usually only write to the display’s registers. And since we are not going to read anything from the display in this project, I have connected the RW pin to the Ground, which selects the Write operation.
The E pin is the Enable pin. This is used to enable the display. If you look at the working of an LCD display, you will understand that this pin also has an important role in printing any text on the LCD display as you will have to disable and enable the display everytime you write anything on it.
Now, the pins labelled Dx are the data pins. You send all the data and commands to the LCD display using these pins. There are eight data pins, from D0 to D7. But you do not have to use all the eight pins to display data on the LCD display. As you can see, we are going to be using only the last four, D4-D7, pins for displaying text in this project.
Now that you have understood (I assume) the circuit, make the connections as shown in the circuit diagram. As usual, you can change the pins on your Arduino board, but be careful to change the pins in the program as well, which is very important.
The Arduino IDE comes with a lot of libraries built in, LiquidCrystal being one of them. The LiquidCrystal library gives you all the functions you will need to work with LCD displays. So the need to understand the working of the C code for an LCD display is not needed. But it is highly recommended that you learn these things the hard way, which will help you play with them at a later stage. So, the code given below will be using the built in libraries.
#include //include the LiquidCrystal library LiquidCrystal lcd(12,11,5,4,3,2); //create a LiquidCrystal object, "lcd" int i=0; //declare an int variable void setup() //start the setup method { lcd.begin(16,2); //initialize the 16x2 LCD display lcd.setCursor(0,0); //set the cursor of the display to column 0, row 0 lcd.print("Hello, World!"); //print "Hello, World!" lcd.setCursor(0,1); //move to the secord row lcd.print("Vcc To Ground"); //print "Vcc To Ground" } void loop() { lcd.setCursor(13,0); //column 13, row 0 lcd.print(" "); //clear three columns lcd.setCursor(13,0); //go back to column 13, row 0 lcd.print(i); //print the value of i i++; //increment the value of i delay(1000); //stay here for one second }
The program is a very basic one. You will see the text “Hello, World!” in the first row, and “Vcc To Ground” in the second row. The program is pretty straight forward and easy to understand. And once you do understand, you will be able to change the code according to your needs and get the desired output.
But these two lines of text are set up in the “setup()” function, which is executed as soon as the microcontroller in powered up. So how do you know if the program is being executed after that? Simple! We just implement an up counter using the delay() function to increment an int variable every one second and display that value on the LCD display. This way, you will know that the program is running properly. And that is what we are doing in the “loop()” function.
You can see the output of this code in the video below. Hope this code worked out for you and helped you understand the interfacing. If you have any doubts or suggestions, please feel free to comment in the comment section below.