Pages

2016/02/27

Arduino I2C LCD 2004A

This is a summary of my findings working with I2C LCD 2004.

Wiring





Sample 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 I2C_ADDR    0x27 // <<----- 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
 lcd.begin (20,4); //  <<----- My LCD was 16x2


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

 lcd.print("SainSmart I2C 20x4");
}

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);
}

Result




Important things to know (abstract from Arduino Forum)

********************************************************************************
The vast majority of the I2C LCDs are basically the same because they use the same 8-bit I2C I/O expander chip - PCF8574. However, as there are other 8-bit I2C expander such as MCP23008 that may be used and require slightly different to drive them. Therefore, when working with I2C LCD, it important to know what I2C I/O expander is used.

In addition to the need to find out what I2C I/O expander is used, it's equally important to find out how the I/O expander is wired up to the LCD module's HD44780 interface. This is important because the HD44780 pinout and interface are standardized but how the I2C I/O expander is wired up is not.

Because of the above issues, the LiquidCrystal_I2C library used must match the type of I/O expander used and how it's wired up to the HD44780 interface. If the LiquidCrystal_I2C library used is made for different I/O expander and/or different wiring then it's not possible for it to drive the I2C LCD correctly.

To make matter worst, there are different versions of  LiquidCrystal_I2C library made by vendors for their particular I2C LCDs. While the libraries all have the same name, they are actually made for I2C LCDs that may have different I/O expander and/or different wiring. Unless you check the code of the library, it's not possible to tell which I2C LCD is supported by the library. To add to this confusion is that there are vendors that provide libraries that don't match the I2C LCDs they offer.

The solution to this is to use a library such as the one from FM that allows for the configuration of the wiring in the Arduino sketch.

*******************************************************************************

The I2C I/O expander on the add-on module of my I2C LCD.


The chip is PCF8574.

It's important to keep the original LiquidCrystal library after the NewliquidCrystal library is added as there are applications (for example, Marlin for 3D printing) that use the original LiquidCrystal library. The NewliquidCrystal library can co-exist with the original LiquidCrystal library. To do this, simply copy the NewliquidCrystal library to the libraries folder.


In Arduino IDE, both the original and the new LiquidCrystal library will appear as available libraries.


Reference:
Instruction for downloading and installing the new LCD libraries (I2C & Non I2C) - must read!!
https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

IIC/I2C/TWI 2004 20X4 Character LCD Module Display
http://forum.arduino.cc/index.php?topic=162029.0

I2C LCD - Setup instructions for 16x2
http://forum.arduino.cc/index.php?topic=128635.0

No comments:

Post a Comment