Acoptex.Com

Learn ESP32, ESP8266, Arduino, and Raspberry Pi

Arduino Tutorial for LCD1602 I2C display

We are going to learn about the LCD1602 (LCD16x2) I2C display and how to use it with Arduino board.

We will print the message ‘Hello World! LCD tutorial’ on the LCD1602 (LCD16x2) I2C display screen.

Tag: Project 010 arduino i2c lcd 1602 example with LiquidCrystal I2C library by Frank de Brabander

Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you. Acoptex.com

Project resources

  • Libraries: We have used the library – LiquidCrystal_I2C-1.1.2.zip which we downloaded, unzipped, changed the name of folder to LiquidCristal and added to libraries in our PC, for example C:\Users\acoptexCom\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. If you have LiquidCristal folder in this location already – delete this folder and copy folder, which was made by you, to this location.
  • Code: sketch;
  • Datasheets: NXP Semiconductors and Texas Instruments;
  • Other: Fritzing drawings and views.

Parts required

1. Arduino Uno R3 1 pc (you can also use the other version of Arduino);

Arduino Uno

2. LCD16x2 I2C Display module 1 pc;

LCD16x2 (LCD1602) I2C display

3. Arduino IDE ( you can download it from here  )

4. Jumper cables F-M 4 pcs.

jumper cables

Understanding LCD16x2 I2C module

If you have ever tried to connect an LCD16x2 display with an Arduino board, you may have noticed that it is using a lot of Arduino board pins. Even in 4-bit mode, the Arduino still requires 7 connections – that is half of Arduino’s available digital I/O pins. The best solution is to use an LCD16x2 I2C Display. It consumes only two I/O pins that are not even part of a set of digital I/O pins and can also be shared with other I2C devices.

All LCD displays based on Hitachi HD44780 controller have two types of memories that store defined characters called CGROM and CGRAM (Character Generator ROM & RAM). CGROM memory is non-volatile and can’t be modified whereas; CGRAM memory is volatile and can be modified any time. CGROM is used for storing all permanent fonts that can be displayed by using their ASCII code. For example, if we write 0x41 then on the LCD we get character ‘A’. CGRAM is another memory that can be used for storing user defined characters. This RAM is limited to 64 bytes. Meaning, for 5×8 pixel based LCD; up to 8 user-defined characters can be stored in the CGRAM. And for 5×10 pixel based LCD, only 4 user-defined characters are can be stored.

A typical LCD16x2 I2C Display consists of a HD44780 based character LCD Display (LCD1602 Display) and an I2C LCD adapter.

LCD16x2 (LCD1602) I2C display

The meaning of LCD1602 (LCD16x2) marking is Liquid Cristal Display 16 characters by 2 rows, it can display 32 ASCII characters in two rows with 16 characters on each row of either 5×7 or 5×8 dot matrix characters. The LCD is available in a 16 pin package. It consists of LED backlight and contrast adjustment function and each dot matrix has 5×8 dot resolution.

If you look closely, you can actually see the little rectangles for each character on the display and the pixels that make up a character. Each of these rectangles is a grid of 5×8 or 5×7 pixels.

Each of these rectangles is a grid of 5×8 or 5x7 pixels.

At the heart of the I2C LCD Adapter is an 8-Bit I/O Expander chip – PCF8574. This chip converts the I2C data from an Arduino into the parallel data required by the LCD display. The board also comes with a small trimpot to make display’s contrast adjustments. There is a jumper on the board that supplies power to the backlight. To control the intensity of the backlight, you can remove the jumper and apply an external voltage to the header pin that is marked as LED.

LCD1602 I2C Display backside

If you are using multiple devices on the same I2C bus, you may need to set a different I2C address for the board, so that it does not conflict with another I2C device. To do so, the board has three solder jumpers (A0, A1 and A2) or solder pads. Each of these is used to hardcode the address. If a jumper is shorted with a blob of solder, it sets the address. An important point here is that many companies manufacture the same PCF8574 chip: Texas Instruments and NXP Semiconductors to name a few. And the I2C address of your LCD depends on the chip manufacturer.

solder pads of I2C Display adapter

Signals and connections of LCD1602 I2C module

I2C address

If your LCD16x2 I2C Display has a PCF8574 chip from Texas Instruments:

According to the Texas Instruments’ datasheet, three address selection bits (A0, A1 and A2) are placed at the end of the 7-bit I2C address register.

I2C address

As there are 3 address inputs, which can take 2 states, either HIGH/LOW, we can therefore create 8 (23) different combinations(addresses). By default, all the 3 address inputs are pulled HIGH using onboard pullups, giving PCF8574 a default I2C address of 0100111 Binary or 0x27 Hex.

By shorting the solder jumpers, the address inputs are puled LOW. If you were to short all three jumpers, the address would be 0x20. The range of all possible addresses spans from 0x20 to 0x27. Please refer the below table.

If your LCD16x2 I2C Display has a PCF8574 chip from Texas Instruments

If your LCD16x2 I2C Display has a PCF8574 chip from NXP Semiconductors:

According to the NXP Semiconductors’ datasheet, three address selection bits (A0, A1 and A2) are also placed at the end of the 7-bit I2C address register. But other bits in the Address Register are different.

I2C address

The 3 address input can have 2 states, either HIGH / LOW, giving us 8 (23) different combinations.

By default, all the 3 address inputs are pulled HIGH using onboard pullups, giving PCF8574 a default I2C address of 0111111 Binary or 0x3F Hex.

By shorting the solder jumpers, the address inputs are puled LOW. If you were to short all three jumpers, the address would be 0x38. The range of all possible addresses spans from 0x38 to 0x3F. Please refer the below table.

If your LCD16x2 I2C Display has a PCF8574 chip from NXP Semiconductors

Pinout

LCD1602 I2C Display has 4 pins.

GNDa ground pin and should be connected to the ground of Arduino board.
VCCsupplies power to the module and the LCD. Connect it to the 5V
output of the Arduino or a separate power supply.
SDAa Serial Data pin. This line is used for both transmit and receive. Connect to the SDA pin on the Arduino board.
SCLa Serial Clock pin. This is a timing signal supplied by the Bus Master device. Connect to the SCL pin on the Arduino board.

Wiring

It is much easier to connect an LCD16x2 I2C Display than to connect a LCD16x2. You only need to connect 4 pins instead of 12. Start by connecting VCC(VIN) pin to the 5V output on the Arduino board and connect GND to ground.

Now we are remaining with the pins that are used for I2C communication. Note that each Arduino Board has different I2C pins which should be connected accordingly. On the Arduino boards with the R3 layout, the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. They are also known as A5 (SCL) and A4 (SDA).

If you are using a different Arduino board, please refer below table.

Board / PinsSDASCL
Arduino UnoA5A4
Arduino NanoA5A4
Arduino Mega2120
Leonardo/Micro32
Wiring LCD16x2 (LCD1602) I2C display with Arduino Uno | Acoptex.com
Wiring LCD16x2 (LCD1602) I2C display with Arduino Uno | Acoptex.com

Step by Step instruction

  1. Do wiring. You will need to adjust the contrast of the display. On the I2C module, you will find a potentiometer that you can turn with a small screwdriver.
  2. Plug your Adruino Uno board into your PC/Laptop USB port. You should see the backlight light up. Now rotate the potentiometer until the first line of the rectangle appears.
  3. Open Arduino IDE. Select the correct board and COM port.
  4. It is recommended that you find out the actual I2C address of the LCD16x2 I2C Display before using. It depends on the manufacturer: if your LCD has a PCF8574 chip from Texas Instruments, its default I2C address is 0x27. If your LCD has a PCF8574 chip from NXP semiconductors, its default I2C address is 0x3F.
  5. Open and upload the sketch from http://playground.arduino.cc/Main/I2cScanner and follow the instructions to use it.  
  6. Open Arduino Serial Monitor at 9600 baud and Arduino board will find the actual I2C address.  Even though the documentation said it was 0x27, this sketch can detect different (in my case 0x3F)
  7. Modify the sketch: the line LiquidCrystal_I2C lcd(0x3F,16,2); (See part marked bold)
  8. Verify and upload the sketch to your Adruino Uno.
  9. You will see the message Hello World! LCD tutorial displayed. If you want you can change the text of the message.
LCD16x2 I2C Display with Arduino board | Acoptex.com

Code

#include <LiquidCrystal_I2C.h>// we are using LiquidCrystal_I2C library

LiquidCrystal_I2C lcd(0x3F,16,2);  // the lcd object of LiquidCrystal_I2C class is created. This object uses 3 parameters - address,columns,rows. 
//This is where you will need to change the default address to the address you found earlier if it happens to be different, and dimensions of the display.

void setup() {
  lcd.init(); //  initialize the lcd object
  lcd.clear();//  clears the LCD screen and moves the cursor to the top-­left corner        
  lcd.backlight(); // make sure backlight is on
  
  lcd.setCursor(2,0);   // set cursor to character 2 on line 0
  lcd.print("Hello world!");  // print a message
  
  lcd.setCursor(2,1);   // move cursor to character 2 on line 1
  lcd.print("LCD Tutorial"); // print a message
}

void loop() {}

Other useful functions of the Library

There are a few useful functions you can use with LiquidCrystal_I2C object. Few of them are listed below:

  • home() – positions the cursor in the top-left corner of the LCD without clearing the display.
  • cursor() – displays the LCD cursor, an underscore (line) at the position of the next character to be printed.
  • noCursor() – hides the LCD cursor.
  • blink() – creates a blinking block style LCD cursor: a blinking rectangle of 5×8 pixels at the position of the next character to be printed.
  • noBlink() – disables the blinking block style LCD cursor.
  • display() – turns on the LCD screen and displays the characters that were previously printed on the display.
  • noDisplay() – turns off the LCD screen. Simply turning off the LCD screen does not clear data from the LCD memory. This means that it will be shown again when the display() function is called.
  • scrollDisplayLeft() – scrolls the contents of the display one space to the left. If you want to scroll the text continuously, you need to use this function inside a loop.
  • scrollDisplayRight() – scrolls the contents of the display one space to the right.
  • autoscroll() – turns on automatic scrolling of the LCD. If the current text direction is left-to-right (default), the display scrolls to the left, if the current direction is right-to-left, the display scrolls to the right.
  • noAutoscroll() – turns off automatic scrolling.

Wrapping up

We have learned about the LCD1602 I2C Display, how to use it with Arduino board and printed the message Hello World! LCD tutorial on LCD1602 I2C Display.

If you are finding characters on the display dull and unexciting, you can create your own custom characters (glyph) and symbols for your LCD. They are extremely useful when you want to display a character that is not part of the standard ASCII character set. To define a custom character the createChar() function is used. This function accepts an array of 8 bytes. Each byte (only 5 bits are considered) in the array defines one row of the character in the 5×8 matrix. Whereas, 0s and 1s in the byte indicate which pixels in the row should be off and which should be turned on. You can use this WEBSITE to help you create your custom characters. You can click on any of the 5×8 pixels to set/clear a particular pixel. As you click on pixels, the code for the character is generated next to the grid. This code can directly be used in your Arduino sketch.

LCD CUSTOM CHARACTER GENERATOR
#include <LiquidCrystal_I2C.h>// we are using LiquidCrystal_I2C library

LiquidCrystal_I2C lcd(0x3F,16,2);  // the lcd object of LiquidCrystal_I2C class is created. This object uses 3 parameters - address,columns,rows. 
//This is where you will need to change the default address to the address you found earlier if it happens to be different, and dimensions of the display.

/*After including the library and creating the LCD object, the custom character array is defined. This array consists of 8 bytes, 1 byte for each row of the 5×8 led matrix. In this sketch, 1 custom character is created. Let's examine customChar[8] array as an example. You can see how bits are forming a heart shape that are actually 0s and 1s. A 0 sets the pixel off and a 1 sets the pixel on.*/
// make custom character:
byte customChar[8] = {
  B01010,
  B10101,
  B10001,
  B10001,
  B10001,
  B10001,
  B01010,
  B00100
};

void setup() {

  lcd.init();//  initialize the lcd object         
  lcd.backlight();// make sure backlight is on


/*In the setup, the custom character is created using the createChar() function. This function takes two parameters. The first one is a number between 0 and 7 in order to reserve one of the 8 supported custom characters. The second parameter is the name of the array of bytes.*/

  lcd.createChar(0, customChar);   // create a new character

  lcd.clear();// clears the LCD screen
  
  lcd.print("Custom Character");// prints a message
}

/*Next in the loop, to display the custom character we use write() function and as a parameter we use the number of the character that we want to display.*/
void loop() 
{ 
  lcd.setCursor(0, 1);
  lcd.write(0);
}

There are other libraries available for LCD16x2 I2C Display: NewliquidCrystal library. You will need to use different code with each library to run your LCD16x2 I2C Display.

Thank you for reading!

Other similar posts

Arduino Guide for LCD16x2 I2C display (NewliquidCrystal library)

Check for more DIY projects on Acoptex.lt and Acoptex.com!

If you are looking for high quality PCBs PCBWay is the best choice: