Arduino Mega 2560 – Getting started with LCDs

In this tutorial, the goal is to guide you into the basic functions of a Liquid Crystal Display (LCD). Besides learning how to set it up on an Arduino, this tutorial will also show you how to print text, scroll it, position it anywhere in your LCD and even create custom characters. This type of hardware is very useful whenever we need to output some information like time and date, the temperature inside a room or the values measured by any sensor you are using. Has you can imagine, LCDs can make our projects a lot more interactive, so let´s get to work!

Parts Required

  • Arduino Mega 2560;
  • USB 2.0 Cable Type A/B;
  • 1 Liquid Crystal Display (LCD);
  • 1 220Ω resistor;
  • 1 10kΩ potentiometer;
  • Male to male jumper wires;
  • 1 Breadboard.

Note: Since only 6 digital pins are required for you to follow this tutorial, an ESP8266 NodeMCU board or any similar board can be used instead. Just be sure to change the pin numbers in the code if you use other boards.

Liquid Crystal Display (LCD)

The LCD used in this tutorial is known as a 16×2 LCD display (Figure 1). Its name derives from the fact that the LCD has 16 columns and 2 lines (rows), meaning it can display 16 characters per line at once. However, this does not mean we can not show more than 32 characters if we need. We will see how to do it later in this tutorial.

Figure 1 – Liquid Crystal Display (LCD).

One useful thing we always should have when working with any piece of hardware is its corresponding datasheet since it contains all the hardware’s important information. From this LCD’s datasheet, we can see that it can be connected considering two possible modes: 4-bit mode or 8-bit mode. In the 8-bit mode, the 8 data pins of the LCD are required so that the 8-bit ASCII value of the character can be sent simultaneously to the LCD. Regarding the 4-bit mode, only 4 data pins are required, but the 8 bits of the character have to be divided in two. Hence, the LCD receives only 4 bits at a time, starting with the high order bits and followed by the low order bits. After that, the character is displayed on the LCD.

Although the 8-bit mode seems straightforward, it requires four additional digital pins from our microcontroller to work, which can be a problem for people using other boards with less digital pins, like ESP8266 NodeMCU or similar. For that reason, in this tutorial, we are going to use the 4-bit mode, which, besides its inherent latency, does not present a significant difference in performance.

Circuit

From the LCD’s datasheet, we have access to its pins’ interface description. To facilitate, a diagram of the LCD pins is shown in Figure 2. If you are using a different LCD display, these pins may not be the same, so be sure to check them in their corresponding datasheet before you connect them to your Arduino.

Figure 2 – LCD pins’ diagram.

After the power pins (VSS and VDD), we can see that there is a pin (VE) used to adjust the contrast of the character pixels, which can be very helpful depending on the light conditions where we are using our LCD. Then, the register select pin (RS) is responsible for selecting the command and data registers. The command register stores the command instructions were given to the LCD (e.g. initialize it, clear the screen, scroll the screen, etc.), while the data register stores the data to be displayed in its ASCII value. Next, the read/write (RW) pin is the pin that either enables us to write the characters we want to the LCD (write mode) or to read the characters from it (read mode). After that, the enable (E) pin functions like a trigger, sending the information to the data pins of the LCD when a high to low pulse is given, followed by the 8 data pins (D0-D7). Finally, the backlight anode and backlight cathode pins are used to connect the background LED of the display.

With this in consideration, we can begin to connect our circuit, which schematic is represented in Figure 3.

Figure 3 – Circuit’s schematic.

First, connect the VSS and VDD to ground and 5 Volts, respectively. Then, in order to adjust the contrast of the character pixels, we can use a 10kΩ potentiometer. Connect the first terminal to 5 Volts, the middle terminal to the LCD’s VE pin and the third pin of the potentiometer to ground. Next, connect the digital pins 9 and 8 of the Arduino to the RS and E pins of the LCD, respectively. Due to the fact that, in this tutorial, we only want to write something to be displayed in the LCD, we can connect pin RW of the LCD directly to ground, since, when the signal in this pin is zero (low), it is set to write to the register, whereas, when the signal is one (high), the LCD is set to read from the register.

Since we are using the 4-bit mode, we can omit the data pins D0 to D3. Connect pins 5 to 2 of the Arduino to pins D4 to D7 of the LCD, respectively. Regarding the background LED of the LCD, in order to limit the current flowing through it, connect a terminal of the 220Ω resistor to the backlight anode pin of the LCD and the remaining terminal to 5 Volts. Finally, connect the backlight cathode pin to ground.

Code

At last, we can start developing our code. To do so, we have to use the LiquidCrystal library, which allows our Arduino to control any LCD based on the Hitachi HD44780 LCD controller, which is found on most text-based LCDs [1]. Fortunately for us, this library is pre-installed in the Arduino IDE.

After including it in our code, we must create an LCD object (lcd) in order to define its parameters, meaning that we need to indicate which Arduino pins are going to be connected to the LCD pins. To do so, we have to know the syntax to set these parameters [2], which is shown below:

LiquidCrystal lcd(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)

Considering that we are using the 4-bit mode and that the LCD pin RW is connected to ground, we can omit pins d0 to d3 and rw. The resulting code is as follows:

LiquidCrystal lcd(rs, enable, d4, d5, d6, d7)

With all the parameters defined, we can then start to write what we want to display. In this tutorial, I will display in a loop an introductory message, followed by our web site’s name with a smile blinking an eye in the middle of the LCD’s first row and then an ending message scrolling from right to left. All the library functions used in this tutorial and more are presented here, so go on and explore all of them!

If, alternatively, you are using an ESP8266 NodeMCU board, you may need to change the pin numbers in the line of code where the parameters are defined (line 5). In case you need help with that, you may found useful to check our tutorial “ESP8266 NodeMCU – Blinking a LED” to see the corresponding pin numbers for this board.

// Includes the library code
#include <LiquidCrystal.h>

// Defines the LCD's parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

// Defines the first user defined character - :)
byte smile1[8] = {
  0b00000,
  0b11010,
  0b11001,
  0b00001,
  0b00001,
  0b11001,
  0b11010,
  0b00000
};

// Defines the second user defined character - ;)
byte smile2[8] = {
  0b00000,
  0b11010,
  0b11001,
  0b00001,
  0b01001,
  0b01001,
  0b10010,
  0b00000
};

void setup() {
  
  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  
}

void loop() {

  // Creates the user defined characters - :) and ;)
  lcd.createChar(1, smile1);
  lcd.createChar(2, smile2);

  // Prints the introductory message
  
  // Prints the first part of the message to the LCD
  lcd.print("Hello everyone!");
  // Sets the cursor to column 0, row 2
  // (note: the counting of rows and columns begins with 0)
  lcd.setCursor(0, 1);
  // Prints the second part of the message to the LCD
  lcd.print("We are ...");
  delay(2000);
  // Clears the LCD screen
  lcd.clear();
  
  // Prints the website's name and the user defined 
  // characters to the LCD

  // Sets the cursor to column 3, row 1 (so that it appears
  // in the middle of the LCD)
  lcd.setCursor(2, 0);
  // Prints the website's name and characters separately
  lcd.print("GEE");
  delay(500);
  lcd.print("KE");
  delay(500);
  lcd.print("RING");
  delay(500);
  lcd.setCursor(12, 0);
  // Prints the first character 
  lcd.write(byte(1));
  delay(500);
  // Prints the second character (only for a short period
  // of time to make a blinking efect)
  lcd.setCursor(12, 0);
  lcd.write(byte(2));
  delay(300);
  lcd.setCursor(12, 0);
  // Prints the first character again
  lcd.write(byte(1));
  delay(2000);
  lcd.clear();

  // Prints the ending message

  lcd.setCursor(15, 0);
  lcd.print("  visit us on");
  lcd.setCursor(15, 1);
  lcd.print("www.geekering.com");
  // Loop for scrolling the message since it begins (from
  // the right side of the LCD) until it desapears
  for (int i = 0; i < 32; i++) {
    lcd.scrollDisplayLeft();
    delay(400);
  }
}

If you enjoyed this tutorial, you can visit our YouTube channel and watch this and many other videos. Thanks for following us and be sure to rate, comment and share our content. Have fun!!

References

[1] https://www.arduino.cc/en/Reference/LiquidCrystal

[2] https://www.arduino.cc/en/Reference/LiquidCrystalConstructor

4 thoughts on “Arduino Mega 2560 – Getting started with LCDs

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock