Introduction to LCDs

An LCD is a great way to communicate a lot of information fast from an Arduino. Combine it with a few buttons, and it suddenly becomes a great control interface for your new machine, whatever it may be. However, LCDs only use 16 pins to communicate with other circuit boards, but the Arduino UNO only has 13 pins. We’re going to need to use something in between to let us do more with less. This is where an I2C backpack comes to the rescue. The only tricky part is that each I2C backpack and LCD are slightly different and require some small tweaks. Today, let’s go through it and show you how the LCD/I2C combo works out.

Check out our final product:

What you’ll need:

  • LCD
  • I2C Backpack
  • Arduino UNO
  • USB Cable
  • Wires
  • Sumotoy’s LiquidCrystalNewV2(found here or here)

Before we go to the code, let’s sort out the hardware first.

My pencil is pointing to the first of 16 through-hole solders that hold the I2C backpack in place. You could also use female herders for a removable option. I’ve colour-coded the wiring exactly how it is in the picture.

lcd and i2c png

LCD library setup

Recall before that each I2C is different and each LCD is different. I’m using Sumotoy’s awesome LiquidCrystalNewV2 libraries, which understands most variations. If you got the code from the first source, put the file into arduino/libraries/gpio_expander. From the second – arduino/libraries/LiquidCrystalNewV2.

Now, let’s go and modify …/LiquidCrystalNewV2/_configurations/pin_config_default.h. Find the lines below and make them the same.

static const byte LCDPIN_EN = 2;       // enable pin
static const byte LCDPIN_EN2 = 1;           // Not used if you have 1 chip LCD's (most ones), 
                                            // leave unconnected
static const byte LCDPIN_RS = 0;            // rs pin
static const byte LCDPIN_D4 = 4;            // data
static const byte LCDPIN_D5 = 5;            // data
static const byte LCDPIN_D6 = 6;            // data
static const byte LCDPIN_D7 = 7;            // data
static const byte LCDPIN_LD = 3;            // Background Led (use a transistor or mosfet!!! 
                                            // see notes)

Please note – if you’re using a different LCD, these changes will be different. The LCD pin names are clearly labelled on the front for you to check.

Lastly, confirm your I2C backpack model. You’ll need this when we get into the software. Look where the model number is on the chip:

My model is PCF8574T.

Software Testing

Let’s start teaching Arduino to display messages for us.

#include <Wire.h>                    // necessary if using LiquidCrystalNew_TWI
#include <LiquidCrystalNew_TWI.h>    // import library

LiquidCrystalNew_TWI lcd(0x27,             // the I2C address
0,
PCF8574);                                  // the I2C chip type

void setup() {
  lcd.begin(20,2);                         // the LCD size (columns, rows)
  lcd.backlight(0);                        // turn light on
  lcd.setCursor(0,0);                      // go to position (x,y)
  lcd.print(" Yay!");
}

void loop() {
  lcd.setCursor(0,1);
  lcd.print("Hello ");
  delay(500);
  lcd.setCursor(0,1);
  lcd.print(" World");
  delay(500);
}

Questions

  • What can the Arduino say to teh LCD? The API can be found in your Arduino library.
  • Try adding a few buttons or a clickable joystick.