Arduino Mega - LCD 20x4

This guide shows you how to use a 20x4 LCD screen with an Arduino Mega board through an I2C connection. In detail, we will learn:

Arduino Mega and LCD I2C 20x4

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×LCD 20x4
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of LCD I2C 20x4

Pinout

The 20x4 I2C LCD uses the I2C bus and has 4 pins:

  • GND pin: connect to ground (0V).
  • VCC pin: connect to +5V to power the LCD.
  • SDA pin: I2C data line.
  • SCL pin: I2C clock line.
LCD 20x4 I2C Pinout

LCD Coordinate

The I2C LCD 20x4 screen has 20 columns and 4 rows. The columns and rows start at 0.

Arduino Mega LCD I2C Coordinate

Wiring Diagram

The wiring diagram between Arduino Mega LCD 20x4 I2C

This image is created using Fritzing. Click to enlarge image

LCD I2C Arduino Mega, Nano Arduino Mega
Vin 5V 5V
GND GND GND
SDA A4 20
SCL A5 21

How To Program For LCD I2C

It's easy to use the LCD with the LiquidCrystal_I2C library.

  • Install the library:
#include <LiquidCrystal_I2C.h>
  • Create a LiquidCrystal_I2C object using the I2C address, the number of columns, and the number of rows:
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD over I2C at address 0x27 with 20 columns and 4 rows
  • Set up the LCD display.
lcd.init(); // Initialize the LCD module lcd.backlight(); // Enable the backlight
  • Put the cursor at the place given by column_index and row_index.
lcd.setCursor(column_index, row_index);
  • Show a message on the LCD screen.
lcd.print("Hello World!");

※ NOTE THAT:

Different brands use different I2C addresses for the LCD. In our example, we used 0x27, the address given by the maker DIYables (https://diyables.io/products/lcd-20x4-display-i2c-interface).

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-lcd-20x4 */ #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 20, 4); // Configure LCD object for a 20x4 display using I2C address 0x27 void setup() { lcd.init(); // Initialize the display driver for the LCD lcd.backlight(); // Enable the LCD backlight lcd.setCursor(0, 0); // Move cursor to row 0, column 0 lcd.print("LCD 20x4"); // Write "LCD 20x4" on the top line lcd.setCursor(0, 1); // Move cursor to row 1, column 0 lcd.print("I2C Address: 0x27"); // Show the address on the second line lcd.setCursor(0, 2); // Move cursor to row 2, column 0 lcd.print("DIYables"); // Print the brand name on the third line lcd.setCursor(0, 3); // Move cursor to row 3, column 0 lcd.print("www.diyables.io"); // Print the URL on the bottom line } void loop() { // TODO }

Detailed Instructions

Follow these steps one by one.

  • Connect the LCD I2C 20x4 to the Arduino Mega according to the provided diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Select the correct Arduino Mega board and the serial port.
  • Click the Libraries icon on the left side of the Arduino IDE.
  • Type "LiquidCrystal I2C" and find the LiquidCrystal_I2C library by Frank de Brabander.
  • Click Install to add the LiquidCrystal_I2C library.
Arduino Mega LiquidCrystal I2C library
  • Copy the code shown above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Check the LCD to see the result.
  • Try changing the text and where it appears.

Your Arduino Mega board might not give the LCD enough power. If the LCD is not bright or can't display characters, use an external power source for the LCD.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!