Last updated
LCD1602 liquid crystal display is a widely used character type liquid crystal display module. It is composed of a character-type liquid crystal display (LCD), a control driver main circuit HD44780, etc., and is mainly used to display character-type data.
在HD44780内部含有DDRAM、CGROM以及CGRAM;
SCL is the serial clock line for the I2C LCD interface.
SDA is the serial data line for the I2C LCD interface.
VCC is the LCD’s power supply input pin (connects to +5v).
GND is the ground pin.
Here is the wiring diagram for the LCD 20×4 I2C display with Arduino that we’ll be using in the examples hereafter in this tutorial.
And here is a summary table for different Arduino Boards -> I2C LCD connections.
After connecting the I2C LCD module, you’ll be able to control the LCD contrast by using the PCF8574 module’s on-board potentiometer. Turn it to the right and to the left until you feel satisfied with the current contrast level.
If you’re not quite sure about the I2C LCD’s device address, you can use the code example below and run it on your Arduino board after connecting the I2C LCD display to your Arduino. It’ll automatically detect and print out the I2C device address for the LCD over the serial monitor.
And here is the result after running this code example on my Arduino UNO board.
You can download and install the Arduino I2C LCD library manually from GitHub, or alternatively, install it within the Arduino IDE itself. Just open the library manager. Tools > Manage Libraries > Search for LiquidCrystal I2C. Then, click Install and let it finish the installation.
Now, you can easily use the Arduino LiquidCrystal I2C LCD library and check the built-in examples for the library to help you get started.
We’ll move now to the practical code examples to test the Arduino I2C LCD display.
Now, let’s test what we’ve learned so far about the Arduino LCD 20×4 I2C and create our first project to display text messages on 4 separate lines of the LCD using the .print() function.
The .print() function can also accept a lot of variable data types (strings, integers, float, double, etc). So luckily, we won’t need to do string manipulations and data type conversion.
Wiring
This wiring of Arduino with LCD 20×4 I2C is the same as shown before in the previous section.
Example Code
Here is the full code listing for this example.
Code Explanation
First of all, we need to include the Arduino Wire.h library to use the I2C communication module, then the LiquidCrystal_I2C.h library which we’ll be using to control the I2C LCD module (PCF8574).
Next, we’ll create an object of the LiquidCrystal_I2C class and define its parameters. The parameters for the LiquidCrystal_I2C object are the I2C device address (0x27), the number of LCD columns, and the number of LCD rows. Those are (20, 4) for 20×4 LCDs.
setup()
in the setup() function, we initialize the I2C LCD object ( MyLCD) using the .init() function. We also activate the LCD’s backlight by using the .backlight() function.
Then, we set the LCD cursor position to point to the first line, the first character cell. Any write operation to the LCD will start from the current cursor position value, that’s why it’s important to set it manually before attempting any write operation. To make sure that the text will be displayed exactly where we want it to be.
Next, we’ll print the first text message "Hello World!" to the LCD starting from the current cursor position (0, 0). Using the .print() function.
and we’ll do the same for the other 3 messages to display them on the subsequent lines on the LCD.
loop()
Nothing to be done in the loop() function.
Testing Results
Here is the result of testing this project code example on my Arduino UNO board.
名称 | 功能 |
---|---|
序号 | 符号 | 功能说明 |
---|---|---|
Dimensions | 98 mm x 60 mm x 14 mm |
---|---|
DDRAM(显示数据RAM)
用来寄存待显示的字符代码
CGROM(字符产生ROM)
LCD内部固化字模存储器,内部含有常用字符
CGRAM(字符产生RAM)
用户自定义的字符产生存储器
1
GND
电源地
2
VCC
电源
3
VO
对比调整电压(液晶显示偏压)
4
RS
指令/数据选择
5
RW
读/写选择
6
E
使能信号
7-14
DB0-DB7
数据总线
15
BG VCC
背光电源正极
16
BG GND
背光电源负极
Resolution
20 characters x 4 lines
LCD type
STN, positive, transflective, blue
Backlight
Blue
Modes
Parallel (8-bit and 4-bit)
I2C adapter / I2C address
0x27; Address can be selected - area 0x20 to 0x27
Operating voltage
5V
Operating temperature
From -20 ° C to +70 ° C
SDA
SCL
Arduino UNO, Nano, Pro Mini
A4
A5
Arduino Mega
20
21
Arduino Leonardo, Micro
2
3