Use 16x2 LCD With I2C

26,119

12

7

About: My team has developed a new product. PLEASE CHECK IT OUT ON INDIEGOGO AND SHOW YOUR SUPPORT. CAMPAIGN LINK: https://igg.me/p/2165733/x/17027489. I'm a tech and food enthusiast and also a Soccer Player. I lo...

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

Share

    Recommendations

    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure
    • Arduino Contest 2019

      Arduino Contest 2019

    7 Discussions

    0
    None
    trentl2

    Question 3 days ago

    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

    0
    None
    JeneroseC

    11 days ago

    Stuck on Scanning...

    0
    None
    KayeronneO

    26 days ago

    sketch_mar19a:8:7: error: expected unqualified-id before 'int'
    lcd.int(); // initialize the lcd

    0
    None
    tannenba

    5 weeks ago

    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.

    0
    None
    PatK_

    7 weeks ago

    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

    0
    None
    BharathK87

    Question 2 months ago on Step 4

    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

    0
    None
    GökçerG

    2 months ago on Step 1

    Maybe there is a mistypo. Connection is worked with SDA to A4 and SDC to A5.