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!

2.5 I2C Interface¶

In this lesson, we’ll delve into the capabilities of the I2C interface, a cornerstone for communication between microcontrollers and various peripherals. Our focus will be on utilizing the ESP32’s I2C interface to drive an LCD1602 module for character display.You will learn how to initialize the LCD module, configure display parameters, and send text data to be shown on the screen. Whether you aim to display custom messages, sensor readings, or interactive menus, mastering the LCD1602 will expand your ability to create informative and interactive displays.

Available Pins

Here is a list of available pins on the ESP32 board for this project.

Available Pins

Usage Description

IO21

SDA

IO22

SCL

Required Components

In this project, we need the following components.

COMPONENT INTRODUCTION

PURCHASE LINK

ESP32 WROOM 32E

BUY

ESP32 Camera Extension

-

Several Jump Wires

BUY

I2C LCD1602

BUY

Schematic

../_images/circuit_2.6_lcd.png

Wiring

../_images/2.6_i2clcd1602_bb.png

Code

  1. Download this code or copy this code to the Arduino IDE directly.

Note

When this program is uploaded, the I2C LCD1602 will display the welcome message, “Hello, Sunfounder!”, for 3 seconds. After that, the screen will show a “COUNT:” label and the count value, which increments every second.

Note

If the code and wiring are correct, but the LCD still fails to display any content, you can adjust the potentiometer on the back 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:

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

    LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
    
    • lcd_AddR: The address of the LCD defaults to 0x27.

    • lcd_cols: The LCD1602 has 16 columns.

    • lcd_rows: The LCD1602 has 2 rows.

  • Initialize the lcd.

    void init()
    
  • Turn the (optional) backlight on.

    void backlight()
    
  • Turn the (optional) backlight off.

    void nobacklight()
    
  • Turn the LCD display on.

    void display()
    
  • Turn the LCD display off quickly.

    void nodisplay()
    
  • Clear display, set cursor position to zero.

    void clear()
    
  • Set the cursor position to col,row.

    void setCursor(uint8_t col,uint8_t row)
    
  • Prints text to the LCD.

    void print(data,BASE)
    
    • 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).