Difference between revisions of "I²C LCD1602"

From Wiki
Jump to: navigation, search
(Experimental Procedures)
(Resources)
(46 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
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>  
 
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 LCD1602 is used to adjust backlight to make it easier to display on the I2C LCD1602. <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 LCD1602 is used to adjust backlight to make it easier to display on the I2C LCD1602. <br>
[[File:back.png|200px|center]][[File:Forward.png|200px|center]]
+
[[Image:Back.png|800px]]
  
 
=='''Components'''==
 
=='''Components'''==
- 1 *[http://www.sunfounder.com/index.php?c=showcs&id=50&model=UNO%20R3&pname=Arduino&name=&pid=21 SunFounder Uno board<br>
+
- 1 *SunFounder Uno board <br>
- 1 * [http://www.amazon.com/SunFounder-interface-Plate-KeyPad-Raspberry/dp/B00MRWB5LW/ref=sr_1_1?m=ADHH624DX2Q66&s=merchant-items&ie=UTF8&qid=1446870985&sr=1-1&keywords=i2c+lcd1602&pebp=1446870940122&perid=125KN0M4SXF3RDWYX2V0 I2C LCD1602] module<br>
+
- 1 * I2C LCD1602 module<br>
 
- 1 * USB cable<br>
 
- 1 * USB cable<br>
 
- Several jump wires<br>
 
- Several jump wires<br>
 
=='''Experimental Principle'''==
 
=='''Experimental Principle'''==
In this experiment, we will let [http://www.amazon.com/SunFounder-interface-Plate-KeyPad-Raspberry/dp/B00MRWB5LW/ref=sr_1_1?m=ADHH624DX2Q66&s=merchant-items&ie=UTF8&qid=1446870985&sr=1-1&keywords=i2c+lcd1602&pebp=1446870940122&perid=125KN0M4SXF3RDWYX2V0 I2C LCD1602] display “SUNFOUNDER" and “hello, world" by programming.<br>
+
In this experiment, we will let I2C LCD1602 display “SUNFOUNDER" and “hello, world" by programming.<br>
 
I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).<br>
 
I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).<br>
I²C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typical voltages used are +5 V or +3.3 V although systems I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).<br>
+
I²C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL),pulled up with resistors. Typical voltages used are +5 V or +3.3 V although systems with other voltages are permitted. <br>
I²C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typical voltages used are +5 V or +3.3 V although systems with other voltages are permitted.<br>
+
 
 
For more information about I2C operation principle, please visit [https://en.wikipedia.org/wiki/I²C I²C].<br>
 
For more information about I2C operation principle, please visit [https://en.wikipedia.org/wiki/I²C I²C].<br>
  
 
=='''Experimental Procedures'''==
 
=='''Experimental Procedures'''==
Step 1: Connect the circuit<br>
+
===Step 1: Connect the circuit===
 
See the following table for connection between the I2C LCD1602 and the SunFounder Uno board:<br>  
 
See the following table for connection between the I2C LCD1602 and the SunFounder Uno board:<br>  
 
<center>
 
<center>
 
{| border="1" class="wikitable"
 
{| border="1" class="wikitable"
 
|-
 
|-
! scope="col" | [http://www.amazon.com/SunFounder-interface-Plate-KeyPad-Raspberry/dp/B00MRWB5LW/ref=sr_1_1?m=ADHH624DX2Q66&s=merchant-items&ie=UTF8&qid=1446870985&sr=1-1&keywords=i2c+lcd1602&pebp=1446870940122&perid=125KN0M4SXF3RDWYX2V0 I2C LCD1602]
+
! scope="col" | I2C LCD1602
! scope="col" | [http://www.sunfounder.com/index.php?c=showcs&id=50&model=UNO%20R3&pname=Arduino&name=&pid=21 SunFounder Uno board]
+
! scope="col" | SunFounder Uno board
  
 
|-
 
|-
Line 33: Line 33:
 
|-
 
|-
 
|align="center"|SDA  
 
|align="center"|SDA  
|align="center"|A4  
+
|align="center"|A4 /pin 20 mega2560
 
|-
 
|-
 
|align="center"|SCL
 
|align="center"|SCL
|align="center"|A5
+
|align="center"|A5 /pin 21 mega2560
 
|}
 
|}
 
</center>
 
</center>
[[File:Experiment.png]]
+
[[File:Experiment.png|800px|center]]
<font color="red">Note: The wiring of I2C LCD1602 is the same through the following lessons.</font>
+
 
+
===Step 2:Add library===
Step 2: Program (Please refer to the example code in LEARN -> [http://www.sunfounder.com/index.php?c=case_incs&a=typelist Get Tutorial] on our website)<br>
+
Before you upload the code to the control board,you neeed to add the LiquidCrystal_I2C library.<br>
Step 3: Compile the code<br>
+
1) Download the [http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip '''LiquidCrystal_I2C library''']<br>
Before compiling the code, you need to add the LiquidCrystal_I2C file to the libraries of Arduino. If you don't know the libraries path, you can open Arduino IDE ,click File ->Preferences, as shown in the follwing diagram. Then add LiquidCrystal_I2C to the folder Libraries under the path<br>
+
<br>
   
+
2) Open the Arduino IDE,Select Sketch -> Include Library -> Add ZIP Library<br>
Step 4: Upload the sketch to the SunFounder Uno board
+
<br>
You should now see your I2C LCD1602 display the flowing characters "SunFounder" and "hello, world".<br>
+
[[File:Rfd.png]]<br>
 +
<br>
 +
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.<br>
 +
[[File:Fdfds.png]] <br>
 +
<br>
 +
 
 +
===Step 3: Copy the code ===
 +
Copy the follwing code to the Arduino IDE ,click to the upload icon to upload the code to the control board<br>
 +
<pre>
 +
/********************************
 +
  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.
 +
}
 +
/************************************************************/
 +
</pre>
 +
 
 +
=='''experimental phenomenon'''==
 +
You should now see your I2C LCD1602 display the flowing characters "SunFounder" and "hello, world".<br>
 
[[File:I2c_lcd102_1.png|800px|center]]
 
[[File:I2c_lcd102_1.png|800px|center]]
  
[http://192.168.0.121/mediawiki/index.php/File:I2C_LCD1602_Test_Experiment.pdf I2C_LCD1602_Test_Experiment.pdf ]<br>
+
=='''Note'''==
[http://192.168.0.121/mediawiki/index.php/File:I2C_LCD1602_Test_Experiment.zip I2C LCD1602 Test Experiment.zip]
+
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>
 +
LiquidCrystal_I2C lcd(0x27,16,2);
 +
</pre>
 +
<pre>
 +
/*****************************************************
 +
* 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
 +
}
 +
</pre>
 +
=='''Resources'''==
 +
[http://wiki.sunfounder.cc/images/1/18/PCF8574T_datasheet.pdf PCF8574T_datasheet][[File:PDF.jpg]]<br>
 +
[https://www.sunfounder.com/learn/sensor-kit-v2-0-for-raspberry-pi-b-plus/lesson-30-i2c-lcd1602-sensor-kit-v2-0-for-b-plus.html Test_Experiment_for_Raspberry_Pi][[File:LINK.jpg]]<br>
 +
[http://wiki.sunfounder.cc/images/7/7e/LiquidCrystal_I2C.zip LiquidCrystal_I2C library][[File:ZIP.jpg]]

Revision as of 07:24, 20 March 2017

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.
Back.png

Components

- 1 *SunFounder Uno board
- 1 * I2C LCD1602 module
- 1 * USB cable
- Several jump wires

Experimental Principle

In this experiment, we will let I2C LCD1602 display “SUNFOUNDER" and “hello, world" by programming.
I²C (Inter-Integrated Circuit), pronounced I-squared-C, is a multi-master, multi-slave, single-ended, serial computer bus invented by Philips Semiconductor (now NXP Semiconductors). It is typically used for attaching lower-speed peripheral ICs to processors and microcontrollers. Alternatively I²C is spelled I2C (pronounced I-two-C) or IIC (pronounced I-I-C).
I²C uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL),pulled up with resistors. Typical voltages used are +5 V or +3.3 V although systems with other voltages are permitted.

For more information about I2C operation principle, please visit I²C.

Experimental Procedures

Step 1: Connect 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

Step 2: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

Step 3: Copy 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

Note

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
}

Resources

PCF8574T_datasheetPDF.jpg
Test_Experiment_for_Raspberry_PiLINK.jpg
LiquidCrystal_I2C libraryZIP.jpg