100% found this document useful (2 votes)
547 views

I2C LCD With ESP32

This document provides instructions for using an I2C LCD display with an ESP32 microcontroller using the Arduino IDE. It describes wiring the LCD to the ESP32 using the I2C pins, installing the LiquidCrystal_I2C library, and includes code examples to display static and scrolling text on the LCD. A function is defined to scroll text longer than the LCD width across the display with a customizable delay.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
547 views

I2C LCD With ESP32

This document provides instructions for using an I2C LCD display with an ESP32 microcontroller using the Arduino IDE. It describes wiring the LCD to the ESP32 using the I2C pins, installing the LiquidCrystal_I2C library, and includes code examples to display static and scrolling text on the LCD. A function is defined to scroll text longer than the LCD width across the display with a customizable delay.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

How to Use I2C LCD with ESP32

on Arduino IDE
This work shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using
Arduino IDE.
16×2 I2C Liquid Crystal Display

The advantage of using an I2C LCD is that the wiring is really simple. You just need to wire
the SDA and SCL pins. Additionally, it comes with a built-in potentiometer you can use to
adjust the contrast between the background and the characters on the LCD. On a “regular”
LCD you need to add a potentiometer to the circuit to adjust the contrast.
Parts Required
 ESP32 DOIT DEVKIT V1 Board
 16×2 I2C Liquid Crystal Display (LCD)
 Female to female jumper wires

Wiring the LCD to the ESP32


This display uses I2C communication, which makes wiring really simple. Wire your LCD to
the ESP32 by following the next schematic diagram. We’re using the ESP32 default I2C
pins (GPIO 21 and GPIO 22).

I2C LCD ESP32

GND GND

VCC VIN

SDA GPIO 21

SCL GPIO 22

You can also use the following table as a reference.


Installing the LiquidCrystal_I2C Library
There are several libraries that work with the I2C LCD. We’re using this library by Marco
Schwartz. Follow the next steps to install the library:
1. Click here to download the LiquidCrystal_I2C library.
2. Unzip the .zip folder and you should get LiquidCrystal_I2C-master folder
3. Rename your folder from LiquidCrystal_I2C-master to LiquidCrystal_I2C
4. Move the LiquidCrystal_I2C folder to your Arduino IDE installation libraries folder
5. Finally, re-open your Arduino IDE

Display Static Text on the LCD


Here’s a very simple sketch example that displays “Hello, World!“.

#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows


int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows


// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}
void loop(){
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("Hello, World!");
delay(1000);
// clears the display to print new message
lcd.clear();
// set cursor to first column, second row
lcd.setCursor(0,1);
lcd.print("Hello, World!");
delay(1000);
lcd.clear();
}
Display Scrolling Text on the LCD
Scrolling text on the LCD is specially useful when you want to display messages longer than
16 characters. The library comes with built-in functions that allows you to scroll
text. However, many people experience problems with those functions because:

 The function scrolls text on both rows. So, you can’t have a fixed row and a scrolling
row;
 It doesn’t work properly if you try to display messages longer than 16 characters.

The following sketch displays a static message in the first row and a scrolling message
longer than 16 characters in the second row.
#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows


int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows


LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

String messageStatic = "Static message";


String messageToScroll = "This is a scrolling message with more than 16
characters";

// Function to scroll text


// The function acepts the following arguments:
// row: row number where the text will be displayed
// message: message to scroll
// delayTime: delay between each character shifting
// lcdColumns: number of columns of your LCD
void scrollText(int row, String message, int delayTime, int lcdColumns) {
for (int i=0; i < lcdColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int pos = 0; pos < message.length(); pos++) {
lcd.setCursor(0, row);
lcd.print(message.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}

void loop(){
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print static message
lcd.print(messageStatic);
// print scrolling message
scrollText(1, messageToScroll, 250, lcdColumns);
}

After reading the previous section, you should be familiar on how this sketch works, so we’ll just
take a look at the newly created function: scrollText()

void scrollText(int row, String message, int delayTime, int lcdColumns) {


for (int i=0; i < lcdColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int pos = 0; pos < message.length(); pos++) {
lcd.setCursor(0, row);
lcd.print(message.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}

To use this function you should pass four arguments:

 row: row number where the text will be display


 message: message to scroll
 delayTime: delay between each character shifting. Higher delay times will result in slower
text shifting, and lower delay times will result in faster text shifting.
 lcdColumns: number of columns of your LCD
In our code, here’s how we use the scrollText() function:
scrollText(1, messageToScroll, 250, lcdColumns);
The messageToScroll variable is displayed in the second row (1 corresponds to the second
row), with a delay time of 250 ms (the GIF image is speed up 1.5x).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy