Sub topic 6
Saving Pins with an I2C LCD Backpack
This chapter assumes you can upload an Arduino sketch, use setup() and loop(), wire power and ground on a breadboard, and install libraries with the Arduino Library Manager. It also assumes you understand the basics of I2C and have either run an I2C scanner or know the address of your LCD backpack.
A 16x2 HD44780 LCD normally uses several Arduino pins. An I2C backpack adds an I2C-to-parallel interface chip to the back of the LCD. The Arduino then sends display commands through two signal wires:
SDAcarries data.SCLcarries the clock signal.5VorVCCpowers the module.GNDprovides the shared electrical reference.
On an Arduino Uno, SDA is A4 and SCL is A5. Boards such as the Mega and Leonardo use different pins, so check the board documentation.
Wiring the backpack
Connect the backpack header to the Arduino as follows:
| Backpack pin | Arduino Uno connection | Purpose |
|---|---|---|
| GND | GND | Common reference |
| VCC | 5V | Backpack and LCD power |
| SDA | A4 | I2C data |
| SCL | A5 | I2C clock |
The backpack is usually soldered directly to the 16x2 LCD's 16-pin header. Some modules arrive unsoldered. The backpack's pin labels must match the LCD pins, so inspect the board before applying power.
The backpack commonly includes a small potentiometer for contrast. Turn it slowly with a screwdriver. If the backlight works but no characters appear, contrast is one of the first things to adjust.
Install a compatible library
A common choice is LiquidCrystal_I2C. Several library forks use this name, and their initialization methods are not identical. The example below uses the widely used init() method. If the compiler reports that init() does not exist, read the library's example sketches. The equivalent method may be begin().
Install the library through the Arduino IDE's Library Manager, then select the address discovered by your scanner. 0x27 and 0x3F are common values, but your module may use another address.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Replace 0x27 with the address found by your scanner.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("I2C LCD ready");
lcd.setCursor(0, 1);
lcd.print("Two pins used");
}
void loop() {
}LiquidCrystal_I2C lcd(0x27, 16, 2); creates an LCD object with address 0x27, 16 columns, and 2 rows. lcd.init() configures the backpack and LCD. lcd.backlight() enables the backlight on backpacks that provide that control.
The backpack does not change the LCD's display capacity. A 16x2 module still has 16 character positions on each of two rows. It changes how the Arduino communicates with the LCD and reduces the number of directly occupied pins.
Updating a value
Write changing values at a known position. If a new value can be shorter than the old value, overwrite the remaining characters with spaces.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
}
void loop() {
unsigned long seconds = millis() / 1000;
lcd.setCursor(0, 0);
lcd.print("Uptime: ");
lcd.print(seconds);
lcd.print(" s ");
delay(250);
}The spaces after the value clear leftover characters when the number gets shorter. For more complex screens, print a complete fixed-width line or use lcd.clear() sparingly. Clearing the display on every loop can produce visible flicker because the LCD briefly erases its contents before drawing again.
Common backpack problems
- Backlight on, no text: Adjust the contrast potentiometer and confirm that the backpack is aligned with the LCD header.
- No backlight or response: Check VCC, GND, and the orientation of the backpack pins.
- Scanner finds no address: Recheck SDA and SCL, then verify that the module receives power.
- Scanner finds an address, but the sketch fails: Replace
0x27with the detected address and check the library's initialization method. - Garbled characters: Confirm that the LCD is a 16x2 model and that the backpack is firmly soldered.
Exercise
Why does an I2C backpack reduce the number of Arduino pins needed, and what must you change if your scanner reports address 0x3F instead of 0x27?
Show answer
The backpack converts I2C messages into the LCD's parallel signals, so the Arduino needs only SDA and SCL plus power and ground. Change the constructor address from 0x27 to 0x3F.