Loading...
Pages: [1]   Go Down
Author Topic: [SOLVED] I2C LCD - Setup instructions for 16x2  (Read 3489 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 3
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
/*
** 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);
}



Now compile and you’re away!
« Last Edit: November 15, 2012, 08:55:10 pm by ianbren » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very, VERY grateful for this.  I have been banging my head for hours trying to get a noname ebay LCD working and it turned out to be the i2c address issue you pointed out.  THANK YOU!
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I created an account just so i could log in and say to you THANKYOU for this. it helped me so much and saved me so much time. I now have my 16x2 LCD working nicely.

The only issue i need to solve now is when displaying a value, which is for example being read from an analogue pin, and that value is for example 1234 and then the value drops down to say 99 there seems to be some retention of the previous chars. e.g. in this case i will see 9934 on the display. i need to figure out why the chars arent cleared. oh the fun.

but thanks so much again. your my favourite person at the moment. smiley
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 124
"to make something better we have to break it"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

blackscience, have a look at this topic http://arduino.cc/forum/index.php/topic,94633.msg710615.html#msg710615%20%28http://arduino.cc/forum/index.php/topic,94633.msg710615.html#msg710615. May help you.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pavilion1984. Thanks alot mate, i managed to solve the issue yesterday evening using the exact same method smiley nice to know my solution wasnt a bad idea as i am new to all of this. I thought i was going to have to modify some of the libraries which looked rather daunting.

Code:


 threshold = analogRead (pinA0Value);   // read value of A0

  if (threshold<=999 && threshold>=100) {
      lcd.setCursor (3,1);        // go to start of the second line - 4th char
      lcd.print("    ");           // clear out the thousands char as it is no longer needed
 
  }else if (threshold<=99 && threshold>=10) {
      lcd.setCursor (2,1);        // go to start of the second line - 3rd char
      lcd.print("     ");         // clear out the hundreds char as it is no longer needed

  }else if (threshold<=9 && threshold >=0) {
      lcd.setCursor (1,1);        // go to start of the second line - 2nd char
      lcd.print("      ");        // clear out the tens char as it is no longer needed
  }
 
  lcd.setCursor (0,1);        // go to start of 2nd line
  lcd.print(threshold, DEC);  // print the threshold value
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 124
"to make something better we have to break it"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

blackscience. No problem, happy to help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for these clear instructions, I am sure they will help, but I afraid that I have got stuck on the first step "Rename the existing library" (or words to that effect).

After years using a PC I am sure that I could do it there, but not on my newish iMac!

If I rename the folders there, the IDE still picks up the old library. Any ideas anyone?

Thanks in advance
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-yell

OK, it is done, if anyone out there is trying to help with my MAC query, thanks but somehow I've stumbled on a solution. The LCD now works fine.

And thanks for the original post, could not be more clear.


Logged

Offline Offline
Newbie
*
Karma: 3
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm glad you all found it useful - I also spent evenings banging my head against a wall trying to solve this.
 smiley
Logged

Málaga, Spain
Offline Offline
Edison Member
*
Karma: 33
Posts: 2013
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@ianbren - nice post there, here is a bit of karma for you!

I am glad that you like the library and find it useful.
Logged

   

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome stuff. Great help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks to ianbren. I was stuck with some lcds due to wrong i2c address. Your instructions are clear and comprehensive. Good job.
Logged

Pages: [1]   Go Up
Print
 
Jump to: