Difference between revisions of "I²C LCD1602"

From Wiki
Jump to: navigation, search
Line 1: Line 1:
 
=='''Introduction'''==
 
=='''Introduction'''==
As we all know, though LCD and some other displays greatly enrich the man-machine interaction, they share a common weakness. When they are connected to a controller, multiple IOs will be occupied of the controller which has no so many outer ports. Also it restricts other functions of the controller. Therefore, LCD2004 with an I2C bus is developed to solve the problem.<br>
+
As we all know, though LCD and some other displays greatly enrich the man-machine interaction, they share a common weakness. When they are connected to a controller, multiple IOs will be occupied of the controller which has no so many outer ports. Also it restricts other functions of the controller. Therefore, LCD1602 with an I2C bus is developed to solve the problem.<br>  
I2C bus is a type of serial bus invented by PHLIPS. It is a high performance serial bus which has bus ruling and high or low speed device synchronization function required by multiple host system. I2C bus has only two bidirectional signal lines, Serial Data Line (SDA) and Serial Clock Line (SCL). The blue potentiometer on the I2C LCD2004 is used to adjust backlight to make it easier to display on the I2C LCD2004.
+
I2C bus is a type of serial bus invented by PHLIPS. It is a high performance serial bus which has bus ruling and high or low speed device synchronization function required by multiple host system. I2C bus has only two bidirectional signal lines, Serial Data Line (SDA) and Serial Clock Line (SCL). The blue potentiometer on the I2C LCD1602 is used to adjust backlight to make it easier to display on the I2C LCD1602. <br>
  
[[File:Lcd2004front(NEW).jpg|400px]] [[File:Lcd2004back(NEW).jpg|400px]] <br>
+
[[Image:I2c_lcd1602.png]]<br>
  
 
- '''GND''': Ground<br>
 
- '''GND''': Ground<br>
Line 28: Line 28:
 
- '''Shorting Cap''': Backlight can be enabled by this cap,unplugg this cap to disable the backlight.<br>
 
- '''Shorting Cap''': Backlight can be enabled by this cap,unplugg this cap to disable the backlight.<br>
 
- '''Potentiometer''': It is used to adjust the contrast (the clarity of the displayed text), which is increased in the clockwise direction and decreased in the counterclockwise direction.<br>
 
- '''Potentiometer''': It is used to adjust the contrast (the clarity of the displayed text), which is increased in the clockwise direction and decreased in the counterclockwise direction.<br>
 +
  
 
=='''For Arduino User'''==
 
=='''For Arduino User'''==
  
==='''<font color="orange">Components</font>'''===
+
==='''<font color="orange">Build the circuit</font>'''===
- 1 *SunFounder Mars board <br>
+
 
- 1 * I2C LCD2004 module<br>
+
See the following table for connection between the I2C LCD1602 and the SunFounder Uno board:<br>  
- 1 * USB cable<br>
+
 
- Several jump wires<br>
+
==='''<font color="orange">Connect the circuit</font>'''===
+
See the following table for connection between the I2C LCD2004 and the SunFounder Uno board:<br>  
+
<center>
+
 
{| border="1" class="wikitable"
 
{| border="1" class="wikitable"
 
|-
 
|-
! scope="col" | I2C LCD2004
+
! scope="col" | I2C LCD1602
! scope="col" | SunFounder Mars board
+
! scope="col" | SunFounder Uno board
  
 
|-
 
|-
Line 57: Line 54:
 
|align="center"|A5 /pin 21 mega2560
 
|align="center"|A5 /pin 21 mega2560
 
|}
 
|}
</center>
 
  
==='''<font color="orange">Add library</font>'''===
+
[[File:Experiment.png|1000px]]
 +
 
 +
==='''<font color="orange">Add Library</font>'''===
 +
 
 
Before you upload the code to the control board,you neeed to add the LiquidCrystal_I2C library.<br>
 
Before you upload the code to the control board,you neeed to add the LiquidCrystal_I2C library.<br>
 
1) Download the [http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip '''LiquidCrystal_I2C library''']<br>
 
1) Download the [http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip '''LiquidCrystal_I2C library''']<br>
Line 71: Line 70:
 
<br>
 
<br>
  
==='''<font color="orange">Copy the code</font>'''===
+
==='''<font color="orange">Copy and Upload the code</font>'''===
Copy the follwing code to the Arduino IDE
+
 
 +
Copy the follwing code to the Arduino IDE, click to the upload icon to upload the code to the control board<br>
 
<pre>
 
<pre>
/*****************************************
+
/********************************
* name: I2C LCD2004
+
  name:I2C LCD1602
* function: You should now see your I2C LCD2004 display "Hello,world!","IIC/I2C LCD2004"
+
  function:You should now see your I2C LCD1602 display the flowing characters: "SunFounder" and "hello, world".
* "20 cols, 4 rows","www.sunfounder.com"
+
 
  ********************************/
 
  ********************************/
//Email:service@sunfounder.com
+
//Email:support@sunfounder.com
 
//Website:www.sunfounder.com
 
//Website:www.sunfounder.com
  
 
/********************************/
 
/********************************/
 
// include the library code
 
// include the library code
#include <Wire.h>  
+
#include <Wire.h>
 
#include <LiquidCrystal_I2C.h>
 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
+
/**********************************************************/
 +
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
 
/*********************************************************/
 
/*********************************************************/
 
void setup()
 
void setup()
 
{
 
{
 
   lcd.init();  //initialize the lcd
 
   lcd.init();  //initialize the lcd
   lcd.backlight();  //open the backlight  
+
   lcd.backlight();  //open the backlight
 
+
 
+
  lcd.setCursor ( 0, 0 );            // go to the top left corner
+
  lcd.print("    Hello,world!    "); // write this string on the top row
+
  lcd.setCursor ( 0, 1 );            // go to the 2nd row
+
  lcd.print("  IIC/I2C LCD2004  "); // pad string with spaces for centering
+
  lcd.setCursor ( 0, 2 );            // go to the third row
+
  lcd.print("  20 cols, 4 rows  "); // pad with spaces for centering
+
  lcd.setCursor ( 0, 3 );            // go to the fourth row
+
  lcd.print(" www.sunfounder.com ");
+
 
}
 
}
 
/*********************************************************/
 
/*********************************************************/
void loop()  
+
void loop()
 
{
 
{
 
+
  lcd.setCursor(3, 0); // set the cursor to column 3, line 0
 +
  lcd.print("SunFounder");  // Print a message to the LCD
 +
 
 +
  lcd.setCursor(2, 1); // set the cursor to column 2, line 1
 +
  lcd.print("Hello, World!");  // Print a message to the LCD.
 
}
 
}
 
/************************************************************/
 
/************************************************************/
 
</pre>
 
</pre>
==='''<font color="orange">Upload the code</font>'''===
 
Before upload the code ,you need to select correct Board and Port,please follow the steps:<br>
 
<br>
 
1) Click Tools ->Board and select Arduino/Genuino Uno.<br>
 
<br>
 
[[File:Board.png]]<br>
 
<br>
 
2) Then select Tools ->Port.<br>
 
<br>
 
[[File:Port.png]]<br>
 
<br>
 
3) Click to the upload icon to upload the code to the control board<br>
 
<br>
 
[[File:UPLOAD.png]]
 
<br>
 
If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.<br>
 
  
==='''<font color="orange">Read I2C Address</font>'''===  
+
==='''<font color="orange">Experimental Phenomenon</font>'''===
 +
 
 +
 
 +
You should now see your I2C LCD1602 display the flowing characters "SunFounder" and "hello, world".<br>
 +
[[File:I2c_lcd102_1.png|1000px]]
 +
 
 +
 
 +
==='''<font color="orange">Read I2C Address</font>'''===
 +
 
 
If everything is correct, but the display just shows 16 black rectangles on Line 1.it may be the address of i2c is not 0x27,therfore you need to run the following code to read the address,then modify the 0x27 to which you read.
 
If everything is correct, but the display just shows 16 black rectangles on Line 1.it may be the address of i2c is not 0x27,therfore you need to run the following code to read the address,then modify the 0x27 to which you read.
 
<pre>
 
<pre>
Line 275: Line 261:
 
==='''<font color="orange">Download and run the code</font>'''===
 
==='''<font color="orange">Download and run the code</font>'''===
  
'''Step 1''': Download the [http://wiki.sunfounder.cc/images/7/7f/I2c_lcd2004_for_raspberry_pi.zip code] package.<br>
+
'''Step 1''': Download the [http://wiki.sunfounder.cc/images/7/7f/I2c_lcd1602_for_Raspberry_Pi.zip code] package.<br>
  
 
<pre>
 
<pre>
wget http://wiki.sunfounder.cc/images/7/7f/I2c_lcd2004_for_raspberry_pi.zip
+
wget http://wiki.sunfounder.cc/images/7/7f/I2c_lcd1602_for_Raspberry_Pi.zip
 
</pre>
 
</pre>
  
Line 284: Line 270:
  
 
<pre>
 
<pre>
unzip I2c_lcd2004_for_raspberry_pi.zip
+
unzip I2c_lcd1602_for_Raspberry_Pi.zip
 
</pre>
 
</pre>
  
Line 291: Line 277:
 
'''Step 3''': Get into the folder of code.<br>
 
'''Step 3''': Get into the folder of code.<br>
 
<pre>
 
<pre>
cd I2c_lcd2004_for_raspberry_pi/c
+
cd I2c_lcd1602_for_Raspberry_Pi/c
 
</pre>
 
</pre>
  
 
'''Step 4''': Compile.<br>
 
'''Step 4''': Compile.<br>
 
<pre>
 
<pre>
gcc lcd2004.c –o lcd2004 –lwiringPiDev –lwiringPi
+
gcc lcd1602.c –o lcd1602 –lwiringPiDev –lwiringPi
 
</pre>
 
</pre>
  
 
'''Step 5''': Run.<br>
 
'''Step 5''': Run.<br>
 
<pre>
 
<pre>
sudo ./lcd2004
+
sudo ./lcd1602
 
</pre>
 
</pre>
  
Line 307: Line 293:
 
'''Step 3''': Get into the folder of code.<br>
 
'''Step 3''': Get into the folder of code.<br>
 
<pre>
 
<pre>
cd I2c_lcd2004_for_raspberry_pi/python
+
cd I2c_lcd1602_for_Raspberry_Pi/python
 
</pre>
 
</pre>
  
 
'''Step 4''': Run.<br>   
 
'''Step 4''': Run.<br>   
 
<pre>
 
<pre>
sudo python lcd2004_show.py
+
sudo python lcd1602_show.py
 
</pre>
 
</pre>
  
==Change I2C Address==
 
When a conflict happens on the I2C address, you can change the I2C address by setting the A0, A1, A2 pins to 1 or 0 on the PCF8574 chip.<br>
 
There is a red module on the back of the I2C LCD, the main chip is PCF8574 or PCF8574T, pin A0, A1, A2 has been extended, as shown below.<br>
 
[[File:MG 0611-2.jpg]]<br>
 
For PCF8574T chip, I2C address format: 0 0 1 0 0 A2 A1 A0. The default address is: 0x27    <br>
 
For PCF8574 chip, I2C address format: 0 0 1 1 1 A2 A1 A0. The default address is: 0x3F    <br>
 
If you want to modify the default address, just need connect pinA0, A1, A2 to GND respectively. <br>
 
For PCF8574T chip, I2C address range: 0x20-0x27 <br>
 
For PCF8574 chip, I2C address range: 0x38-0x3F  <br>
 
If you are not sure, you can use the above code to scan the changed address.<br>
 
  
=='''Resources'''==
+
=='''Resources'''===
[http://wiki.sunfounder.cc/images/5/56/I2C_LCD2004.zip I2C_LCD2004 test code] [[File:ZIP.jpg]]<br>
+
 
[http://wiki.sunfounder.cc/images/1/18/PCF8574T_datasheet.pdf PCF8574T_datasheet] [[File:PDF.jpg]]<br>
+
[http://wiki.sunfounder.cc/images/1/18/PCF8574T_datasheet.pdf PCF8574T_datasheet][[File:PDF.jpg]]<br>
[http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip LiquidCrystal_I2C library] [[File:ZIP.jpg]]
+
[http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip LiquidCrystal_I2C library][[File:ZIP.jpg]]
 +
 
 +
[http://wiki.sunfounder.cc/images/7/7f/I2c_lcd1602_for_Raspberry_Pi.zip I2C LCD1602 for Raspberry Pi][[File:ZIP.jpg]]

Revision as of 10:09, 12 April 2023

Introduction

As we all know, though LCD and some other displays greatly enrich the man-machine interaction, they share a common weakness. When they are connected to a controller, multiple IOs will be occupied of the controller which has no so many outer ports. Also it restricts other functions of the controller. Therefore, LCD1602 with an I2C bus is developed to solve the problem.
I2C bus is a type of serial bus invented by PHLIPS. It is a high performance serial bus which has bus ruling and high or low speed device synchronization function required by multiple host system. I2C bus has only two bidirectional signal lines, Serial Data Line (SDA) and Serial Clock Line (SCL). The blue potentiometer on the I2C LCD1602 is used to adjust backlight to make it easier to display on the I2C LCD1602.

I2c lcd1602.png

- GND: Ground
- VCC: Voltage supply, 5V.
- SDA: Serial data line. Connect to VCC through a pullup resistor.
- SCL: Serial clock line. Connect to VCC through a pullup resistor.

I2C Address

The default address is basically 0x27, in a few cases it may be 0x3F.

Taking the default address of 0x27 as an example, the device address can be modified by shorting the A0/A1/A2 pads; in the default state, A0/A1/A2 is 1, and if the pad is shorted, A0/A1/A2 is 0.

I2c address.jpg


Backlight/Contrast

Backlight can be enabled by jumper cap, unplugg the jumper cap to disable the backlight. The blue potentiometer on the back is used to adjust the contrast (the ratio of brightness between the brightest white and the darkest black).

Back lcd1602.jpg


- Shorting Cap: Backlight can be enabled by this cap,unplugg this cap to disable the backlight.
- Potentiometer: It is used to adjust the contrast (the clarity of the displayed text), which is increased in the clockwise direction and decreased in the counterclockwise direction.


For Arduino User

Build the circuit

See the following table for connection between the I2C LCD1602 and the SunFounder Uno board:

I2C LCD1602 SunFounder Uno board
GND GND
VCC 5V
SDA A4 /pin 20 mega2560
SCL A5 /pin 21 mega2560

Experiment.png

Add Library

Before you upload the code to the control board,you neeed to add the LiquidCrystal_I2C library.
1) Download the LiquidCrystal_I2C library

2) Open the Arduino IDE,Select Sketch -> Include Library -> Add ZIP Library

Rfd.png

3) Find the file LiquidCrystal_I2C which you just download. Click it open and then you'll be prompted by "Library added to your libraries. Check 'Import libraries'”. You also can see the libraries just imported have appeared on the list by Sketch->Include Library->LiquidCrystal_I2C.
Fdfds.png

Copy and Upload the code

Copy the follwing code to the Arduino IDE, click to the upload icon to upload the code to the control board

/********************************
   name:I2C LCD1602
   function:You should now see your I2C LCD1602 display the flowing characters: "SunFounder" and "hello, world".
 ********************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com

/********************************/
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/**********************************************************/
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*********************************************************/
void setup()
{
  lcd.init();  //initialize the lcd
  lcd.backlight();  //open the backlight
}
/*********************************************************/
void loop()
{
  lcd.setCursor(3, 0); // set the cursor to column 3, line 0
  lcd.print("SunFounder");  // Print a message to the LCD
  
  lcd.setCursor(2, 1); // set the cursor to column 2, line 1
  lcd.print("Hello, World!");  // Print a message to the LCD.
}
/************************************************************/

Experimental Phenomenon

You should now see your I2C LCD1602 display the flowing characters "SunFounder" and "hello, world".
I2c lcd102 1.png


Read I2C Address

If everything is correct, but the display just shows 16 black rectangles on Line 1.it may be the address of i2c is not 0x27,therfore you need to run the following code to read the address,then modify the 0x27 to which you read.

LiquidCrystal_I2C lcd(0x27,16,2);
/*****************************************************
 * name:I2C_Address
 * function:read the address of the I2C lcd1602
 * Connection:
 * I2C                 UNO 
 * GND                 GND
 * VCC                  5V
 * SDA                  A4(pin20 in mega2560)
 * SCL                  A5(pin21 in mega2560)
 ********************************************************/

#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 = 1; 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(5000); // wait 5 seconds for next scan
}

For Raspberry Pi User

Build the circuit

1000px

Setup I2C

Enable the I2C port of your Raspberry Pi (If you have enabled it, skip this; if you do not know whether you have done that or not, please continue).

Step 1: Run the following command.

sudo raspi-config

Step 2: 3 Interfacing options.


Image282.png

Step 3: P5 I2C.

Image283.png

Step 4: <Yes>, then <Ok> -> <Finish>.

Image284.png

Step 5: Check whether the i2c modules are loaded and active.

lsmod | grep i2c

Step 6: Then the following codes will appear (the number may be different).

i2c_dev                     6276    0
i2c_bcm2708                 4121    0

Step 7: Install i2c-tools.

sudo apt-get install i2c-tools

Step 8: Check the address of the I2C device.

i2cdetect -y 1      # For Raspberry Pi 2 and higher version
i2cdetect -y 0      # For Raspberry Pi 1
    pi@raspberrypi ~ $ i2cdetect -y 1
        0  1  2  3   4  5  6  7  8  9   a  b  c  d  e  f
    00:           -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --

If there is an I2C device connected, the address of the device will be displayed.

Step 9: Install libi2c-dev or smbus.

For C language users

sudo apt-get install libi2c-dev 

For Python users

sudo pip3 install smbus2

Download and run the code

Step 1: Download the code package.

wget http://wiki.sunfounder.cc/images/7/7f/I2c_lcd1602_for_Raspberry_Pi.zip

Step 2: Extract the package

unzip I2c_lcd1602_for_Raspberry_Pi.zip

(For C Language Users)

Step 3: Get into the folder of code.

cd I2c_lcd1602_for_Raspberry_Pi/c

Step 4: Compile.

gcc lcd1602.c –o lcd1602 –lwiringPiDev –lwiringPi

Step 5: Run.

sudo ./lcd1602

(For Python Users)
Step 3: Get into the folder of code.

cd I2c_lcd1602_for_Raspberry_Pi/python

Step 4: Run.

sudo python lcd1602_show.py


Resources=

PCF8574T_datasheetPDF.jpg
LiquidCrystal_I2C libraryZIP.jpg

I2C LCD1602 for Raspberry PiZIP.jpg