I2C driver for LCD 20×4 and 16×2

Step 0: config hardware, SDA = A4 and SCL = A5

Step 1: go to https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library download .zip file
unzip it and copy the folder inside (inside the folder will have a folder called examples, keywords.txt, file LiquidCrystal_I2C.cpp and file LiquidCrystal_I2C.h) to the Arduino library folder, for example, E:\arduino-1.6.10\libraries

rename the folder as exactly LiquidCrystal_I2C

Step 2: LCD 20x4


/*
 * Date: Sun, 30/04/2017
 * Desc: Testing LCD with I2C driver PCF8754
 * Address = 0x3F, SDA = A4, SCL = A5
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F 
#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 = 0;

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, cursor will be at (col,row)=(0,0)

lcd.print("Testing_LCD_20x4_I2C"); 
 lcd.setCursor(0,1); // go to the 2nd line
 lcd.print("libLiquidCrystal_I2C");
 lcd.setCursor(0,2); // go to the third line
 lcd.print("Test By drjquang_eng");
 lcd.setCursor(0,3); // go to the fourth line
 lcd.print("Counting to 100: ");
}

void loop()
{
 n++;
 if(n==100){
 n=0;
 lcd.setCursor(19,3); // go col 19 of line 3 to clear the last digit)
 lcd.print(" ");
 }
 // Backlight on/off every 3 seconds
 lcd.setCursor(18,3); // go col 18 of line 3
 lcd.print(n,DEC);
 lcd.setBacklight(LOW); // Backlight off
 delay(3000);
 lcd.setBacklight(HIGH); // Backlight on
 delay(3000); 
}

Step 3: unplug the LCD I2C driver so that you are be able to upload program to arduino. If you do not unplug, uploading will take forever (I think the LCD consumps too much power so that there is not enough power to erase EEPROM)

Then plug driver, hit RESET.

However, if you use LCD 16x2, you do not need to unplug LCD when uploading code

LCD 16x2


/*
 * Date: Sun, 02/05/2017
 * Desc: Testing LCD with I2C driver PCF8754
 * Address = 0x3F, SDA = A4, SCL = A5
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F 
#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 = 0;

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);
 
// Switch on the backlight
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
 lcd.setBacklight(HIGH);
 lcd.home(); // go home, cursor will be at (col,row)=(0,0)

lcd.print("Testing_LCD_16x2"); 
 lcd.setCursor(0,1); // go to the 2nd line
 lcd.print("Counting 100: ");
}

void loop()
{
 n++;
 if(n==100){
 n=0;
 lcd.setCursor(15,1); // go col 15 of line 1 to clear the last digit
 lcd.print(" ");
 }
 // Backlight on/off every 3 seconds
 lcd.setCursor(14,1); // go col 14 of line 1
 lcd.print(n,DEC);
 lcd.setBacklight(LOW); // Backlight off
 delay(500);
 lcd.setBacklight(HIGH); // Backlight on
 delay(500); 
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s