Lesson 1.1.7 I2C LCD1602

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

LCD1602 is a character type liquid crystal display, which can display 32 (16*2) characters at the same time.

Components

Principle

I2C LCD1602

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 communication

I2C(Inter-Integrated Circuit) bus is a very popular and powerful bus for communication between a master device (or master devices) and a single or multiple slave devices.

I2C main controller can be used to control IO expander, various sensors, EEPROM, ADC/DAC and so on. All of these are controlled only by the two pins of host, the serial data (SDA1) line and the serial clock line(SCL1).

Schematic Diagram

T-Board Namephysical
SDA1Pin 3
SCL1Pin 5

Experimental Procedures

Step 1: Build the circuit. 

Step 2: Setup I2C (see Appendix. If you have set I2C, skip this step.)

  • For C Language Users

Step 3: Change directory.

    cd /home/pi/davinci-kit-for-raspberry-pi/c/1.1.7/

Step 4: Compile.

    gcc 1.1.7_Lcd1602.c -lwiringPi

Step 5: Run.

    sudo ./a.out

After the code runs, you can see ”Greetings”,”From SunFounder” displaying on the LCD.

Code

Note: None of the following functions with ellipses is complete. You can view the complete code by using the command, nano 1.1.7 _lcd1602.c in the Bash interface.

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <string.h>

int LCDAddr = 0x27;
int BLEN = 1;
int fd;

void write_word(int data){……}
void send_command(int comm){……}
void send_data(int data){……}
void init(){……}
void clear(){……}
void write(int x, int y, char data[]){……}

void main(){
    fd = wiringPiI2CSetup(LCDAddr);
    init();
    write(0, 0, "Greetings!");
    write(1, 1, "From SunFounder");
}

Code Explanation

void write_word(int data){……}
void send_command(int comm){……}
void send_data(int data){……}
void init(){……}
void clear(){……}
void write(int x, int y, char data[]){……}

These functions are used to control I2C LCD1602 open source code. They allow us to easily use I2C LCD1602.

Among these functions, init() is used for initialization, clear() is used to clear the screen, write() is used to write what is displayed, and other functions support the above functions.

    fd = wiringPiI2CSetup(LCDAddr);

This function initializes the I2C system with the specified device symbol. The prototype of the function:

 int wiringPiI2CSetup(int devId);

Parameters devId is the address of the I2C device, it can be found through the i2cdetect command(see Appendix) and the devId of I2C LCD1602 is generally 0x27.

void write(int x, int y, char data[]){}

In this function, data[] is the character to be printed on the LCD, and the parameters x and y determine the printing position (line y+1, column x+1 is the starting position of the character to be printed).

  • For Python Language Users

Step 3: Change directory.

    cd /home/pi/davinci-kit-for-raspberry-pi/python/

Step 4: Run.

    sudo python3 1.1.7_Lcd1602.py

After the code runs, you can see ”Greetings”,”From SunFounder” displaying on the LCD.

Code

import LCD1602
import time

def setup():
    LCD1602.init(0x27, 1)   # init(slave address, background light)
    LCD1602.write(0, 0, 'Greetings!!')
    LCD1602.write(1, 1, 'from SunFounder')
    time.sleep(2)

def destroy():
    LCD1602.clear()

if __name__ == "__main__":
    try:
        setup()
    except KeyboardInterrupt:
        destroy()

Code Explanation

import LCD1602

This file is an open source file for controlling I2C LCD1602. It allows us to easily use I2C LCD1602.

LCD1602.init(0x27, 1) 

The function initializes the I2C system with the designated device symbol. The first parameter is the address of the I2C device, which can be detected through the i2cdetect command (see Appendix for details). The address of I2C LCD1602 is generally 0x27.

LCD1602.init(0x27, 1) 

Within this function, ‘Greetings!! ‘ is the character to be printed on the Row 0+1, column 0+1 on LCD.

Now you can see “Greetings! From SunFounder” displayed on the LCD.

Phenomenon Picture