How to wire an I2C LCD to an Arduino Mega
To wire an I2C LCD to an Arduino Mega, connect VCC to 5V, GND to GND, SDA to pin 20 and SCL to pin 21. The Arduino Mega is a 5 V board and the backpack runs from 5 V, so its pull-ups hold SDA and SCL at the board’s own level and nothing else is needed. The sketch below uses the LiquidCrystal_I2C (LiquidCrystal_I2C.h) library. It writes the board name on the first line and counts the seconds on the second.
- 5 V logic
- Library: LiquidCrystal_I2C.h
- 4 wires
I2C LCD to Arduino Mega wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| I2C LCD pin | Arduino Mega pin | Wire color | Why |
|---|---|---|---|
| VCC | 5V | red | The display and its backlight run from 5 V. |
| GND | GND | blue | Common ground. |
| SDA | pin 20 | violet | I2C data. |
| SCL | pin 21 | cyan | I2C clock. |
Power and logic levels for I2C LCD and Arduino Mega
Logic level
The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
Supply
The I2C LCD takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.
This pair
On the Mega the I2C bus is pin 20 (SDA) and pin 21 (SCL), not A4 and A5 as on an Uno. The SDA and SCL pins by AREF are the same two nets. The backpack’s own pull-ups hold both lines at 5 V, which is the Arduino Mega’s own logic level, so nothing else is needed.
Pins to leave alone
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
The working code for the Arduino Mega
Library: LiquidCrystal_I2C (LiquidCrystal_I2C.h). This is the sketch the circuit above runs.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// A 16 x 2 display behind a PCF8574 backpack at 0x27.
// SDA pin 20, SCL pin 21.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Arduino Mega");
Serial.println("LCD ready");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("Seconds: ");
lcd.print(millis() / 1000);
lcd.print(" "); // clears the tail of a shorter number
delay(250);
}
What you should see
The first line of the display reads Arduino Mega and the second line reads Seconds: followed by a number that goes up once a second. The serial monitor, at 9600 baud, prints LCD ready once, after the display has been set up.
A blank screen with the backlight on means the contrast is too low or the address is wrong. This circuit answers at 0x27, the address most backpacks ship with.
The serial monitor prints this, recorded from the circuit above running:
LCD readyCommon mistakes wiring an I2C LCD to an Arduino Mega
The wrong address
Backpacks with the PCF8574T chip answer at 0x27; those with the PCF8574AT answer at 0x3F. The screen lights but shows nothing when the address is wrong. Run an I2C scanner, or set the part’s address property in the editor to match yours.
Contrast turned all the way down
A row of solid blocks, or nothing at all with the backlight on, is usually the blue trimmer on the backpack. Turn it until the text appears. In the simulator the part has a contrast property for the same thing.
Using A4 and A5
An Uno tutorial says SDA on A4 and SCL on A5. On the Mega those are plain analog pins; the bus is on 20 and 21. The display lights and shows nothing until the wires move.
Build your own I2C LCD project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.