Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 26: I2C LCD 1602

In this lesson, you will learn how to set up and display messages on a 16x2 Liquid Crystal Display (LCD) with an I2C interface using Arduino. We’ll cover the basics of using the LiquidCrystal I2C library to initialize the LCD, display text, and control the backlight. You’ll see how to print “Hello world!” and “LCD Tutorial” on the display, providing a hands-on introduction to interfacing LCDs with Arduino. This tutorial is perfect for beginners as it offers a practical lesson in controlling electronic displays.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

I2C LCD 1602

BUY

Wiring

../_images/Lesson_26_I2C_lcd_circuit_uno_bb.png

Code

Note

To install the library, use the Arduino Library Manager and search for “LiquidCrystal I2C” and install it.

Code Analysis

  1. Library Inclusion and LCD Initialization: The LiquidCrystal I2C library is included to provide functions and methods for LCD interfacing. Following that, an LCD object is created using the LiquidCrystal_I2C class, specifying the I2C address, number of columns, and number of rows.

    Note

    To install the library, use the Arduino Library Manager and search for “LiquidCrystal I2C” and install it.

    #include <LiquidCrystal_I2C.h>
    LiquidCrystal_I2C lcd(0x27, 16, 2);
    
  2. Setup Function: The setup() function is executed once when the Arduino starts. In this function, the LCD is initialized, cleared, and the backlight is turned on. Then, two messages are displayed on the LCD.

    void setup() {
      lcd.init();       // initialize the LCD
      lcd.clear();      // clear the LCD display
      lcd.backlight();  // Make sure backlight is on
    
      // Print a message on both lines of the LCD.
      lcd.setCursor(2, 0);  //Set cursor to character 2 on line 0
      lcd.print("Hello world!");
    
      lcd.setCursor(2, 1);  //Move cursor to character 2 on line 1
      lcd.print("LCD Tutorial");
    }