Sub topic 6
Saving pins with an I2C LCD backpack
A 16x2 character LCD wired in 4-bit mode uses six Arduino pins: RS, E, and D4 to D7. On an Uno that is nearly half of the digital pins. An I2C backpack is a small board soldered to the back of the LCD. It reduces the display connection to two signal wires, and those two wires can be shared with other I2C devices. This chapter covers how the backpack works, how to wire it, how to run it, and how to diagnose the failures it tends to produce.
I assume you have completed the parallel LCD chapter and the two I2C chapters before this one. You should know what lcd.setCursor() and lcd.print() do, what SDA, SCL and a 7-bit address are, and how to find an address with the scanner sketch. You should also be able to install a library with the Library Manager.
What the backpack contains
Most backpacks are built around an I/O expander chip, the PCF8574 (or its sibling, the PCF8574A). The chip has eight output pins and an I2C interface. The Arduino sends one byte over I2C, and the chip sets its eight outputs to match the bits of that byte. Those eight outputs are wired to the LCD like this:
| PCF8574 pin | Connected to | Purpose |
|---|---|---|
| P0 | RS | Selects command or data |
| P1 | RW | Tied to write; the library never reads |
| P2 | E | Enable strobe |
| P3 | Backlight transistor | Backlight on or off |
| P4 to P7 | D4 to D7 | 4-bit data bus |
This mapping is common but not universal. A few boards use a different wiring, and then a library written for the usual layout shows garbage even though the address is correct.
The backpack is doing the same job your six jumper wires did. The library toggles the same RS, E and data lines, but each change is a short I2C transmission instead of a pin write. The cost is speed. Each LCD operation needs several I2C bytes, so the backpack is slightly slower than direct wiring. For a display that shows a few values per second, you will not notice.
The board also carries a small blue trimmer potentiometer for contrast, a jumper that connects the backlight, and three solder pads labeled A0, A1 and A2 that set the address.
Wiring
The backpack has four pins: GND, VCC, SDA and SCL. Connect them as follows.
| Backpack pin | Uno / Nano | Mega 2560 | Leonardo |
|---|---|---|---|
| GND | GND | GND | GND |
| VCC | 5V | 5V | 5V |
| SDA | A4 | 20 | 2 |
| SCL | A5 | 21 | 3 |
On newer Uno R3 boards, dedicated SDA and SCL header pins next to AREF are wired to the same lines as A4 and A5. Use whichever is convenient.
Power the backpack from 5 V. Most HD44780 character LCDs are 5 V parts, and their contrast and logic levels are not reliable at 3.3 V. If your board is a 3.3 V board, check the LCD datasheet before connecting, and use a level shifter on SDA and SCL if the backpack is powered at 5 V.
Backpacks usually include pull-up resistors on SDA and SCL, often 4.7 kΩ. That is normally enough for one device. If you add several modules that each have their own pull-ups, the parallel resistance drops and can become too low for the bus to drive. A scanner that finds nothing, or a bus that sticks low, is a hint to check this.
Finding the address
Run the scanner from the previous chapter before you write the main sketch. The backpack address depends on which PCF8574 variant is on the board and on the A0 to A2 pads.
| Chip | Address range | Default with open pads |
|---|---|---|
| PCF8574 | 0x20 to 0x27 | 0x27 |
| PCF8574A | 0x38 to 0x3F | 0x3F |
With the pads unsoldered, pull-up resistors hold A0 to A2 high, which gives the top address in each range. Bridging a pad with solder pulls that address bit low. Bridging all three pads on a PCF8574 gives 0x20. This is how you run several backpacks on one bus, which is useful when you want two LCDs from the same two pins.
If the scanner prints Device found at 0x27, use 0x27 in your sketch. If it prints nothing, fix the wiring before touching the code.
The basic sketch
Install the library named LiquidCrystal I2C from the Library Manager. Several packages share this name, and their methods differ slightly. The version by Frank de Brabander is the most widespread and is the one assumed here.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Use the address found by the scanner; 0x27 and 0x3F are common.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C LCD ready");
}
void loop() {}The constructor takes the address, the number of columns and the number of rows. A 20x4 display would be LiquidCrystal_I2C lcd(0x27, 20, 4);. Getting the dimensions wrong does not cause an error. It makes the row addressing wrong, so text appears on the wrong line or is cut off.
lcd.init() starts the I2C bus and runs the HD44780 initialization sequence. lcd.backlight() sets the P3 bit so the backlight transistor conducts. Without it the screen may be on but hard to see, and you may wrongly conclude it is dead.
Some forks of this library expect lcd.begin() or lcd.begin(16, 2) instead of lcd.init(). If the compiler says 'class LiquidCrystal_I2C' has no member named 'init', you have a different fork. Check the examples that ship with the library you installed, because they match its API.
Same API, different constructor
After construction, the calls are the same as for the parallel LiquidCrystal library: print(), setCursor(), clear(), createChar(), scrollDisplayLeft() and others. This makes porting a sketch easy. Change the include, change the constructor, replace lcd.begin(16, 2) with lcd.init() and add lcd.backlight(). The rest of the code stays as it was.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Uptime (s):");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
lcd.print(" "); // overwrite digits left by a longer earlier value
delay(250);
}The trailing spaces matter. The LCD keeps whatever characters it already has, so a value that shrinks (say from 100 to 99) would leave a stale digit behind. Writing spaces after the value erases it without calling lcd.clear(), which is slow and makes the screen flash.
Contrast and backlight
The trimmer on the backpack sets the LCD's contrast voltage. Two typical symptoms follow from a wrong setting:
- Too low or too high: the backlight is on and the glass looks empty, or the whole top row shows solid blocks.
- Correct: characters show clearly against the background.
Turn the trimmer slowly with a small screwdriver while the sketch is running. The useful range is narrow, so a quarter turn can move you from blank to solid.
The backlight jumper on the backpack connects the LED supply. Removing it disconnects the backlight completely, which reduces power draw if you want the display on but dim to save battery. In software, lcd.noBacklight() turns the backlight off through P3 and lcd.backlight() turns it on again. The backlight is the largest consumer of current on these modules, typically some tens of milliamps, so turning it off after a timeout is a worthwhile habit in battery projects.
Parallel wiring versus a backpack
| Aspect | 4-bit parallel | I2C backpack |
|---|---|---|
| Arduino pins used | 6 | 2, shared with other I2C devices |
| Extra hardware | Contrast potentiometer, resistor for backlight | Included on the backpack |
| Speed | Faster | Slower, but enough for text |
| Wires | About 12 for the full circuit | 4 |
| Failure causes | Wiring mistakes on any of 6 lines | Address, library fork, contrast |
The backpack wins whenever pins or wiring effort matter. Parallel wiring still makes sense when you need the highest refresh speed or have pins to spare and no backpack.
Common errors
- Blank screen with backlight on. Turn the contrast trimmer first. If you see rows of blocks, contrast is the likely cause.
- Nothing at all. Run the scanner. No result means a wiring or power problem. A result means the address in your sketch probably differs from the one found.
- Compile error on
init()orbegin(). You have a different library fork. Use the examples that come with the installed library. - Text on the wrong rows, or cut off. The row and column numbers in the constructor do not match the display.
- Garbled characters. Check for loose jumper wires, and check that the backpack uses the standard pin mapping. A cable on a breadboard that is only half pushed in can cause intermittent garbage.
- Display works alone but fails with other I2C modules. Two devices may share the same address. Change the address with the A0 to A2 pads, or pick a different module.
Exercise
Your scanner reports a device at 0x3F, but your sketch uses LiquidCrystal_I2C lcd(0x27, 16, 2); and the screen stays blank with the backlight on. What do you change, and why can't adjusting contrast fix it?
Show answer
Change the constructor to LiquidCrystal_I2C lcd(0x3F, 16, 2);. With the wrong address, the Arduino sends its bytes to a device that does not exist, so the LCD never receives initialization or text. Contrast only changes how visible characters are. It cannot create characters that were never sent.
Exercise
A counter on row 2 shows 1000, then the value drops to 999 and the display reads 9990. What causes this, and how do you fix it without calling lcd.clear()?
Show answer
The LCD keeps the old fourth digit because the new, shorter text does not overwrite it. After printing the value, print spaces to cover the old width (for example lcd.print(" ");), or print the number into a padded buffer. This avoids the flash that lcd.clear() causes.
Exercise
You want two 16x2 backpacks on the same Uno. Which two hardware connections are shared, and what must differ between the modules?
Show answer
SDA and SCL (plus power and ground) are shared by both modules. Each module must have a different I2C address, which you set by bridging different combinations of the A0 to A2 solder pads. Each sketch object then uses its own address in the constructor.