SainSmart 20 x 4 LCD wanting to use I2C from Arduino Uno

I appreciate the help guys.....I found a similar thread on a different library a guy used so I'm going to check into that....everything here is close but no cigar......its definitely 0x3F as well....no matter their website......labor day amend.....gonna break for now

Did you try the code posted changing the address.

Hey buddy,

Try the code below. I was having the exact same problem, but its working nicely with this example.

As mentioned below by Don, static display should go in setup(), and variables should go in loop().

This info is also helping me with my own project :slight_smile: Appreciate the feedback. Will be going from Arduino n00b to competent coder in no time.

Let me know if you have any questions.

Cheers,

Andrew.

/*
** Example Arduino sketch for SainSmart I2C LCD2004 adapter for HD44780 LCD screens
** Readily found on eBay or http://www.sainsmart.com/ 
** The LCD2004 module appears to be identical to one marketed by YwRobot
**
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)
**
** sain_lcd_2.ino
** Simplified and modified by Andrew Scott for Arudino 1.0.1, Arudino Uno R3.
** NOTE: I2C pins are A4 SDA, A5 SCL
** Don't forget to use the new LiquidCrystal Library.
*/



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

#define I2C_ADDR    0x3F  // Define I2C Address where the SainSmart LCD is
#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

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

void setup()
{
  lcd.begin (20,4);
  
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);

  // Position cursor and write some text
  lcd.home ();                   // go to the first line, first character
  lcd.print("SainSmart I2C tester");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("It's Working!");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Sainsmarts docs suck");
  lcd.setCursor ( 0, 3 );        // go to the fourth line
  lcd.print("Nice LCD Though. ");
}

void loop()
{

}

Moderator edit: colour tag swapped for code tags

  lcd.home ();                   // go home

  lcd.setCursor ( 0, 0 );

This second step appears redundant. Shouldn't you explain why it may be necessary.

Don

Thanks for your feedback.

You're right, now I see its redundant.

Fixed :slight_smile:

Andrew.

By the way, the LCD display code should probably be in the loop() if you want to add any useful output.

I missed this one. This statement is not correct since the information that you are displaying does not change.

You should only put LCD display code in loop() if the information that you are displaying changes.

For example to display "The temperature is xxx" you should display "The temperature is " in setup() and "xxx" in loop(). Then as the temperature changes you just update the "xxx" part.

Don

floresta:

By the way, the LCD display code should probably be in the loop() if you want to add any useful output.

I missed this one. This statement is not correct since the information that you are displaying does not change.

You should only put LCD display code in loop() if the information that you are displaying changes.

For example to display "The temperature is xxx" you should display "The temperature is " in setup() and "xxx" in loop(). Then as the temperature changes you just update the "xxx" part.

Don

Ahh, this makes good sense. Thanks again for the advice. Will update accordingly.

For arjscott.....in your comments you say don't forget to use the new liquidcrystal library...you're referring to the one sainsmart provides compatible with arduino right?

Aaron_dyer:
For arjscott.....in your comments you say don't forget to use the new liquidcrystal library...you're referring to the one sainsmart provides compatible with arduino right?

The new liquidcrystal library can be found here:

https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

One important thing to note from the installation instructions (at the above link) is this:

"The library has been developed to replace the current Arduino library, therefore you will need to remove/backup the LiquidCrystal folder from the Arduino library folder the original LiquidCrystal library and replace it for this one."

So, be sure to move the current LiquidCrystal folder somewhere, and replace it with the new library.

Best regards,

Andrew

Oh, I think the library on the sainsmart website is the same one that I have linked to.

But, personally I don't have much faith in any docs or code on their site.

Andrew!!!!!! You are the Man! I just hooked it up , downloaded library and uploaded and it worked like a champ! Thank you so much bro!

Aaron_dyer:
Andrew!!!!!! You are the Man! I just hooked it up , downloaded library and uploaded and it worked like a champ! Thank you so much bro!

Hey that's great!!! I'm really glad you got it working! I'm loving playing with this LCD too.

Cheers,

Andrew

You did use my New LiquidCrystal library right?

The whole thing is that these folks sell their stuff and rely on someone else to sort it out for them. The link in the LCD product page is not for my library (didn't check). But if it was, they could at least mention how they've mapped the I2c extender pins to the LCD to simplify the initialisation and use of what they sell!

It is a zipped image of my library the one they have on their site! http://www.sainsmart.com/home-page-view/sainsmart-iic-i2c-twi-serial-2004-20x4-lcd-module-shield-for-arduino-uno-mega-r3.html

They have even writen a sample sketch to go with it that is not correctly initialised as it uses 0x27 as its base address as opposed to 0x3F used by you LCD module. The sample sketch is called "sainlcdtest".

/*
** Example Arduino sketch for SainSmart I2C LCD2004 adapter for HD44780 LCD screens
** Readily found on eBay or http://www.sainsmart.com/ 
** The LCD2004 module appears to be identical to one marketed by YwRobot
**
** Address pins 0,1 & 2 are all permenantly tied high so the address is fixed at 0x27
**
** Written for and tested with Arduino 1.0
** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
** https://bitbucket.org/fmalpartida/new-liquidcrystal 
**
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)
**
** NOTE: TEsted on Arduino NANO whose I2C pins are A4==SDA, A5==SCL
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#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 (20,4);
  
// Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home

  lcd.print("SainSmart I2C tester");  
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("F Malpartida library");
  lcd.setCursor ( 0, 2 );        // go to the third line
  lcd.print("Test By Edward Comer");
  lcd.setCursor ( 0, 3 );        // go to the fourth line
  lcd.print("Iteration No: ");
}

void loop()
{
  // Backlight on/off every 3 seconds
  lcd.setCursor (14,3);        // go col 14 of line 3
  lcd.print(n++,DEC);
  lcd.setBacklight(LOW);      // Backlight off
  delay(3000);
  lcd.setBacklight(HIGH);     // Backlight on
  delay(3000);
}

How intersting!

FM,

Yes...your Library you were trying to Direct me to was the same Andrew was......and the one sainsmart has you download is the same as well.

Here is what i was doign wrong all along! So In the beginning I had my I2C scanner running so I knew it was address 0x3F i was communicating with right?

Well when I had first downloaded that new LiquidCrystal Library...I discarded the other liquidCrystal library that is in 1.0.1 but what I was doing wrong: I didn't discard the LiquidCrystal_I2C library I had downloaded before...it kept wanting to use that one......because the LiquidCrystal_i2c.h and LCD.h is already in the new LiquidCrystal library.....Now I just have to tinker with the code to make it say what I want in the loop etc...

But you guys have been so great and patient...I REALLY APPRECIATE it.....

Thanks to FM and Andrew......

I was going to mention...about the sainsmart stuff....forget their 0x27 garbage! you need to verify the I2C address for the device you're communicating with via this I2C scanner code:

// i2c_scanner
//
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
//
// This sketch tests the standard 7-bit addresses
// from 0 to 127. Devices with higher bit address
// might not be seen properly.
//
// Adapted to be as simple as possible by Arduino.cc user Krodal
//
// June 2012
// Using Arduino 1.0.1
//

#include <Wire.h>

void setup()
{
Wire.begin();

Serial.begin(9600);
Serial.println("\nI2C Scanner");
}

void loop()
{
byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 0; 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("Unknow 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(8000); // wait 8 seconds for next scan
}

Then open the serial monitor after uploading and check the address.....mine was 0x3F

@Aaron_dyer - thanks for your comments, much appreciated.
This whole thing of not publishing correct information about device addresses is a bit of a bother.

What I am pleased about is that they've linked the library in and respected the Open Source Creative Commons license! Cool.

FM,

Is that you in your picture for your profile? It looks like a girl I went to High school with

Aaron_dyer:
FM,

Is that you in your picture for your profile? It looks like a girl I went to High school with

Lets just say that the picture was taken 22 odd years ago.
Lets also say that fm = fmalpartida

Pretty!