I2C LCD - Setup instructions for 16x2

(Read 283 times)
I2C LCD - Setup instructions for 16x2 on: March 07, 2020, 08:51:28 AM
Like many on this discussion group, I bought an I2C LCD device for my Arduino only to find that the documentation is either non-existent or, if it does exist, just wrong.

Having spent the last few nights trying out all the options I could find, I thought I'd pull together the steps I used to get my LCD going.

This is an amalgamation of a number of threads and I'll acknowledge as I go along.

Firstly, the LCD panel I have is an I2C from sainsmart.  It's a 16x2 LCD based on the 1602 panel with a small back-panel to convert it to I2C.  It's shown here http://farm9.staticflickr.com/8046/8115486011_a99d721319_b.jpg for clarity. 

Step 1 - collect your tools.

There's a new LCD library that F Malpartida has created at https://bitbucket.org/fmalpartida/new-liquidcrystal

This tutorial is based on version LiquidCrystal_V1.2.1.zip

This library REPLACES the standard one in Arduino V1.0.  So
-   rename the existing LCD library directory
-   unzip the new library
-   place the folder LiquidCrystal into the library folder.
-   Start up the Arduino IDE

Step - 2 Wire up the I2C LCD panel to the Arduino.
Reviewing the photo at http://farm9.staticflickr.com/8046/8115486011_a99d721319_b.jpg you can see the four wires attached to the top of the device.  The connections, from left to right, are GND, VCC, SDA and SCL.  These latter two are the I2C leads.

My Arduino is an Uno, so the I2C connections are on SDA=A4 and SCL=A5.  So go ahead and wire these up, along with the two power leads to the 5V and GND terminals.

Step 3 - Power up your devices. 

You should see the LCD light up.  Depending on how the device was constructed, you might want to turn down the contrast of the LCD; you can do this by inserting a screwdriver into the potentiometer at the back.  I suggest you turn it half-way so that there's still a little contrast.

Step 4 - find the I2C address
Each device has an I2C address that it uses to  accept commands or send messages.   Unfortunately this is one of the areas where the documentation falls down.  So let's go and look for the one on your device.

Load the sketch over at http://arduino.cc/playground/Main/I2cScanner and follow the instructions to use it.  By opening up the monitor window after you upload the sketch, Arduino will scan the address range looking for a reply.  Even though the documentation said it was 0x27, this scanner detected it at 0x3F… so I was never going to find it based on the documentation!

Write down the Address that you have found, you'll need it in the next step.



Step 5 - Fire up the LCD
OK, so now you know that you have a device that works and is responding on the I2C bus.  Now we'll light this up.

The next bit of code is based on Edward Comer's project over at
https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino

Load this up into your IDE, but before you upload it to the Arduino, check two things

The #define I2C_ADDR on line 21 should be set to the value you got on Step 4 above (0x3F in my case).

Line 37 should be reviewed for for the size of lcd.begin (20,4);

For completeness, here's his sketch modified for my 16x2 LCD


/*
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** based on https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino
** by
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)

** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
** https://bitbucket.org/fmalpartida/new-liquidcrystal

** Modified - Ian Brennan ianbren at hotmail.com 23-10-2012 to support Tutorial posted to Arduino.cc

** Written for and tested with Arduino 1.0
**
** NOTE: Tested on Arduino Uno whose I2C pins are A4==SDA, A5==SCL

*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int n = 1;

LiquidCrystal_I2C   lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
 lcd.begin (16,2); //  <<----- My LCD was 16x2

 
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home

 lcd.print("SainSmartI2C16x2"); 
}

void loop()
{
 // Backlight on/off every 3 seconds
 lcd.setCursor (0,1);        // go to start of 2nd line
 lcd.print(n++,DEC);
 lcd.setBacklight(LOW);      // Backlight off
 delay(3000);
 lcd.setBacklight(HIGH);     // Backlight on
 delay(3000);
}