Learn to code

ARDUINO & CODES

Previous Post
Control a Servo Motor with a Potentiometer and Arduino
Next Post
Controlling a TM1637 4 Digit 7 segment display with Arduino

How to connect and use a Text LCD Display with Arduino

in Arduino Displays

In this Arduino Tutorial, you will control a Text LCD Display. The LCD display can be used to display alphanumeric characters. The one that you will use, in this tutorial, has 16 columns and 2 row. You can create 32 characters with this. As you will see, there a lot of connections on the LCD board. These pins are used for power and communication to display the characters. However, you don’t need to wire all the connections. You will create a button that will show a random number on the Text LCD Display.

Liquid crystal displays(LCDs) offers you a simple and inexpensive way to provide a user interface for a Arduino Project. The most common and popular LCD is the text panel based on the Hitachi HD44780 chip. The displays with this chip displays two or four lines, with 16 or 20 characters per line. In this Arduino tutorial, you will use the one with two lines and 16 characters.

To be able to use the LCD a library for text LCD displays is used. By using this, you are able to print text on yourr LCD as easily as on the Serial Monitor because they share the same print function.

With LCDs you can do more than just display simple text: words can be scrolled or highlighted and you can also display some special symbols.

LCD Displays have a lot of wires connecting to the Arduini Board. Incorrect connections are the number one cause for problems with LCDs. So, take your time connecting everything and double-check that things are connected correctly.

Rember, if your display has a backlight, this needs connecting, usually through a resistor. Furthermore, most LCD Display come without pin headers soldered to them. So, there is a task of soldering the connections if you would like to use LCDs.

Parts you will need

BreadBoard
Jumper Wires
LCD Display
Push Button
220 ohm resistor
10 Kilohm resistor
Potentiometer

BreadBoard Layout

BreadBoard Layout – Connect power and ground to one side of your breadboard.

Place the push button switch on the breadboard and atach one lead to 5V. Atach the other side to ground through a 10-kilohm resistor, and to your Arduino’s pin 6. You’re wiring this as a digital input, just as you’ve done in several other tutorials on this website.

The register select (RS) pin controls where the characters will appear on screen. The read/write pin (R/W) puts the screen in read or write mode. You’ll be using the write mode in this project. The enable (EN) tells the LCD that it will be receiving a command. The data pins (D0-D7) are used to send character data to the screen. You’ll only be using 4 of these (D4-D7). Finally, there’s a connection for adjusting the contrast of the display. You’ll use a potentiometer to control this.

The LiquidCrystal library that comes with the Arduino sofware handles all  handles all the writing to these pins, and simplifies the process of writing sofware to display characters. The two outside pins of the LCD (Vss and LED-) need to be connected to ground. Also, connect the R/W pin to ground. This places the screen in write mode. The LCD power supply (Vcc) should connect directly to 5V. The LED+ pin on the screen connects to power through a 220-ohm resistor.

Connect: Arduino Digital pin 2 to LCD D7, Arduino Digital pin 3 to LCD D6, Arduino Digital pin 4 to LCD D5, Arduino Digital pin 5 to LCD D4. These are the data pins that tell the screen what character to display.

Connect EN on the screen to pin 11 on your Arduino. RS on the LCD connects to pin 12. This pin enables writing to the LCD.

Place the potentiometer on the breadboard, connecting one end pin to power and the other to ground. The center pin should connect to V0 on the LCD. This will allow you to change the contrast of the screen.

The Code


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;
const int numRows = 2;
const int numCols = 16;
int switchState = 0;
int prevSwitchState = 0;
int reply;

void setup() {
  lcd.begin(numRows, numCols);
  pinMode(switchPin, INPUT);

  lcd.print("Push the");
  lcd.setCursor(0, 1);
  lcd.print("Button!");
}

void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      reply = random(5);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("The Button gives the number");
      lcd.setCursor(0, 1);

      switch (reply) {
        case 0:
          lcd.print("Zero");
          break;
        case 1:
          lcd.print("One");
          break;
        case 2:
          lcd.print("Two");
          break;
        case 3:
          lcd.print("Three");
          break;
        case 4:
          lcd.print("Four");
          break;

      }
    }
  }
  prevSwitchState = switchState;
}

Code Explanation

Setup Liquid Crystal Library

First, you’ll need to import the LiquidCrystal library. Next, you’ll initialize the library, somewhat similar to the way you
did with the Servo library, telling it what pins it will be using to communicate. Now that you’ve set up the library, it’s time to create some variables and constants. Create a constant to hold the pin of the switch pin, a variable for the current state of the switch, a variable for the previous state of the switch, and one more to choose which reply the screen will show. Furthermore, two constant variables are made for the rows and columns of the LCD.


#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;
const int numRows = 2;
const int numCols = 16;
int switchState = 0;
int prevSwitchState = 0;
int reply;

 

lcd.begin()

This function sets the dimensions of the LCD. It needs to be placed before any other LiquidCrystal function in the void setup() section of the program. The number of rows and columns are specified as lcd.begin(columns, rows). For a 16×2 LCD, you would use lcd.begin(16, 2), and for a 20×4 LCD you would use lcd.begin(20, 4). In the void setup() part you will also set the pinmode of the swithPin to an input mode.

lcd.begin(numRows, numCols);
  pinMode(switchPin, INPUT);
lcd.print()

This function is used to print text to the LCD. It can be used in the void setup() section or the void loop()section of the program.

To print letters and words, place quotation marks (” “) around the text. For example, to print Push the Button, use lcd.print(“Push the Button”). However we would like to use both rows and therefore we will need another function.

lcd.print("Push the");
lcd.setcursor()

This function places the cursor (and any printed text) at any position on the screen. It can be used in the void setup() or void loop() section of your program.

In order to write to the next line, you’ll have to tell the screen where to move the cursor. The coordinates of the first column on the second line are 0,1 (recall that computers are zero indexed. 0,0 is the first column of the first row). Use the function lcd. setCursor() to move the cursor to the proper place, and tell it to write “Button!”.

  lcd.setCursor(0, 1);
  lcd.print("Button!");
void loop() button

You will use a tactile push button to trigger the arduino to show a random number on the LCD Display. So, first you will read the sensor and store the state (HIGH, LOW) in the variable switchstate. Next, the code determines if the switchstate is not the same as the previous state. In other words, is the push button released or not. In the case the if comdition is pased the next if statement checks whether the button is released, which is a LOW signal, before executing the swith statement.

switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == LOW)
random number

To give a random number, you can use the function random(). Between the Parenthesis () you can give the end number of the random numbers that will be generated. So, the number 5, gives random results between 0 – 5. Remember, there is already text on the LCD Display. Therefore, we need the clear the display from text before printing new text to the LCD Display. You can use the function lcd.clear() to do this. After that we send the line The button gives the number to the LCD Display. We are using lcd.setCursor() again to go to the next line for showing the result.

 

 reply = random(5);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("The Button gives the number");
      lcd.setCursor(0, 1);
Switch Case Statement

You arrived at the final part of the sketh, the switch statement. So, after pushing the button a random number is created by the code. The next step is to display the number on the LCD Display. You can use a switch statement for that. To see how the switch statement function work please go back to: Arduino Tutorial: 2.8 Using Switch Statements

 switch (reply) {
        case 0:
          lcd.print("Zero");
          break;
        case 1:
          lcd.print("One");
          break;
        case 2:
          lcd.print("Two");
          break;
        case 3:
          lcd.print("Three");
          break;
        case 4:
          lcd.print("Four");
          break;

      }

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu