3.4 - Liquid Crystal Display

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

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.

pin_i2c

Here we will use the I2C0 interface to control the LCD1602 and display text.

Schematic

sch_lcd

Wiring

wiring_lcd

Code

Note

  • You can open the file 3.4_liquid_crystal_display.ino under the path of kepler-kit-main/arduino/3.4_liquid_crystal_display.

  • Or copy this code into Arduino IDE.

  • For detailed tutorials, please refer to Open & Run Code Directly.

Don’t forget to select the Raspberry Pi Pico W board and the correct port before clicking the Upload button.

The libraries Wire and LiquidCrystal_I2C are used here, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Add libraries.

After the program runs, you will be able to see two lines of text appear on the LCD in turn, and then disappear.

Note

When the code is running, if the screen is blank, you can use a screwdriver to turn the potentiometer on the back of the module to increase the contrast.

How it works?

By calling the library LiquidCrystal_I2C.h, you can easily drive the LCD.

#include "LiquidCrystal_I2C.h"

Library Functions:

LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)

Creates a new instance of the LiquidCrystal_I2 C class that represents a particular LCD attached to your Arduino board.

lcd_AddR: The address of the LCD defaults to 0x27. lcd_cols: The LCD1602 has 16 columns. lcd_rows: The LCD1602 has 2 rows.

void init()

Initialize the lcd.

void backlight()

Turn the (optional) backlight on.

void nobacklight()

Turn the (optional) backlight off.

void display()

Turn the LCD display on.

void nodisplay()

Turn the LCD display off quickly.

void clear()

Clear display, set cursor position to zero.

void setCursor(uint8_t col,uint8_t row)

Set the cursor position to col,row.

void print(data,BASE)

Prints text to the LCD.

data: The data to print (char, byte, int, long, or string).

BASE (optional): The base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

Learn More

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

Note

  • You can open the file 3.4_liquid_crystal_display_2.ino under the path of kepler-kit-main/arduino/3.4_liquid_crystal_display_2.

  • Or copy this code into Arduino IDE.

  • For detailed tutorials, please refer to Open & Run Code Directly.

Don’t forget to select the Raspberry Pi Pico W board and the correct port before clicking the Upload button.

In addition to reading data from the electronic components, the Pico W can read the data input in the serial port monitor, and you can use Serial.read() as the controller of the circuit experiment.

Run the serial communication in setup() and set the data rate to 9600.

Serial.begin(9600);

The state of serial port monitor is judged in loop(), and the information processing will be carried out only when the data are received.

if (Serial.available() > 0){}

Clear the screen.

lcd.clear();

Reads the input value in the serial port monitor and stores it to the variable incomingByte.

char incomingByte = Serial.read();

Display each character to the LCD and skip the line-feed character.

while (Serial.available() > 0) {
    char incomingByte=Serial.read();
    if(incomingByte==10){break;}// skip the line-feed character
    lcd.print(incomingByte);// display each character to the LCD
}