Como fazer a interface do LCD de 4 bits com o Arduino- (Parte 4/49)

How to interface 4-bit LCD with Arduino - (Part 4/49)

Liquid crystal displays are used in many types of devices, from small screens in calculators to large screens in televisions. There are many advantages of using LCD monitors in systems such as energy efficiency, slim size, low cost, etc. Small LCD-based display modules are typically found in all types of embedded devices. The LCD, although it looks simple, is really difficult to make work.
The LCD works only with voltage pulses and with precise timing and voltage levels. Hence, special types of LCD drivers are developed to drive the LCD. Two or more driver ICs of this type, together with the LCD screen, form LCD modules that are typically found in embedded systems. The LCD module makes a stand-alone system that can receive inputs and display the corresponding output. This specific project demonstrates how to interface a 16x2 LCD display with an Arduino board .

Any AVR microcontroller based board that follows the standard Arduino schematic and is flashed with the Arduino bootloader can be called an Arduino board. There is no other tool available that helps in easy prototyping like Arduino. The Arduino board has all the necessary circuitry to make the integrated AVR microcontroller work. When it comes to programming the Arduino board, anyone who has basic knowledge of C programming can quickly get started with the Arduino IDE. The Getting Started with Arduino tutorial explains the steps needed to start using an Arduino board. The Arduino board used in this project is the Arduino pro-mini board and the IDE version of Arduino is 1.0.3 for Windows. The image of the Arduino pro-mini board and the Arduino IDE are shown below;

Placa Arduino Pro-Mini típica

Fig. 2: Typical Arduino Pro-Mini board

 Janela do software Arduino IDE

Fig. 3: Arduino IDE software window

Since the Arduino pro-mini board does not have circuitry to interface with the PC's serial port or USB port, an external USB to TTL converter board is required to connect it to the PC. This hardware helps in programming the Arduino board and also helps in serial communication with the USB port of the PC.

 Placa conversora USB externa para TTL para programação de Arduino e comunicação serial

4: External USB to TTL converter board for Arduino programming and serial communication

It is assumed that the reader has gone through the project how to get started with Arduino and tried all the things discussed there. The Arduino IDE has many functions that help to interface the four-bit LCD module. There are functions for initializing the LCD module and for writing characters to the LCD module. The functions used in coding these projects are lcd.begin and lcd.print . Functions are available in the library and you must initialize the library before using these functions.

Liquid crystal

This function must be called to initialize the four-bit LCD library and then only the library functions can be called in the code. The function has six parameters that must be provided during a function call according to the connection of the circuit with the Arduino board and the LCD module. Function parameter details are listed in the order below.

Parâmetros de função de LiquidCrystallcd

Fig. 5: Function parameters of LiquidCrystallcd

For example, the following instruction can be used to initialize an LCD library for code written for the circuit in which the RS pin is connected to pin 12, the Enable pin to pin 11, and D4, D5, D6, and D7 to pins 5, 4, 3 and 2 respectively.

lcd.begin

This function can be used to initialize the LCD module. The first parameter is the number of lines of the LCD module in use and the second parameter is the number of columns. The lcd.begin function can be used to initialize a 16*2 LCD using the instruction;

lcd.begin(16, 2);

The above instruction will initialize the 16*2 LCD into four-bit mode with two-line display mode.

lcd.print

This function is used to display an ASCII character or string on an LCD screen. If a value is provided as a function parameter, it formats that value into a displayable string and then displays it on the LCD.

The lcd.print function is analogous to the Serial.print function discussed in the project on how to do serial debugging with Arduino , how to do serial input and output with Arduino and how to send serial data from Arduino.

The lcd.print function can be used to print a string using the instruction shown below;

lcd.print(“hello world”);

The above instruction will print the string “hello world” on the LCD screen. If the value of a variable needs to be printed on the LCD screen, the same function can be used as it can format a value to the ASCII string representing the value.

lcd.print(100);

In the above instruction, the value 100 is formatted to the string “100” by the lcd.print function and then displays it on the LCD module.

THE CODE

Code includes library first which has all the necessary functions to access the LCD module. The 16*2 LCD module used in this project is initialized using the lcd.begin function. The lcd.print function is then used to print data to the LCD module.

// includes library code:

#include

// initialize the library with the interface pin numbers

LiquidCrystallcd(12, 11, 5, 4, 3, 2);

// give the LED pin a name:

Internal LED = 6;

null configuration

{

// configures the number of LCD columns and lines:

lcd.begin(16, 2);

// Prints a message on the LCD.

lcd.print(“ENGINEERS GARAGE”);

// initialize the digital pin as output.

pinMode(led, OUTPUT);

}

empty loop

{

digitalWrite(led, HIGH); //turns the LED on (HIGH is the voltage level)

delay(1000); // Wait a second

digitalWrite(led, LOW); //turns off the LED leaving the voltage LOW

delay(1000); // Wait a second

}

The code initializes the module, displays a string in the module using functions available in the library . The code that blinks an LED in an infinite loop using the pinMode and digitalWrite functions discussed in the project at how to get started with Arduino and how to use Arduino digital input and output.

Project source code

 ###




 /*============================= EG LABS ================== =================//

 

 Demonstration on how to use 16x2 LCD display with an arduino board

 

 The circuit:

 The circuit:

 * LCD RS pin to digital pin 12
 
* LCD Enable pin to digital pin 11

 * LCD D4 pin to digital pin 5

 * LCD D5 pin to digital pin 4

 * LCD D6 pin to digital pin 3

 * LCD D7 pin to digital pin 2

 * LCD R/W pin to ground

 * 10K resistor:

 * ends to +5V and ground

 *wiper to LCD pin 3

 * LED anode attached to digital output 6

 * LED cathode attached to ground through a 1K resistor

 

//============================= EG LABS ================== =================*/


 // include the library code:

 #include


 // initialize the library with the numbers of the interface pins

 LiquidCrystal LCD(12, 11, 5, 4, 3, 2);

 
// give the LED pin a name:

 int led = 6;


 void setup

 {

 // set up the LCD's number of columns and rows:

 lcd.begin(16, 2);

 // Print a message to the LCD.

 lcd.print("ENGINEERS GARAGE");

 // initialize the digital pin as an output.

 pinMode(led, OUTPUT);

 }


 void loop

 {

 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

 delay(1000); // wait for a second

 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

 delay(1000); // wait for a second

 }

 ###

Circuit diagrams

Circuit Diagram-4-Bit LCD-Arduino Interface

Project Components

  • Arduino ProMini
  • LCD
  • LED
  • Resistor

Project video

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.