Use 16x2 LCD With I2C

97K2724

Intro: Use 16x2 LCD With I2C

In one of my previous Instructable, I showed you how to connect and interface an LCD to Arduino Uno and display values on it. But as observed, there were a lot of connections and if the project started getting a lot messy due to too many wires.

In this Instructable, I'll show you how to connect an LCD to an I2C, which will have only 4 pins to control and use the LCD. So let's get started.

STEP 1: Components Required

For this project you will need :

  • Arduino Uno
  • 16x2 LCD
  • I2C for 16x2 LCD
  • Hook-Up wires

With all these, let's get into the connections part.

STEP 2: Connections

Refer the pictures and make the connections. It's quite simple, you just have to plug in the I2C in the ports of the LCD and solder it into place. Then connect the SCL pin to A4 pin on the Arduino and the SDA pin to the A5 pin on the Arduino.

I'm not soldering the I2C as I have already soldered header pins on the LCD. But I would suggest soldering the I2C on the LCD.

STEP 3: Code

There is an LCD I2C master library included in the Arduino IDE. But there's a slight problem with the code in it. All the examples in this library assumes the default address of the I2C as 0x27. So first we have to find out what the address of our I2C is. We can do this by using the I2C scanner code. Once we have the I2C address we can replace this value in the example code and start using it.

I2C Scanner Code:

#include <Wire.h> 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             //  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
}

Example Code(To display characters entered in Serial Monitor):

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() { lcd.init(); // initialize the lcd lcd.backlight(); Serial.begin(9600); }

void loop() { // when characters arrive over the serial port... if (Serial.available()) { // wait a bit for the entire message to arrive delay(100); // clear the screen lcd.clear(); // read all the available characters while (Serial.available() > 0) { // display each character to the LCD lcd.write(Serial.read()); } } }


STEP 4: The Output

Once you have uploaded the code, you are ready to go. For this Instructable I have taken the example of the Serial Print Code. So now after uploading the code, open the Serial Monitor and type a word and click "send". Now you should see this value getting displayed on the LCD.

That's All Folks !! Stay Tuned For More !!

10 Comments

there was a change in the LiquidCrystal_I2C library, need to change lcd.int(); to lcd.begin(); in the code.
Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "Arduino Uno"
C:\Users\Ian Fenner\Documents\Arduino\libraries\LiquidCrystal\I2CIO.cpp:35:10: fatal error: ../Wire/Wire.h: No such file or directory
#include <../Wire/Wire.h>
^~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board Arduino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
The connection is written wrong...
Works only first line...
Also, there are 4-line character after every given text...!!!
so i got it working but can only display text on the first line so how would you get text to second line on the lcd
sketch_mar19a:8:7: error: expected unqualified-id before 'int'
lcd.int(); // initialize the lcd
Yep A4 and A5 swapped. The scanner finds my address, but the other doesn't print a thing. Found the sketch was for a 20 x 4 at a different address...Unexpected when the demo here is 16 x 2. Corrected that and played with the contrast, and it runs now.
Can you tell me the source for the LiquidCrystal_I2C.h you are using? The one I've found with the Arduino IDE "Manage Libraries" tool says it may not work with current sketches and using your sketch above all my LCD does is blink off and back on once and does not display anything from the serial monitor input. Nor will it display anything if I clear the entire loop() and just enter a fixed write statement in the setup(). Thanks
Hi,
I did the same connections as you did. the lcd power is on but its not showing anything.

Actually, for checking the i2c address also, the serial monitor is not showing anything, at least it has to show no of connections also, but i cant see anything.

The lcd is getting on but not displaying anything on the lcd screen. please help me

I'm using Olimexino-32u4 arduino board
Maybe there is a mistypo. Connection is worked with SDA to A4 and SDC to A5.