How to control I2C LCD with arduino

This article includes everything you need to know to get started with a I2C LCD with Arduino. I have included a wiring diagram and sample codes.


In this tutorial I will cover how to display numeric and alphabetical characters on different locations on the screen. Secondly, I will go into detail on the process of creating custom characters and displaying animations

I2C lcd vs standard lcd

A standard lcd module has 16 pins, thus harder to wire, and can be problematic, that's where the I2C module comes in, it can be attached to any standard lcd module but it provides the functionality to control the display with only 2 pins, SDA and SCL which is serial data and serial clock. The I2C module also has a potentiometer onboard which you can use to set the brightness of the backlight.

Wiring

Before we get into wiring the I2C and power pins, we first have to provide backlight power to the lcd, and in order to do that we have to short the pins in the picture (to the left).

After that's done, we need to wire the power and I2C pins, and the connections are in this table (bottom). Once the arduino has power your display's backlight should turn on (troubleshooting tips at the end of the page) . The second table is the SDA and SCL pins of a few different types of arduinos

Finally, we have to download the i2c lcd library to use the lcd with the i2c backpack.

Programming

This example will perform a few possible functions. Firstly, we need to import the I2C lcd library and wire library. We then, create an lcd object. The parameters of this object are (address, columns, rows) which we passed in as (0x27, 16, 2) the address might be different for you so, I will include an arduino program to find your i2c address. We then call the lcd.begin() function which passes in the parameter we defined earlier to the display after which, we clear the lcd and turn on its backlight with the clear() and backlight() functions. The blink() function is used for displaying a blinking cursor and noBlink() will turn it off. The cursor() function is used for displaying an underscore and noCursor() for turning it off


/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/


#include <Wire.h> // importing the wire library

#include <LiquidCrystal_I2C.h> // importing the I2C lcd library


LiquidCrystal_I2C lcd(0x27, 16, 2); // instantiating of the I2C lcd with the parameters(address, columns, rows)


void setup()

{

Serial.begin(9600); // beginning serial communication at 9600 baud rate

lcd.begin(); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

lcd.clear(); // clearing the lcd

lcd.backlight(); // turning on the backlight

lcd.print("Rohan Katreddy");

lcd.setCursor(1,1); // sets cursor to the 2nd column and 2nd row

lcd.print("justknowhow.ca");

delay(4000); // wait 4000 milliseconds

lcd.clear();

lcd.print("Running");

}


void loop()

{

lcd.setCursor(10,0); // Sets the cursor for upcoming text

delay(1500);

lcd.clear(); // Clears the display

lcd.blink(); //Displays the blinking LCD cursor

delay(5000);

lcd.noCursor(); // lcd cursor turns off

lcd.clear(); // Clears the screen

delay(3000);

lcd.noBlink(); // blinking cursor turns off

lcd.setCursor(10,1);

delay(1500);

lcd.cursor();

}


The second example code below will display what you write on the serial monitor on the lcd. For this example you have to have the arduino plugged in to a computer so that, the arduino can communicate with the computer using serial communication.


/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/


#include <Wire.h> // importing the wire library

#include <LiquidCrystal_I2C.h>// importing the I2C lcd library


LiquidCrystal_I2C lcd(0x27, 16, 2); // instantiating of the I2C lcd with the parameters(address, columns, rows)


void setup()

{

Serial.begin(9600); // beginning serial communication at 9600 baud rate

lcd.begin(); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

lcd.clear(); // clearing the lcd

lcd.backlight(); // turning on the backlight

lcd.print("Rohan Katreddy");

lcd.setCursor(1,1); // sets cursor to the 3 column and 2 row

lcd.print("justknowhow.ca");

delay(4000); // wait 4000 milliseconds

lcd.clear();

}


void loop()

{

if (Serial.available()) { // check for incoming data at serial port

delay(100);

lcd.clear();

while (Serial.available() > 0) {

lcd.print(Serial.read()); // print incoming data if any

}

}}


The third example below is for displaying custom characters. The lcd supports up to 8 characters of 5x8 pixels. In the setup we create a character with the createChar(), the parameters of this function are a number between 1 and 8 as well as the name of the array of bytes. A number between 1 to 8 is required because it reserves one of the 8 slots for custom characters. We write the character by using the write() function. NOTE: The begin() function must be called before the createChar() function so that the lcd is initialized first.


/*

* Arduino I2C LCD Tutorial

*

* program by Rohan Katreddy

* www.justknowhow.ca

*

*/


#include <Wire.h> //importing the wire library

#include <LiquidCrystal_I2C.h> // importing the I2C lcd library



byte smile[8]= { // Array of bytes

B00000, // B stands for binary formatter

B01010, // the 5 numbers are the pixels

B00000,

B10001,

B01110,

B00000,

B00000,

B00000

};


byte sad[8]= { // Array of bytes

B00000, // B stands for binary formatter

B01010, // the 5 numbers are the pixels

B00000,

B01110,

B10001,

B00000,

B00000,

B00000,

};



LiquidCrystal_I2C lcd(0x27, 16, 2);// instantiating of the I2C lcd with the parameters(address, columns, rows)


void setup()

{

Serial.begin(9600); // beginning serial communication at 9600 baud rate

lcd.begin(); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

lcd.createChar(0, smile);// creates the character happy using the predefined byte value

lcd.createChar(1, sad);// creates the character sad using the predefined byte value

lcd.clear(); // clearing the lcd

lcd.backlight();// turning on the backlight

lcd.print("Rohan Katreddy");

lcd.setCursor(1,1); // sets cursor to the 3 column and 2 row

lcd.print("justknowhow.ca");

delay(4000); // wait 4000 milliseconds

lcd.clear();

}


void loop()

{

lcd.setCursor(7,0);

lcd.clear();

lcd.write(0); // writes the character assigned to 0

delay(1000);

lcd.clear();

lcd.write(1); // writes the character assigned to 1

delay(1000);

}

I2C Scanner

This code is from Arduino playground

// --------------------------------------

// i2c_scanner

//

// Version 1

// This program (or code that looks like it)

// can be found in many places.

// For example on the Arduino.cc forum.

// The original author is not know.

// Version 2, Juni 2012, Using Arduino 1.0.1

// Adapted to be as simple as possible by Arduino.cc user Krodal

// Version 3, Feb 26 2013

// V3 by louarnold

// Version 4, March 3, 2013, Using Arduino 1.0.3

// by Arduino.cc user Krodal.

// Changes by louarnold removed.

// Scanning addresses changed from 0...127 to 1...119,

// according to the i2c scanner by Nick Gammon

// https://www.gammon.com.au/forum/?id=10896

// Version 5, March 28, 2013

// As version 4, but address scans now to 127.

// A sensor seems to use address 120.

// Version 6, November 27, 2015.

// Added waiting for the Leonardo serial communication.

//

//

// This sketch tests the standard 7-bit addresses

// Devices with higher bit address might not be seen properly.

//

#include <Wire.h>

void setup()

{

Wire.begin();

Serial.begin(9600);

while (!Serial); // Leonardo: wait for serial monitor

Serial.println("\nI2C Scanner");

}

void loop()

{

byte error, address;

int nDevices;

Serial.println("Scanning...");

nDevices = 0;

for(address = 1; address < 127; address++ )

{

// The i2c_scanner uses the return value of

// the Write.endTransmisstion to see if

// a device did acknowledge to the 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\n");

else

Serial.println("done\n");

delay(5000); // wait 5 seconds for next scan

}


Troubleshooting tips:

  • My lcd backlight does not turn on?

Check your power and backlight power pins,

switch your jumper cables,

connect your VCC of the lcd to 3.3v on the arduino, if that works, then that might mean your 5v is not outputting power.


  • Lcd powers up but does not display anything?

If you are using a different arduino then your sda and scl pins might be different. Check for you pins.


  • My custom characters are not being displayed or they are not displaying it correctly?

In your code, make sure your begin() function is called before your createChar() function, this ensures your lcd is initialized before creating characters.


  • Arduino powers up but not lcd?

Try the ones above. If one of the above solutions work then you may have a faulty lcd or i2c backpack.


  • Arduino does not power up?

You may have a faulty arduino


My Other Tutorials: