Projects

Interfacing LCD with Arduino – Learn How It Works

In our previous tutorial, we discussed arduino water level indicator and control using Ultrasonic Sensor, in this tutorial, we shall discuss how to interface a liquid crystal display (LCD) with arduino microcontroller, after which we will interface both Ultrasonic distance sensor and LCD with arduino.

With a liquid crystal display like the 16×2 character LCD Module that uses HD44780, which is a Hitachi parallel interface LCD chip, you can display about 32 ASCII characters. Meanwhile any LCD that uses the Hitachi chip mentioned above can be interfaced with arduino, because the library for writing the arduino code is readily available online.

How Liquid Crystals Display (LCD) Work

A liquid crystal display is a display that uses liquid crystalline materials to produce visible image. Here is how the system works, when current is applied to such crystalline material, it becomes opaque thereby blocking the backlight behind the screen, hence, that particular area becomes dark relative to other areas. The character to be displayed assumes this dark area of the screen.

Nature of the Character LCD display

LCDs come in various colours and sizes, e.g. 16×1, 16×2, 16×4, 20×4, etc. also; some of them display black texts on a green background, others display white texts on a blue background, etc. see images below:

Green colour 16x2 LCD display
Figure 1: Green colour 16×2 LCD display
Blue colour 16x4 blue LCD display
Figure: Blue colour 16×4 blue LCD display
Red colour 16x2 LCD display
Figure 3: Red colour 16×2 LCD display

Pin configuration of the 16×2 or 1602 LCD character display

In this tutorial, we shall use the 16×2 character LCD to design a project, but before we do that, let’s identify the pin configuration of the 16xLCD character display.

16x2 LCD pin configuration
Figure 4: 16×2 LCD pin configuration

The functions of the pins are explained below:

  1. VSS: Source supply voltage, which refers to ground connection (GND) or negative supply. This pin is connected to the ground of the power source.
  2. VDD: Drain supply voltage, this refers to positive supply which in this case is 5V. This pin is connected to +5V power source. (The arduino board can supply this power.
  3. VO: This pin is the screen contrast pin; it is used to adjust the contrast of the LCD display. To control the contrast of the display, we connect the two extreme legs of a potentiometer to 5V and ground respectively, and connect the middle leg of the potentiometer to this pin.
  4. RS: Register Select, this pin determines whether we are sending a command to the display or data (character). Commands like setting the cursor to a specific location, clearing the display, scrolling the display to the right, etc. Data here refers to the information to be printed on the screen like characters. When this pin is set to LOW, then the command function is engaged, and when it is set to HIGH, the data display function will be enabled.
  5. R/W: Read/Write, this pin denotes whether we are writing to the display or reading from it. Writing to the LCD means we are asking it to display data. Reading from it means information is utilized by the LCD for its internal processes; we are concerned more with writing to the LCD. Setting this pin HIGH means reading from it, and setting it LOW means writing to it.
  6. E: Enable pin. This pin is like a master to all the activities that can go on in the display. When this pin is set to LOW, no data processing happens, and when it is set to HIGH, data processing and display will take effect as expected.
  7. D0-D7: Data Bus. These are 8 in number, which corresponds to the 8 bit data that can be transmitted to the display using the ASCII table. So, in the ASCII table the HEX code for M is 77, to convert this to binary code, which is the language of digital electronics, we would have 1001101 which is a seven bit number, but our LCD is an eight bit device, so we add 0 at the beginning of the binary number, it then becomes 01001101.
  8. A: Anode, connected to positive voltage supply for controlling backlight.
  9. K: Cathode, connected to negative voltage  supply for controlling backlight

16×2 character LCD functions

Listed below are some of the functions of the LCD arduino library that are used to program the LCD using arduino:

LiquidCrystal lcd()
lcd.begin()
lcd.clear()
lcd.home()
lcd.setCursor()
lcd.write()
lcd.print()
lcd.cursor()
lcd.noCursor()
lcd.blink()
lcd.noBlink()
lcd.display()
lcd.noDisplay()
lcd.scrollDisplayLeft()
lcd.scrollDisplayRight()
lcd.autoscroll()
lcd.noAutoscroll()
lcd.leftToRight()
lcd.rightToLeft()
lcd.createChar()

To showcase how the 16×2 LCD character display works, we shall design a device that measures distances using ultrasonic distance sensor and then display the distance measured on the LCD using arduino microcontroller.

Components for this project are listed below:

  1. 16×2 character LCD (1)
  2. Arduino UNO R3  (1)
  3. HC-SR04 Ultrasonic distance sensor (1)
  4. Potentiometer (1)

We can help you get quality components just contact us

I have a made series of arduino tutorials and ultrasonic distance sensor.

To make the design easy to understand, I will first explain the physical diagram containing just the LCD and arduino, after that, I will explain some necessary points about the connection before joining the ultrasonic sensor to the entire circuit.

Physical diagram of the LCD with arduino
Figure 5: Physical diagram of the LCD with arduino

Vital points to note about using the Hitachi chipset character LCD with arduino

  • Not all pins are used when connecting arduino and the LCD
  • R/W can be pulled to ground since we are only writing to the LCD
  • Any digital pin can be used for the connection with arduino, however, the order of the connection should be from RS in to the last data pin. I.e. pins are assigned in the order RS, R/W, E, D0 to D7, since we used pins 12, 11, 2, 3, 4, 5, it means that RS is connected to pin 12, E is connected to pin 11, D4 is connected to pin 2, D5 is connected to pin 3, D6 is connected to pin 4, D7 is connected to pin 5.

LCD with Arduino Pin connections

  1. VSS is connected to ground
  2. VDD is connected to +5V
  3. VO is connected to a potentiometer
  4. RS is connected to pin 12
  5. E is connected to pin 11
  6. D4 is connected to pin 5
  7. D5 is connected to pin 4
  8. D6 is connected to pin 3
  9. D7 is connected to pin 2
  10. A is connected to +5V
  11. K is connected to ground

Now, one thing is clear, we did not make use of all the 8 data pins of the LCD, this is because we can use only 4 of the 8 pins, hence, doing so means that we are transmitting data from the arduino to the LCD in 4 bits instead of 8 bits. Let’s explain something here, the 8 bit data is equivalent to 1 byte of data, and 1 byte of data is equivalent to 2 nibbles of data. 4 bits corresponds to 1 nibble. So, the 4 bits data constitutes 1 nibble data, therefore, the supposed 8 bits data which is now transmitted with 4 bits will be transmitted twice for each byte of data sent. This means that using just 4 data pins for the data transmission takes twice the time needed than using 8 pins. But using 4 bits saves space.

After connecting the circuit as shown above, the next action will be to program the microcontroller.

The sketch for the design is shown in figure 6 below. (Arduino code is called a sketch).

Arduino sketch for LCD
Figure 6: Arduino sketch for LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);
void setup() 
  {
    lcd.begin(16, 2);
      lcd.clear();
        }
          void loop() 
           {
        lcd.setCursor(0,0);
   lcd.print("ettron.com");
  }

This is a very simple code, the sketch displays ettron.com on the LCD.

Arduino with LCD Display Connection
Figure 7: Arduino with LCD Connection

Code explanation

Table 1: code lines of the sketch and their explanations

Code lineExplanation
#include <LiquidCrystal.h>This function includes the liqiudCrystal library that works perfectly with HD44780, which is the Hitachi parallel LCD chip.***
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);This function creates an LCD object. Arduino is an object oriented programming language, since we are hooking up the LCD pins to pins on the arduino, we create objects that correspond to pin parameters used by the arduino board. For commands and data transmission, The pins correspond to the sequential arrangement of the LCD pins from RS to D7, in our case, apart from VSS, VDD, VO, A and K, we used RS, E, D4-D7. And we sequentially and respectively connected them to pins, 12, 11, 2, 3, 4 and 5 of the arduino.
lcd.begin(16, 2);This is a function used to set up the LCD’s number of columns and rows. Please note that columns come before rows.  In our design, we used 16×2.
lcd.clear();This function clears the LCD screen
lcd.setCursor(0,0);We set the cursor at the position on the LCD screen where we want the data to start printing. 0,0 means starting from the left top corner of the LCD screen (column 0 row 0).
lcd.print(“ettron.com”);This functions tells the arduino to print the text Ettron.com on the LCD screen.

We’ve already discussed how to install arduino library.

Using these functions and so many other ones, you can display characters as you want, scroll them, print content of a variable, sensor reading, etc.

To add more features to the design, let’s interface the LCD with an ultrasonic distance sensor. In this design, instead of displaying the result of the distance measured on the computer serial monitor; it will be displayed on the LCD. 

Below is the physical diagram of the design:

Physical diagram of LCD with Arduino Interfacing design
Figure 8: Physical diagram of the design

The code for the design is also a simple one, because we have already discussed how to use an Ultrasonic distance sensor to measure distances.

All we need to do in the code is declare the necessary variables to hold data, and write few other codes that follow. See sketch below. The explanation of the ultrasonic sensor codes is embedded in the comment section of the sketch.

Arduino sketch for interfacing Ultrasonic sensor and LCD
Figure 9: Arduino sketch for interfacing Ultrasonic sensor and LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);
const int trigPin = 9; 
  const int echoPin = 10;
    long duration; 
      int distance; 
        void setup() {
          pinMode(trigPin, OUTPUT); 
      pinMode(echoPin, INPUT); 
        lcd.begin(16, 2);
          lcd.clear();  
        }
          void loop() 
       {    digitalWrite(trigPin, LOW);
               delayMicroseconds(2);
              digitalWrite(trigPin, HIGH);
            delayMicroseconds(10);
          digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
      distance= duration*0.034/2;
   lcd.setCursor(0,0);
 lcd.print("distance: ");
lcd.print (distance);
lcd.print("cm");
delay(1000);
}

NB: you can measure fractional readings, all you need to do is, change the data type of the variable ‘distance’ from int to float, the design can measure distances to  an accuracy of about 3mm.

This project can serve as advancement to our project on water level monitoring. Instead of using light emitting diodes to monitor the level of water in the tank or the level of any liquid in a tank, we can use an LCD to display the level.

I hope you learned from this tutorial, keep in touch for our next tutorial.

Tags

Related Articles

Leave a Reply

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

Back to top button
Close