Lesson 13 I2C LCD1602

Share for us

Overview

In this lesson, you will learn about LCD1602. LCD1602, or 1602 character-type liquid crystal display, a kind of dot matrix module to show letters, numbers, characters and so on.

Components Required

Component Introduction

It’s composed of 5×7 or 5×11 dot matrix positions; each position can display one character. There’s a dot pitch between two characters and a space between lines, thus separating characters and lines. The number 1602 means on the display, 2 rows can be showed and 16 characters in each.

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 (SDA) line and the serial clock line(SCL).

Experimental Procedures

Step 1: Build the circuit.

Step 2: Open the code file.

Step 3: Select the Board and Port.

Step 4: Upload the sketch to the board.

Upload the codes to the Mega2560 board, the content that you input in the serial monitor will be printed on the LCD.

Code Analysis

Code Analysis 13-1 Include a library

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

The libraries Wire.h and LiquidCrystal_I2C.h are used in these codes, Wire.h is built in Arduino, but LiquidCrystal_I2C.h needs adding manually. Add Method: Refer to Lesson 2 Add Libraries.

Code Analysis 132  Define the pins of LCD1602

LiquidCrystal_I2C lcd(0x27,16,2);

set the LCD address to 0x27 for a 16 chars and 2 line display. 

Code Analysis 133   Initialize the LCD

lcd.init(); // initialize the lcd

lcd.backlight();

backlight() is to turn the (optional) backlight on.

Code Analysis 134  Print the content obtained from the serial monitor

// when characters arrive over the serial port…

  if (Serial.available()) {

    // wait a bit for the entire message to arrive

    delay(100);

    // clear the screen

    lcd.clear();

    // read all the available characters

    while (Serial.available() > 0) {

      char incomingByte=Serial.read();

    // skip the line-feed character

      if(incomingByte==10){break;}

      // display each character to the LCD    

      lcd.print(incomingByte);

    }

  }