ArduinoTutorials

How to use 16×2 I2C LCD with Arduino.

arduino 16x2 i2c lcd

In many projects we need to use a display interface. It is very useful when one needs to visualize data. There are different types of displays for different needs. There are OLED displays, LCD displays and e-ink displays. In this tutorial we are going to learn how to use 16×2 LCD displays which are one of the cheapest and widely used displays in the market.

So without wasting anymore time, lets get started.

16×2 LCD:

16×2 LCD is named so because it has 16 columns and 2 rows. It can display 32 characters in total. Now there are two methods in which we can connect these displays to the microcontroller, with or without I2C module. I2C module allows us to communicate with the display using 2 pins (SDA & SCL).

We will use the I2C display in this tutorial. There are 4 pins on the display namely: GND, VCC, SDA and SCL.
Check out the requirements below.

Disclaimer: This blog contains Amazon Affiliate links. Buying product from those links helps us run this blog without any charges on you.

Requirements:

Connections:

Refer the image for connections.

arduino uno with 16x02 I2C LCD

GND = GND
VCC = 5V
SDA = A4
SCL = A5

Codding:

Now instead of writing complicated codes, we can use a library. In this case we are going to use LiquidCrystal Library by johnrickman

To install the library, just download the zip file and extract the contents. Next copy the file and head over to >> Documents >> Arduino >> Library and paste the file there. Now open the Arduino IDE.
Write the code given below:

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
 {
  // The begin call takes the width and height. This
  // Should match the number provided to the constructor.
  lcd.begin(16,2);

  lcd.init(); //initializes the display
  lcd.backlight(); // Turn on the backlight.

  lcd.setCursor(5, 0); //Column, Row
  lcd.print("HELLO");// Print HELLO to the screen, starting at 5,0.
  
  lcd.setCursor(5, 1); //Column, Row  
  lcd.print("WORLD"); // Print Word to the screen, starting at 5,1.
}

void loop() {}

Upload the code on Arduino and you should see the text “Hello World” on the display.

Troubleshooting:

If you do not see the text for some reason, here are some steps you should try:

  • Recheck the connections, make sure the I2C pins are connected correctly.
  • Power the display with 5V.
  • Try changing the I2C address.

If you don’t know the address of your display, use the following code.

#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
}

Upload the code and open Serial Monitor. After a few seconds, the address of I2C device will be displayed on the screen.

Conclusion:

So now you know how to get a 16×2 Display working with Arduino. You can use a display in many projects as we will see in future. So stay tuned and subscribe if you haven’t already.
If you have questions or doubts, feel free to leave a comment.

Check out other related articles and tutorials at our blog.

Leave a Reply

Your email address will not be published. Required fields are marked *