Arduino Mega - LCD I2C

This guide shows you how to use a 16x2 LCD with an I2C connection on an Arduino Mega. In simple terms, we will learn:

Arduino Mega and 16x2 LCD I2C

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 I2C
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 16x2

The 16x2 LCD I2C is a small screen with 16 columns and 2 lines. It has an I2C connection and a small knob to adjust the contrast.

Pinout

The LCD I2C uses an I2C bus and has 4 pins.

  • GND pin: connect to ground (0 volts).
  • VCC pin: connect to power (5 volts).
  • SDA pin: carries the I2C data line.
  • SCL pin: carries the I2C clock line.
LCD I2C Pinout

LCD Coordinate

The LCD I2C 16x2 screen has 16 columns and 2 rows. The columns and rows both start counting from 0.

Arduino Mega LCD I2C Coordinate

Wiring Diagram

The wiring diagram between Arduino Mega LCD 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

The LiquidCrystal_I2C library makes the LCD easy to use.

  • Add the library:
#include <LiquidCrystal_I2C.h>
  • Make a LiquidCrystal_I2C object by giving its I2C address, the number of columns, and the number of rows.
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize a 16x2 LCD via I2C using address 0x27
  • Set up the LCD display.
lcd.init(); // Set up the LCD module lcd.backlight(); // Enable the LCD backlight
  • Put the cursor at the spot you chose (column_index, row_index).
lcd.setCursor(column_index, row_index);
  • Show a message on the LCD screen.
lcd.print("Hello World!");

We can do lots more with an LCD. See the Do More with LCD section.

※ NOTE THAT:

The I2C address of the LCD can be different depending on who made it. In our code, we used 0x27, as the manufacturer says (DIYables: https://diyables.io/products/lcd-i2c-16x2).

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-i2c */ #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 lines void setup() { lcd.init(); // Initialize the LCD lcd.backlight(); lcd.setCursor(3, 0); // Set cursor to column 3, row 0 lcd.print("DIYables"); // Display title text on the screen lcd.setCursor(0, 1); // Move cursor to the start of the second line lcd.print("www.diyables.io"); // Show the website URL on the second line } void loop() { }

Detailed Instructions

Do these steps one by one.

  • Connect the LCD I2C display to the Arduino Mega according to the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose Arduino Mega as the board and select the COM port.
  • Click the Libraries icon on the left side of the IDE.
  • In the search box, type "LiquidCrystal I2C" and find the LiquidCrystal_I2C library by Frank de Brabander.
  • Click Install to install the LiquidCrystal_I2C library.
Arduino Mega LiquidCrystal I2C library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
  • See the result on the LCD screen.
  • Try changing the text and where it is.

Video Tutorial

Do More with LCD

Custom Character

To show special symbols like a heart or an angry bird on an LCD screen, you need to use the character generator, because lcd.print() can only display ASCII characters.

An LCD 16x2 display can show 32 characters in total on 2 lines, with 16 characters on each line. Each character uses 40 small dots, arranged in 8 rows and 5 columns.

Arduino Mega LCD 16x2 Pixel

The character maker creates a character (40 pixels). Just follow these steps:

Click on each pixel to select/deselect


Copy below custom character code
Replace the customChar[8] in the below code
#include <LiquidCrystal_I2C.h> // Create LCD object for I2C backpack at address 0x27 with 16 columns and 2 rows LiquidCrystal_I2C lcd(0x27, 16, 2); // Custom 8-byte bitmap to define a LCD character glyph byte customChar[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 }; void setup() { lcd.init(); // Initialize LCD for operation lcd.backlight(); // Enable the backlight lcd.createChar(0, customChar); // Load the glyph into CGRAM as character 0 lcd.setCursor(2, 0); // Move cursor to column 2, row 0 lcd.write((byte)0); // Print the custom character at the cursor } void loop(){ }

Result shown on the screen:

LCD custom character

Multiple custom characters

We can create up to eight special characters (numbered from 0 to 7). The example below shows how to create and show three characters.

#include <LiquidCrystal_I2C.h> // Create an LCD object, using I2C address 0x27 with 16 columns and 2 rows LiquidCrystal_I2C lcd(0x27, 16, 2); // Define custom character 0 (heart shape) byte customChar0[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 }; // Define custom character 1 (arrow pointing upwards) byte customChar1[8] = { 0b00100, 0b01110, 0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100 }; // Define custom character 2 (arrow pointing downwards) byte customChar2[8] = { 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b11111, 0b01110, 0b00100 }; void setup() { lcd.init(); // Initialize the LCD lcd.backlight(); // Turn on the LCD backlight // Register custom char 0 at LCD index 0 lcd.createChar(0, customChar0); // Register custom char 1 at LCD index 1 lcd.createChar(1, customChar1); // Register custom char 2 at LCD index 2 lcd.createChar(2, customChar2); // Move cursor to column 2, row 0 lcd.setCursor(2, 0); // Print customChar0 at current cursor position lcd.write((byte)0); // Move cursor to column 4, row 0 lcd.setCursor(4, 0); // Print customChar1 at current cursor position lcd.write((byte)1); // Move cursor to column 6, row 0 lcd.setCursor(6, 0); // Print customChar2 at the current cursor position lcd.write((byte)2); } void loop() { }

Result shown on the LCD screen:

LCD multiple custom characters

Summary: how to use custom character on LCD

  • Use the tool above to create the binary code for your character.
  • Write down the binary code for your character (copy it from the previous step).
byte customChar[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 };
  • Create your own character and place it at a number between 0 and 7 in the setup() function.
lcd.createChar(index, customChar);
  • Show a special symbol on the LCD whenever you want, in setup() or in loop().
lcd.setCursor(column, row); // Move to the specified column and row lcd.write((byte)index); // Output the custom character from CGRAM slot 'index'

Other functions

Add these functions to the loop() function one at a time, and pause for five seconds after each function.

  • Clear LCD screen
lcd.clear();
  • Put the cursor in the top left corner of the screen.
lcd.home();
  • Move the cursor to a specific position (column and row).
lcd.setCursor(column, row);
  • Show the cursor on the LCD screen.
lcd.cursor();
  • Turn off the cursor on the LCD screen.
lcd.noCursor();
  • Show the blinking cursor on the LCD screen.
lcd.blink()
  • Stops the blinking cursor on the LCD screen.
lcd.noBlink()
  • Get more information from the LiquidCrystal Library Reference: https://arduinogetstarted.com/reference/library/arduino-lcd-library

Troubleshooting on LCD I2C

If the text does not show on the LCD I2C, please check these things:

  • Change the LCD brightness by turning the small pot on the back.
  • The LCD's I2C address can be different for different brands. It is usually 0x27 or 0x3F. Try these addresses one by one. If neither works, use the following code to find the correct I2C address.
// I2C address scanner for discovering devices on the I2C bus #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found"); else Serial.println("done"); delay(5000); // pause 5 seconds before the next scan }

The result shown on the Serial Monitor:

COM6
Send
Scanning... I2C device found at address 0x3F ! done Scanning... I2C device found at address 0x3F ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 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!