LCD Display- how to get yours working

There are various LCD displays out there, with little "I2C" boards on them to allow you to control the display with just 4 wires. I have had various problems with them and want to offer you some code.
Here are the problems you might meet
a. You don't have the I2C address.
Every I2C device has an address, eg 0x3F and if you don't know it, you won't be able to talk to your display.
b. The I2C boards have an adjuster on the back to change the contrast.
If the contrast isn't set right, you won't see anything useful.
c. You didn't connect it up correctly,
d. It is dead.

I modified some existing code so that it talked to every possible I2C device, assumed it was an LCD display, and displayed its address on the LCD display, so you can see it! As a backup, in case the contrast is not set properly, it sends the I2C address down the serial port so you can see what it is. If you see an I2C address appearing on the Serial port, adjust your contrast on the back of the display.

Here is the code- feel free to use it. It should work for any size character screen.

/*
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** based on Bitbucket
** 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
** Modified 24/11/15 by Alan Howlett of www.DataTechnologies.co.uk to aid in finding LCD devices on the I2C bus.
** It assumes an LCD might exist on any of the 127 valid addresses and send them a message like "I am on 0x3F".
** If you run this and can't see anything:
** 1. Have a look on Tools, Serial Monitor- do you see a message saying it was found? You might need to wait 30 seconds. If not, check your wiring.
** 2. You might need to adjust the contrast screw on the back of the I2C module on your display to see anything!
** Some of this code copied from the I2C_Scanner utility that some kind anonymous person wrote. Thank you, whoever!
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#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

void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop()
{
// Send a message to every possible LCD display on the I2C bus
for (int address=0; address<127; address++) {
LiquidCrystal_I2C lcd(address,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
lcd.begin (20,4); //(How many columns, how many rows) in your display
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go to top left position
lcd.print("I am on 0x");
lcd.print(address, HEX);

Wire.beginTransmission(address); //Now check the I2C bus communications
int error = Wire.endTransmission(); //Get the error code, if any.

if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
Serial.println("--------------------------------------------------------");
Serial.println("");

digitalWrite(13,LOW);
delay(500);
digitalWrite(13,HIGH);
delay(500);
}

#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

That's a pretty big assumption.

EDIT: Paul will be here shortly to remind you about using code tags.

Don

Sure will! First things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.


Now that said, floresta is way ahead of you! We here have bperrybap's "i2cLCDguesser" code which does the whole job, figuring out not only the I2C address, but the all-important connections in the I2C "backpack" itself according to a number of common patterns.

Unfortunately, it appears you have not researched this sufficiently to find the prior knowledge "guesser" and have struck out on your own somewhat ineffectually. Your code in many or most cases will do little more than Nick's I2C Scanner (read well through that detailed page to find it).