Authorized Online Retailers:

AMAZON
AMAZON-jp
amzuk

Overview

In this lesson, we’ll introduce how to drive IIC (sometimes call I2C) 1602 LCD using Raspberry Pi.

Hardware Preparation

1 * Raspberry Pi
1 * Breadboard
1 * IIC 1602 LCD (16 x 2 characters)
Several jumper wires

Software Preparation

Note: In this lesson, we remotely control raspberry pi via PuTTy on PC. To learn how to config raspberry pi, please visit lesson 1: getting started with raspberry pi.

Work Principle

This This 1602 LCD display screen can display 16(each row) x 2 (rows) characters. Generally, LCD 1602 has parallel port, it occupy many GPIO pins. This 1602 LCD comes with a I2C communicate interface using a PCF8574 IC Chip. It means you can realize data display via only 2 wires.

Wiring up IIC 1602 LCD to Pi as following connection graph.

Note:  SDA 1 is BCM 2 , SCL 1 is BCM 3 , for detail about GPIO#, read Lesson 2 , these 2 pins normally are used to exchange serial data with external devices.


Hardware Setup
:

Sample code

To use the IIC 1602 LCD,we should enable the I2C port firstly, please follow the steps as followed:

1) Open IIC

sudo  nano  /boot/config.txt

open the file /boot/config.txt, find the code line”dtparam=i2c_arm”,checking if there is # sign in front of the line, uncomment it (remove the # in front of this line), and make sure the end of the line is”on”, finally the code should look like this:

Press Ctrl+X,and type “Y” to save the file you revised.

2) Load Modules

sudo nano /etc/modules

open /etc/modules file,Add these two lines as below:

i2c-bcm2708
i2c-dev

Press Ctrl+X, and type “Y” to save the file you revised.

3) Install i2c python library and smbus

sudo apt-get install -y python-smbus i2c-tools

Now reboot Pi

reboot

4) Cheking the  if library is installed successfully by typing following command:

lsmod | grep i2c

If the terminal will show the message as below if installed successfully,otherwise please repeat above steps.

5) Checking for any components connected to the i2c bus by using i2c detect

sudo i2cdetect -y 1
or port 0 on the older Raspberry Pi
sudo i2cdetect -y 0

Once run the command, A table like below will be shown and if any devices are connected, the address will be shown. Below you can see that a device is connected to the i2c bus which is using the address of 0x3f.

Note: Above code line 5 is to define LCD I2C address. If your LCD does not display properly, you might need change the value from 0x3f to 0x27


For  C language user,please take the steps as followed:

1)  Download the sample code by typing following terminal command:

cd  ~

sudo  wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_13/i2c1602_lcd.c

If you want to customize the sample code file , you can use nano editor to edit source code by typing following command in terminal:

sudo nano  i2c1602_lcd.c

2) copmlie code

gcc   -Wall   -o  i2c1602_lcd   i2c1602_lcd.c    -lwiringPi

3) Run the program

sudo  ./i2c1602_lcd

4) Running result

Once run the program, the terminal will message as code, while the LCD screen will display the message at the same time.

Note:You can adjust the contrast of the screen by spinning the potentiometer  screw in the back of the LCD clockwise or anticlockwise, until the screen displays characters clearly.

C source code and comments:

i2c1602_lcd.c
 
  1. #include < stdio.h>
  2. #include < wiringPi.h>
  3. #include < wiringPiI2C.h>
  4. #include < string.h>
  5. int LCDAddr = 0x3f;//I2c address of LCD, some LCD i2c address might be 0x27
  6. int BLEN = 0;//1--open backlight.0--close backlight
  7. int fd;//linux file descriptor
  8. //send an 16 bits data to LCD buffer
  9. void write_word(int data){
  10. int temp = data;
  11. if ( BLEN == 1 )
  12. temp |= 0x08;
  13. else
  14. temp &= 0xF7;
  15. wiringPiI2CWrite(fd, temp);
  16. }
  17. //send control command to lcd
  18. void send_command(int comm){
  19. int buf;
  20. // Send bit7-4 firstly
  21. buf = comm & 0xF0;
  22. buf |= 0x04; // RS = 0, RW = 0, EN = 1
  23. write_word(buf);
  24. delay(2);
  25. buf &= 0xFB; // Make EN = 0
  26. write_word(buf);
  27. // Send bit3-0 secondly
  28. buf = (comm & 0x0F)
  29. buf |= 0x04; // RS = 0, RW = 0, EN = 1
  30. write_word(buf);
  31. delay(2);
  32. buf &= 0xFB; // Make EN = 0
  33. write_word(buf);
  34. }
  35. //send character to lcd
  36. void send_data(int data){
  37. int buf;
  38. // Send bit7-4 firstly
  39. buf = data & 0xF0;
  40. buf |= 0x05; // RS = 1, RW = 0, EN = 1
  41. write_word(buf);
  42. delay(2);
  43. buf &= 0xFB; // Make EN = 0
  44. write_word(buf);
  45. // Send bit3-0 secondly
  46. buf = (data & 0x0F)
  47. buf |= 0x05; // RS = 1, RW = 0, EN = 1
  48. write_word(buf);
  49. delay(2);
  50. buf &= 0xFB; // Make EN = 0
  51. write_word(buf);
  52. }
  53. //initialize the lcd
  54. void init(){
  55. send_command(0x33); // Must initialize to 8-line mode at first
  56. delay(5);
  57. send_command(0x32); // Then initialize to 4-line mode
  58. delay(5);
  59. send_command(0x28); // 2 Lines & 5*7 dots
  60. delay(5);
  61. send_command(0x0C); // Enable display without cursor
  62. delay(5);
  63. send_command(0x01); // Clear Screen
  64. wiringPiI2CWrite(fd, 0x08);
  65. }
  66. //clear screen
  67. void clear(){
  68. send_command(0x01); //clear Screen
  69. }
  70. //Print the message on the lcd
  71. void write(int x, int y, char data[]){
  72. int addr, i;
  73. int tmp;
  74. if (x < 0) x = 0; if (x > 15) x = 15;
  75. if (y < 0) y = 0; if (y > 1) y = 1;
  76. // Move cursor
  77. addr = 0x80 + 0x40 * y + x;
  78. send_command(addr);
  79. tmp = strlen(data);
  80. for (i = 0; i < tmp; i++){
  81. send_data(data[i]);
  82. }
  83. }
  84. void print_info()
  85. {
  86. printf("\n");
  87. printf("|***************************|\n");
  88. printf("| IIC 1602 LCD test |\n");
  89. printf("| --------------------------|\n");
  90. printf("| | LCD | | Pi |\n");
  91. printf("| --------------------------|\n");
  92. printf("| | GND | connect to | GND |\n");
  93. printf("| | VCC | connect to | 5V |\n");
  94. printf("| | SDA | connect to | SDA.1|\n");
  95. printf("| | SCL | connect to | SCL.1|\n");
  96. printf("| --------------------------|\n");
  97. printf("| OSOYOO|\n");
  98. printf("|***************************|\n");
  99. printf("Program is running...\n");
  100. printf("Press Ctrl+C to end the program\n");
  101. }
  102. int main(){
  103. //init I2C, assign a buffer handler to variable fd
  104. fd = wiringPiI2CSetup(LCDAddr);
  105. init();
  106. print_info();
  107. write(0, 0, "Hi man.Welcome ");
  108. write(0, 1, "to osoyoo.com");
  109. delay(3000);
  110. clear();
  111. while(1){
  112. write(0,0,"This is Lesson13");
  113. write(0,1,"IIC LCD Test");
  114. delay(1000);
  115. }
  116. return 0;
  117. }

For Python language user


1) Download the Python code from osoyoo by typing following commands:

cd  ~

sudo  wget http://osoyoo.com/driver/pi3_start_learning_kit_lesson_13/i2c1602_lcd.py

Open I2C1602_lcd.py file by typing following command in terminal:

sudo nano i2c1602_lcd.py

find the code line as blow and change the address to your correct I2C address, in this example,the I2C address is 0x3F

2) Run the program

sudo  python  ./i2c1602_lcd.py

3) Running result

Once run the program, the terminal will message as code, while the LCD screen will display the message at the same time.

Note:You can adjust the contrast of the screen by spinning the potentiometer clockwise or anticlockwise, until the screen displays characters clearly.

Python sample code Analysis:

 
 
  1. import smbus
  2. import time
  3. # Define some device parameters
  4. I2C_ADDR = 0x3F # I2C device address, if any error, change this address to 0x27
  5. LCD_WIDTH = 16 # Maximum characters per line
  6. # Define some device constants
  7. LCD_CHR = 1 # Mode - Sending data
  8. LCD_CMD = 0 # Mode - Sending command
  9. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  10. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  11. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  12. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  13. LCD_BACKLIGHT = 0x08 # On
  14. #LCD_BACKLIGHT = 0x00 # Off
  15. ENABLE = 0b00000100 # Enable bit
  16. # Timing constants
  17. E_PULSE = 0.0005
  18. E_DELAY = 0.0005
  19. #Open I2C interface
  20. #bus = smbus.SMBus(0) # Rev 1 Pi uses 0
  21. bus = smbus.SMBus(1) # Rev 2 Pi uses 1
  22. def lcd_init():
  23. # Initialise display
  24. lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  25. lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  26. lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  27. lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  28. lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  29. lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  30. time.sleep(E_DELAY)
  31. def lcd_byte(bits, mode):
  32. # Send byte to data pins
  33. # bits = the data
  34. # mode = 1 for data
  35. # 0 for command
  36. bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  37. bits_low = mode | ((bits
  38. # High bits
  39. bus.write_byte(I2C_ADDR, bits_high)
  40. lcd_toggle_enable(bits_high)
  41. # Low bits
  42. bus.write_byte(I2C_ADDR, bits_low)
  43. lcd_toggle_enable(bits_low)
  44. def lcd_toggle_enable(bits):
  45. # Toggle enable
  46. time.sleep(E_DELAY)
  47. bus.write_byte(I2C_ADDR, (bits | ENABLE))
  48. time.sleep(E_PULSE)
  49. bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  50. time.sleep(E_DELAY)
  51. def lcd_string(message,line):
  52. # Send string to display
  53. message = message.ljust(LCD_WIDTH," ")
  54. lcd_byte(line, LCD_CMD)
  55. for i in range(LCD_WIDTH):
  56. lcd_byte(ord(message[i]),LCD_CHR)
  57. def main():
  58. # Main program block
  59. # Initialise display
  60. lcd_init()
  61. while True:
  62. # Send some test
  63. lcd_string("Created by
  64. lcd_string("Osoyoo.com Tutorial Url:",LCD_LINE_1)
  65. lcd_string("> http://osoyoo.com",LCD_LINE_2)
  66. time.sleep(3)
  67. if __name__ == '__main__':
  68. try:
  69. main()
  70. except KeyboardInterrupt:
  71. pass
  72. finally:
  73. lcd_byte(0x01, LCD_CMD)

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx
"