Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

LCD Display/HD44780

Demonstrate interfacing Arduino Nano with a LCD display compatible with the Hitachi HD44780 driver.

Text based output will often be required by a project to report some form of data and or basic menu I/O functionality. For those projects that cannot rely upon connection to a PC via the serial port, an LCD based upon ubiquitous Hitachi HD44780 driver (usually can be identified by the 16-pin interface) will often be a relatively simply and economical option.

The Arduino IDE provides the LiquidCrystal library (1) which provides convenient driver code to simply interface the Arduino Nano with a LCD display that is compatible with the Hitachi HD44780 driver.

The Hitachi HD44780 based LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

  • A register select (RS) pin that controls where in the LCD's memory you're writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD's controller looks for instructions on what to do next.
  • A Read/Write (R/W) pin that selects reading mode or writing mode
  • An Enable pin that enables writing to the registers
  • 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to a register when you write, or the values you're reading when you read.

There's also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don't need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so the following examples shows how to control a 16x2 LCD in 4-bit mode as this uses the least number of port pins on the Nano.

As generally the number of available I/O ports is at a premium when using the Arduino Nano in typical projects, how to use an I2C connection via a PCF8574 port expander, to decrease the number of required I/O pins on the Nano to only two (2) to connect a LCD is also demonstrated.


The Arduino Nano board has a surface mount microcontroller with headers for all pins. Additionally, the board provides a 5V regulator for external DC power input and PCB copper traces that enable insertion into standard USB connector which can then be used for DC power source. There is an onboard LED to indicate power-on and an additional LED/resistor connected to pin1 for user control.

For convenience for this simple test, the USB power supply is used.

ISP programming is used as detailed in the Arduino Nano introduction.

PCF8574 Port Expander

Pin 8 is connected to ground and pin 16 to Vcc (i.e., 5V). Pins 1 through 3 (A0, A1, A2 respectively) determine the device address. The PCF8574 has a device address of 0100 + A2,A1,A0 whereas the PCF8574A has a device address of 0111 + A2, A1, A0. Therefore, with pins 1 through 3 on the PCF8574A connected to ground, the device address will be 0111000 which is 0x38.

Pin 15 is the I2C data line, which is connected pin 23 (marked A4 on the Nano silk screen) on the Arduino Nano. Whereas, pin 14 is the I2C clock line, and is connected to pin 24 (marked A5 on the Nano silk screen) on the Arduino Nano.

As discussed in the Background Section, pull-up 4K7 to 10K resistors are mandatory on pin 14 and 15.


Note: Image loading can be slow depending on server load.

  • LCD Interface SchematicLCD Interface Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    Arduino Nano with LCD Interface in 4-bit mode

    Arduino Nano with LCD Interface in 4-bit mode

  • LCD via I2C Interface SchematicLCD via I2C Interface Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    Arduino Nano with LCD via I2C Interface

    Arduino Nano with LCD via I2C Interface

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
Resistors
2R1,R210K1/4W, 10% 
1RV110K1/4W, 10%  potentiometer
Diodes
4D6 to D9LED 
Integrated Circuits
1U1Arduino Nanomicrocontroller datasheet
1U3PCF8574A8-bit port expander datasheet
Miscellaeous
3J1wiresDupont pin jumper wires
1Z1LCDLCD (Hitachi HD44780 driver)
Description Downloads
Bill of Materials Text File Download

Before connecting the Arduino Nano to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Nano board and the USB/PC.

The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so the following examples shows how to control a 16x2 LCD in 4-bit mode as this uses the least number of port pins on the Nano.

As generally the number of available I/O ports is at a premium, how to use an I2C connection via a PCF8574 port expander, to decrease the number of required I/O pins on the Nano to only two (2) to connect a LCD is also shown below.

The Photographs Section shows the circuit on breadboard, whereas, the Videos Section shows the circuit in operation.

LCD using 4-bit mode (6 x I/O pins)

The Circuit Details and Schematic Diagram Sections discuss and display the required physical connections and circuitry required for connection of the LCD to the Arduino Nano.

The actual process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library (1) simplifies this for you so you don't need to know the low-level instructions. The LiquidCrystal Library provides a wide range of functions that enable features such as scrolling text, blinking cursors and custom character generation, see the following reference for details (2).

The following code Snippet 1 gives an example of the necessary code to display text on a connected LCD display. The code displays a 'static text' on the first line of the LCD display, with the second line displaying the letters of the alphabet in a continous loop.

Code Snippet 1: LCD display (4-bit mode)


 #include 

 const int rs=9, en=8, d4=7, d5=6, d6=5, d7=4;
 LiquidCrystal myLCD(rs, en, d4, d5, d6, d7);

 char theChar = 65; //ASCII character code

 void setup() {
   myLCD.begin(16,2);    // initialize the lcd
   myLCD.setCursor(0,0); //Defining positon to write from first row, first column .
   myLCD.print("LCD Display");
   myLCD.setCursor(0,1); //Second row, first column
   myLCD.print("Test:");
 }

 // the loop function runs over and over again forever
 void loop() {
   myLCD.setCursor(6,1); //2nd line, 6th col
   myLCD.print(theChar); //print incrementing alphabetic character
   theChar = theChar + 1;
   if (theChar > 90) {theChar = 65;}; // reset to 'A' if reached 'Z'
   delay(1000);
 }
   				

LCD using PCF8574 (2 x I/O pins on the Nano)

The Circuit Details and Schematic Diagram Sections discuss and display the required physical connections and circuitry required for connection of the LCD to the Arduino Nano via I2C using the PCF8574 port expander.

The I2C device (the PCF8574A in this case) needs to be connected via the SDA and SCK pins (with pullup resistors, and obviously connect Gnd and Vcc) to the Arduino Nano. The Serial Monitor on the Arduino IDE is used so output can be directed via USB to a PC Com serial port.

Using a PCF8574A and PCF8574 pins 1 through 3 (ie A0 to A2) tied to ground produces an I2C device address of 0+0111+0+0+0 = 0x38, so the code below should report a single device found with address of 0x38 hexadecimal. Altering which of the PCF8574A pins 1 through 3 are tied to ground or Vcc will change the reported address (the datasheet gives the table showing what the resultant addresses should be).

Initial testing of a I2C interfaced device (and if the I2C interface library is working on the Arduino Nano) is to use the I2C scanner sketch.

Once it is determined that I2C communication is working with the Arduino Nano and the PCF8574A, the next step is to use the HD44780 Library (3) (which is written for interfacing LCDs via I2C) with the test in Code Snippet 2 below.

The Photographs Section shows the circuit on breadboard, whereas, the Videos Section shows the circuit in operation.

Code Snippet 2: LCD using I2C/PCF8574 (2 x I/O pins)


 #include 
 #include                        // main hd44780 header
 #include  // i2c expander i/o class header

 #define LCD_ADDR 0x38

 hd44780_I2Cexp myLCD(LCD_ADDR); // specify a specific i2c address
 const int LCD_COLS = 16;        // LCD geometry
 const int LCD_ROWS = 2;

 char theChar = 65; //ASCII character code

 void setup() {
   int status;
   status = myLCD.begin(LCD_COLS, LCD_ROWS); //  begin() will automatically turn on the backlight
   myLCD.print("LCD Display");

   myLCD.setCursor(0,1); //Second row, first column
   myLCD.print("Test:");
 }

 void loop() {
   myLCD.setCursor(6,1); //2nd line, 6th col
   myLCD.print(theChar); //print incrementing alphabetic character
   theChar = theChar + 1;
   if (theChar > 90) {theChar = 65;};
   delay(1000);
 }
                

The circuit is the basic minimum required to enable the Arduino Nano microcontroller to operate after being programmed with a suitable .hex file. So this circuit was only laid out on a breadboard.

Before connecting the Arduino Nano to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Arduino Nano board and the USB/PC.

The LCD used is based upon the ubiquitous Hitachi HD44780 driver, other types of LCD are not compatible with the code libraries used/described.

The LCD used has a 'contrast' potentiometer connected to pin 3. Make sure this is set appropriately, test the full range of the poteniometer if necessary, if no characters are displayed with the test code.

As discussed in the Background Section, pull-up 4K7 resistors are mandatory on pin 14 and 15 of the PCF8574. This is probably the most common Gotch'ya.

Another common problem is to remember that the I2C address for the PCF8574 (0100+A2,A1,A0) is different to the PCF8574A (0111+A2,A1,A0).


Note: Video loading can be slow depending on server load.

No video's for this topic.


Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with Silver Membership and above can download.


Silver Membership is only $2, so join now!