Notes About Working with Various Arduino & Netduino Microcontroller Boards

Friday, October 26, 2012

SainSmart IIC/I2C/TWI Serial 2004 20x4 LCD Module Shield

SainSmart IIC/I2C/TWI Serial 2004 20x4 LCD Module Shield is much easier to use than simpler LCD displays. Here's an I2C example.  It requires only 4 wires: 5 v, GND, SCL, and SDA.  For the Arduino Uno, connect SDA to A4 and SCL to A5.  (On the Leonardo: SDA = D2, SCL = D3 ).

The LiquidCrystal_I2C library for the 20 x 4 display is available from www.yourduino.com. For documentation, see this page.

Here's a very simple example:


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

//Addr: 0x3F, 20 chars & 4 lines
LiquidCrystal_I2C lcd(0x3F,20,4); 

void setup()
{
    lcd.init(); 
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Hello");
}
void loop()
{

}

I had found that the I2C address is 0x3F, not 0x27 as shown in some of the online examples. As noted in the comments below, though, it seems that some units have the 0x27 address indicated in the documentation. You may need to try both addresses.   

Note (12/14/2013):  I have had occasion to use this display with the Arduino Due and the 3.3V Sparkfun Arduino Pro Mini 328.  It works with both (with VCC on the display connected to 3.3V).  In both cases I've used Arduino IDE ver. 1.5.4.

35 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I'm getting a blank screen when testing this on an Uno.
    Any suggestions appreciated.

    ReplyDelete
  3. All is working now.

    Used: (0x27,20,4); //(0x3F,20,4) Did not work.

    ReplyDelete
    Replies
    1. Hi Beth -

      Thanks for posting. It looks like there may be some variation in the address. Perhaps mine is the exception. I'll update my notes above to reflect this.

      Delete
  4. Super blog, I tried all the codes I found here and there, and neither the code provided by the eBay seller worked. So I was lost for a while until I found this blog. Thanks to the author finally now I have a working LCD with me.
    Thanks a lot Brad.

    ReplyDelete
  5. \Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:81: error: conflicting return type specified for 'virtual void LiquidCrystal_I2C::write(uint8_t)'
    C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'

    Still getting errors, anyone know what this means?

    ReplyDelete
    Replies
    1. Hi Jared -

      Sorry, I somehow managed to miss you question. Which version of the Arduino IDE are you using and which Arduino?

      Delete
  6. I am using 1.0.5 and the Arduino Mega.

    ReplyDelete
  7. Brad I've made some progress, I get it to compile now. I apparently had 2 libraries installed so deleting 1 helped. (I still cannot get it to work.) I'm getting help on Arduino forum but if that doesn't help I will try harassing you again. By the way thanks for your blog some solutions I've seen seem to resolve things by killing a Ant with a shotgun (seems a little overkill) so your solution is one that's understandable.

    ReplyDelete
  8. Something I find very helpful for I2C... A Arduino I2c Scanner sketch. It will finsd all addresses on the I2c bus.
    http://playground.arduino.cc/Main/I2cScanner

    ReplyDelete
    Replies
    1. Thanks for the link. That does look very helpful.

      Delete
  9. Hi Brad,

    Awesome Blog, this post was really helpful. I had been getting the "LCDTest:25: error: no matching function for call to 'LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int)'" error with all the test files I tried, this was the first one to work for me. Thank you!

    ReplyDelete
    Replies
    1. Hi Smitty -

      Glad to hear that this post helped you.

      --Brad

      Delete
  10. Thanks for sharing, works for me too.

    ReplyDelete
  11. I have a question on how to change the pins or even if you can change them? Could you change them from analog to digital? I am using a Arduino Uno.

    ReplyDelete
    Replies
    1. Hi Cemal - Since the connection is via I2C, you could also use the SCL & SDA pins on the Uno Rev. 3, but that is not really a separate connection, just a different physical location on the board. You can't change the pins used for I2C on the board because they have to be connected to particular pins on the processor that handle the I2C protocol.

      Delete
    2. Thanks so much for helping solve the problem. It works

      Delete
  12. Thought I would add a comment that might save someone else a lot of head banging. If you have followed the above instructions and are still staring at a blank screen try turning the blue potentiometer on the back until the contrast is acceptable. If I would have gone to the link you provided above for the documentation I would have figured this out a lot earlier.

    ReplyDelete
  13. I am getting a blank screen any suggestions? I have a mega

    ReplyDelete
  14. I am getting a blank screen any suggestions? I have a mega

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Same thing. Back light works but I cant get any text to the screen.
    My LCD is 20x4 / 0x3F ( link to screen / seller http://www.ebay.com/itm/201013825839 )
    Using Mega 2560 and complier 1.6.5
    Any ideas ?

    ReplyDelete
  17. I managed to get it work.
    Open complier, copy/paste code from the top of the page (make sure your address is right, my LCD has 0x3F) and upload code to your board.
    Now LCD light should turn on. Adjust the screw behind the LCD and "Hello" should come on to your screen.

    In "libraries" folder make sure that needed library depth is only one folder. If you have many folders before actual library files, complier does not find them.

    ReplyDelete
  18. Hey there. I managed to get an "H" to display on the LCD when I changed to 0x27 (0x3F didn't work for me). How can I get it to read "Hello"?

    ReplyDelete
    Replies
    1. At the beginning of the file "LiquidCrystal_I2C.cpp" is a function named:

      inline size_t LiquidCrystal_I2C::write(uint8_t value
      )
      Change the statement "return 0" to "return 1". (this is line 20 in my file)

      Delete
    2. I had the same problem, your solution worked... thanks.

      Delete
    3. Thanks! this helped with my "Sainsmart LCD2004"

      Delete
    4. Still valid solution for Sainsmart LCD2004 with I2C backpack.

      Delete
    5. Thanks- this got me out of the woods for the same product I purchased on Amazon and am using with a Due. Having a bit of trouble getting the contrast to be usable, but that seems more hardware than software.

      Delete
  19. Hi I'm new to Arduino, I have a similar situation like Casey, my LCD only display "H" the first letter of a word "Hello" but mine is 0X3F. Is there a way I can get it to spell out the whole word?

    ReplyDelete
    Replies
    1. The sketch works properly for me. I'm using the library found in the file

      LiquidCrystal_I2C2004V1.zip

      Assuming that you are using the same library, did you change the code as mentioned in the reply to Casey's post? You'll need to do that if it hasn't been done already.

      Delete
    2. Thank you very much, Doug. Now mine works :)

      Delete
  20. Thanks so much, everything worked.

    2 things - my device had an I2C address of 0x27. And I had the same problem as Casey Rogers, only getting an H instead of Hello. The fix listed in the reply made to her comment fixed my problem.

    ReplyDelete
  21. Hello,

    i have 20x4 LCD display with 0x3F adress. When I connect display to power it only shows filled boxes in two rows (first and third row).

    I am using LiquidCrystal_I2C library and I connected LCD to my arduino nano via I2C and tested code from above but nothing happens I get those squares all the time.

    ReplyDelete
    Replies
    1. Problem solved. I have used library for which you provided link in the post above :) Thanks

      Delete