• nithya7rns

Interfacing LCD DISPLAY with Arduino

Updated: Dec 31, 2020

We all know that the television screen we see today has gone through a lot of stages of development in terms of technology to reach the present stage. There can be two types of display, LCD (Liquid crystal Display) and #LED (Light Emitting Diode). In this article, we are going to discuss how to interface this LCD module with Arduino through code to get the desired output.


We will be dealing with different types of output like Static or Non-moving text output, Moving text, Blinking text output, etc. So, let's get started.


Hardware Requirements


  1. Arduino Board


Arduino UNO
Arduino UNO

#Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be from light-detecting sensors, motion sensors (Ultrasonic or IR), temperature sensors, etc. The output function done on the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.

Buy Arduino from here.



2. I2C LCD Board


I2C LCD Module
I2C LCD Board

I2C is basically a serial protocol which is used for two-wire interfacing. It connects low-speed devices like A/D, D/A, #microcontrollers, and other peripherals. I2C LCD is an easy display device that uses a few lines of code for any text output.


Buy I2C LCD Board from here.



3. Jumper Wires


Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.

Buy Jumper Wires from here.



Software Requirements


Arduino IDE


Arduino IDE Logo
Arduino IDE Logo

Arduino IDE (Integrated Development Platform) which is used to interface the boards like #NodeMCU to run the desired code designed to interface hardware devices.



Circuit Connections



Circuit Diagram
Circuit Diagram

This circuit has simple connections. There are four pins on the I2C #LCD that need to be connected to the Arduino board.

  • The Ground pin of the LCD must be connected to the Ground pin of Arduino.

  • The VCC pin of the LCD needs to be connected to the 5V pin in Arduino.

  • The SDA (Serial Data) pin is connected to the analog pin (A4) of Arduino.

  • The SCL (Serial Clock) pin is connected to another analog pin (A5) of the Arduino.

The Arduino board is then connected to the system in which the code is written and interfaced.


This completes the circuit connections. Let's see how the code for different text outputs work.


Code and Working:


Scanner code


This code is the basic code required to be executed as soon as the circuit is interfaced with the IDE to find the number of I2C modules and their addresses. This code will be different from the other given codes for displaying messages as it is going to scan for the I2C modules present.

#include <Wire.h>

This header file can either be typed out on the screen or included from include libraries in the Arduino IDE. See the video to learning how to include the library/header file.

void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}

In the setup( ) function which is used to set up the device for the particular code to be executed, the transmission of data to the LCD device is nothing but the code. The particular #baud rate is chosen for running the code to the device. "I2C Scanner" is displayed on the serial monitor.

void loop() {
byte error, address;
int Devices;

In the loop( ) function, variables are declared. The first is of the type "bytes" which is class. The variables error and address are declared of the same type. A variable called "Devices" is declared of type integer.

Serial.println("Scanning...");
Devices = 0;

The above message is printed on the serial monitor of the IDE first. The value of the variable "Devices" is set to 0 (initialized).

for(address = 1; address < 127; address++ ) {

A "for" loop is started which is used to perform functions continuously from a particular value for a range of given values. The condition here is that the value of the address variable starts from 1 and the for loop runs until the value of "address" is less than 127. It is incremented every time it reaches the end of the loop and comes back to the for loop condition.

Wire.beginTransmission(address);  

Using the header file "Wire. h" that is included, a function beginTransmission( ); is executed to which each value of the address is passed as a parameter every time the loop runs after incrementing the value if "address". This function is used to indicate and initiate the data transmission to the I2C device (LCD).

error = Wire.endTransmission();

Another function of the library Wire. h is used, endTransmission( ) which returns numerical values, and it is equated to the variable "error".

if (error == 0) {
Serial.print("I2C device found at address 0x");

An "if" condition is given inside the for loop which checks if the value of variable "error" is 0 and if it is a message "I2C device found at address 0x" is printed on the Serial monitor. This address is not just 0x, the particular of the I2C device is printed instead. Example: 0x27.

Please note, many other statements and functions fall inside this "if" condition.

if (address<16)
Serial.print("0");

This is a one-line "if" condition which checks the value of the variable "address". If it is less than 16, the value 0 is printed on the Serial monitor.

Serial.print(address,HEX);
Serial.println("  !");
Devices++;
}

As per the previously given "if" condition for the variable "error", the value of "address" in terms of Hexadecimal is printed on the serial monitor, formatting is done by using the Exclamation mark, and the values of the variable "Devices" is incremented by 1. The "if " condition ends here.

else if (error==4) {
Serial.print("Unknown error at address 0x");

An "else-if" condition is started where it checks if the value of the "error" is equal to 4 and the statements are executed accordingly. If the condition is True, the #message "Unknown error at address 0x" is printed on the Serial monitor first.

if (address<16)
Serial.print("0");

This is again a one-line "if" condition which checks if the value of the variable "address" is less than 16 and if it is True, displays the value 0 on the Serial monitor.

Serial.println(address,HEX);
}
}

The Serial monitor displays the address in terms of Hexadecimal value and the "else-if" condition ends here followed by the "for" loop.

if (Devices == 0)
Serial.println("No I2C devices found\n");

Another one-line "if" condition is written outside the "for" loop which prints the message "No I2C devices found" of the values of the variable "Devices" is 0.

else
Serial.println("done\n");

If the "if" condition turns False, the "else" statement prints the message indicating that the work is done.

delay(5000);          
}

A delay of five seconds is given and the void loop( ) function ends here.


COMPLETE CODE


#include <Wire.h>

void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int Devices;
Serial.println("Scanning...");
Devices = 0;
for(address = 1; address < 127; address++ )
{

Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println("  !");
Devices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (Devices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000);          
}

WORKING


The scanner code as mentioned above is used to find the number of I2C modules and their address. The code scan the number of I2C modules finds their address and displays them. This code should be run before using any other code to display a message on the LCD device as we have to know the Hexadecimal value of the module.


After executing the code, the Serial Monitor should be opened which gives the hexadecimal value in the format 0x and a value followed by it.



The inclusion of the header files for using the associated Library functions remains the same for codes even with different outputs.

#include <Wire.h>

The Wire. h header file is included. This library allows communication between the IDE and the I2C device

#include <LiquidCrystal_I2C.h>

The next header file included is for the LCD Display itself. This header file allows us to use functions like backlight( ), setcursor( ), etc in the code.

LiquidCrystal_I2C lcd(0x27, 16, 2);

Now we use the function lcd(0x27, 16, 2), which is derived from the class variable LiquidCrystal_I2C. The lcd( ) function takes three parameters. First is the hexadecimal value, the second value is the number of Rows used for display and lastly, the value is for the number of Columns. Here it is 0x27, 16, and 2 respectively.


*The code for displaying a different kind of text will differ in the upcoming lines of code, that is setup( ) and loop( ) functions will have different lines of code. We will see code for each type of text output now.*


To display the message "CONNECTED" on the LCD.


First, we shall display the message "CONNECTED" on the LCD.

void setup() {
    lcd.begin();

In the setup( ) function, the lcd.begin( ) function is written first to start sending Data to the LCD.

lcd.backlight();

The "backlight( )" function is used to switch on the backlight of the device and the "lcd.noBacklight( )" function can be used when the backlight is not needed.

lcd.clear();

clear( ) is used to clear the screen so that any unwanted values or text are not displayed on the LCD.

lcd.setCursor(0,0);

Through this line of code, the cursor is set to the 0th Row and 0th column.

lcd.print("CONNECTED");
}

The message "CONNECTED" is displayed on the LCD, on the execution of this statement and the loop ends after that.

void loop() { }

In this process of displaying output only once, we do not put any code in the loop( ) function.


COMPLETE CODE


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("CONNECTED");
}

void loop() {
  // put your main code here, to run repeatedly:
}

WORKING


At first, the data transmission to the LCD begins, that is the SDA line of the LCD is intimated to start receiving data once the transmission begins. The backlight of the LCD is switched ON. To avoid any junk data or text, the data is cleared before the message is sent to the LCD.


The cursor is then set to the required position and the transmitted text is received by the LCD and displayed as the message. The data is transmitted as Hexadecimal values which are converted to the respective characters and displayed.


To display two different messages on the LCD screen consecutively.


Let's try to display two different messages consecutively.


The code differs in setup( ) and loop( ) function and that is explained.

void setup() {
  lcd.begin();
  lcd.backlight();
}

In the setup( ) function, the transmission of data to LCD begins with begin( ) function and Backlight is switched on using backlight( ) function.

void loop() {
  lcd.clear();
  lcd.setCursor(0,0);

In the loop( ) function, first the data is cleared using clear( ) function and the cursor is set to the 0th Row and the 0th column using setCursor( ) function.

lcd.cursor();

The cursor( ) function is used to display the cursor on the LCD screen, which is usually represented by the underscore symbol "_".

lcd.print("CONNECTED-1");

From the 0th Row and 0th column, the message "CONNECTED-1" is printed.

delay(2000);
 lcd.clear();
 lcd.setCursor(0,1);

A delay of 2 seconds is given, the data is cleared on the screen and the cursor position is set to 0th Row and 1st Column.

 lcd.noCursor();

Here, this function is used when we do not want to display the cursor( _ ) on the screen.

 lcd.print("CONNECTED-2");
  delay(2000);
}

The message "CONNECTED-2" is printed in the next line as per the cursor position set. A delay of 2 seconds is given. The loop( ) function ends here.


COMPLETE CODE

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.cursor();
  lcd.print("CONNECTED-1");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.noCursor();
  lcd.print("CONNECTED-2");
  delay(2000);
}

WORKING


The SDA line is enabled using the lcd.begin( ) function and the backlight is switched ON. To display two messages consecutively, the cursor is set to two different positions where each of the messages will be displayed. The displaying of the cursor is by users' choice. The SCL (clock line) is used here when the particular delays are given in between the display of messages.


To display Blinking text.


Next, we can display a Blinking text.


void setup() {
   lcd.begin();
  lcd.backlight();

The setup( ) function first begins the transmission of data to the LCD device through the lcd.begin( ) function and the backlight is turned ON using lcd.backlight( ) function.

 lcd.print("Blinking");
}

A message "Blinking" is printed on the screen and the setup( ) function loop ends.

void loop() {
  lcd.display();
  delay(2000);

The loop( ) function is defined. The first function is to allow the message displayed above to continue displaying and that is done by the function lcd. display( ). A delay of two seconds is given.

 lcd.noDisplay();
  delay(2000);
}

Then the function lcd.noDisplay( ) is executed where the text on the LCD disappears and again a delay of two seconds is given. The loop( ) function ends. As we all know, the loop( ) function executes until the code is running in the IDE. So the message/text keeps blinking on the screen.


COMPLETE CODE


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
  lcd.print("Blinking");
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.display();
  delay(2000);
  lcd.noDisplay();
  delay(2000);
}

WORKING


The #SDA (data line) is enabled and the message is printed on the LCD screen showing what type of text is being displayed (blinking). To allow blinking of text, the SCL is used, where after a particular second of delay, the display is enabled and disabled. This repetitive code makes the text shown as blinking text.


To display a Moving message.


Now we shall execute the code for the Moving message.


void setup() {
  lcd.begin();
  lcd.backlight();
  lcd.print("MOVING TEXT");
}

The setup( ) function is as usual used to execute the functions to begin data transmission to LCD device and turn ON the backlight using lcd.begin( ) and lcd.backlight( ) respectively. The LCD shows the message "MOVING TEXT" on the screen.

void loop() {
  lcd.scrollDisplayLeft();
  delay(500);
}

Inside the loop( ) function, lcd.scrollDisplayLeft( ) function is executed. This is the function responsible for moving text. As the name of the function says, the message/text is going to start from the Right end of the LCD screen and scroll to the Left. A delay of 0.5 seconds is given and again the text starts moving/scrolling from Right to Left.


COMPLETE CODE


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.backlight();
  lcd.print("MOVING TEXT");
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.scrollDisplayLeft();
  delay(500);
}

WORKING


As usual, the SDA line is enabled and the data transmission begins. The backlight is switched ON. The data is displayed from the right side and using one of the library functions of LCD, the scrollDisplayLeft( ) function is executed to allowing scrolling of data from the present position to the left. Using #SCL, a delay is given and again the data starts scrolling from right to left.


For the complete code for all these programs, download the following documents.


code1
.txt
TXT • 326B
code2
.txt
TXT • 464B
blinking
.txt
TXT • 356B
moving text
.txt
TXT • 332B
scanner code
.txt
TXT • 778B

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.


Here are the images that show how the circuit is when connected and interfaced.


Circuit connected and before interfacing.
Circuit connected and before interfacing.
Circuit connected and interfaced.
Circuit connected and interfaced.

The video given below shows an explained version of the circuit building, working of the code, and display of output for better understanding.


Project video by Aishwarya Balajee

Using the I2C LCD modules and displaying messages and text is a very interesting circuit/device to explore. These devices themselves on a larger scale are implemented almost everywhere. Say the traffic lights, the ticket display at theatres, the token number display at banks, and many more. Learn how to display your message on the LCD screen and start developing your circuit now.

SEE ALSO

97 views