博客专栏

EEPW首页 > 博客 > 「电子DIY」使用带ESP32的16×2I2C液晶显示器

「电子DIY」使用带ESP32的16×2I2C液晶显示器

发布人:电子资料库 时间:2023-02-14 来源:工程师 发布文章

几周前,研究了一下ESP32模块建立了一个简单的hello world程序来熟悉板子。今天,我们将继续在更高的层次上探索ESP32,我们将研究如何将16×2液晶显示器与它连接起来。

image.png


I2C 16×2液晶显示器

这可能是最受欢迎的液晶显示器制造商之一,也是最受欢迎的液晶显示器制造商之一。在今天的教程中,我们将使用I2C基于16×2液晶显示器显示器,因为它需要方便的布线。它只使用四个管脚,而其他版本的显示器需要至少7个管脚连接到微控制器板上。

image.png


必须是ESP32 DevKit V1

ESP32以模块形式出现,就像它的前身ESP-12e一样,因为使用该模块通常需要一个转接板。因此,当它要在没有定制PCB的应用程序中使用时,使用基于它的开发板会更容易。在今天的教程中,我们将使用必须是ESP32 DevKit V1它是最受欢迎的ESP32开发板之一。

为了演示I2C驱动的LCD和NodeMCU的使用,我们将研究如何在LCD上显示静态和滚动消息。

所需组件

建设本项目需要以下组成部分:;

  1. 必须是ESP32 DevKit V1板

  2. 试验板

  3. 16×2i2c液晶显示器

  4. 跨接导线

试验板要求是可选的,因为您可以选择使用母跳线将LCD直接连接到DOIT devkit板。

示意图

这个项目的原理图相对简单,因为我们只将LCD连接到doitdevkitv1。因为我们使用I2C进行通信,所以我们将把LCD的引脚连接到DevKit的I2C引脚上。按如下所示连接部件。

image.png


示意图

显示组件连接方式的管脚图如下所示。

LCD–ESP32

GND - GNDVCC - 3.3v/VinSDA - D2(GPIO4)SCL - D1 (GPIO 5)

由于LCD的电源要求,当连接到ESP32的3.3v引脚时,可能亮度不够。如果是这种情况,请将LCD的VCC引脚连接到ESP32的Vin引脚,这样它就可以直接从连接的电源获取电源。

检测LCD的I2C地址

在这一点上,需要注意的是,需要一个特殊的设置,使您能够使用Arduino IDE编程基于ESP32的电路板。我们在ESP32教程简介几周前出版。所以,一定要去看看。

能够轻松编写与I2C液晶显示器,我们将使用I2C LCD库。该库具有使LCD寻址更容易的功能和命令。下载I2C LCD库从附加的链接安装到Arduino IDE上,只需将其解压缩到Arduino的库文件夹中即可。

在为项目编写代码之前,了解I2C地址因为没有它我们将无法与显示器对话。

虽然有些液晶显示器上有显示的地址或卖家提供的地址,但在没有地址的情况下,您可以使用一个简单的草图来确定地址,即****I2C线,以检测在其地址旁边连接的设备。这个草图也是测试布线正确性或确定LCD是否正常工作的好方法。

复制下面的代码并粘贴到Arduino IDE中。



#include <Wire.h>

void setup() {

Wire.begin();

Serial.begin(115200);

Serial.println("nI2C Scanner");

}

void loop() {

byte error, address;

int nDevices;

Serial.println("Scanning...");

nDevices = 0;

for(address = 1; address < 127; address++ )

{

// The i2c_scanner uses the return value of

// the Write.endTransmisstion to see if

// a device did acknowledge to the address.

Wire.beginTransmission(address);

error = Wire.endTransmission();

if (error == 0) {

Serial.print("I2C device found at address 0x");

if (address<16) {

Serial.print("0");

}

Serial.println(address,HEX);

nDevices++;

}

else if (error==4) {

Serial.print("Unknow error at address 0x");

if (address<16) {

Serial.print("0");

}

Serial.println(address,HEX);

}

}

if (nDevices == 0) {

Serial.println("No I2C devices foundn");

}

else {

Serial.println("donen");

}

delay(5000);

}

这个草图基本上使用一个“for”循环来生成一个地址列表,然后向地址发送一个begin transmission请求。的返回值Write.endTransmission()函数显示该地址上是否存在设备。收到响应的地址就是我们要查找的地址。

验证并将代码上载到ESP32板并打开串行监视器。您应该看到如下图所示的地址:

image.png


设备地址

如果你总是“找不到任何设备”,那么看看这些连接,以确保你没有把事情弄混,你也可以继续尝试0x27个作为I2C地址. 这是大多数来自中国的I2C LCD模块的通用地址。

有了地址,我们现在可以为这个项目编写代码了。

代码

我们今天教程的任务是在LCD上显示静态和滚动文本,为了实现这一点,我们将使用I2C LCD库来减少需要编写的代码量。我们将写两个单独的草图;一个展示静态文本另一个同时显示静态和滚动文本 .

静态文本

为了从静态文本显示的草图开始,我们通过包含要用于它的库(在本例中是i2clcd库)来开始代码。

#include <LiquidCrystal_I2C.h>

接下来,我们创建一个I2CLCD库类的实例,其中包含显示器的地址、显示器的列数(本例中为16列)和行数(本例中为2行)作为参数。

LiquidCrystal_I2C lcd(0x27, 16, 2);

完成后,我们进入虚空设置()功能。在这里,我们初始化显示器并发出命令打开背光,因为根据LCD的不同,背光灯在默认情况下可能是关闭的。

void setup(){

// initialize LCD

lcd.init();

// turn on LCD backlight

lcd.backlight();

}

接下来是无效循环()功能。循环代码背后的思想很简单,我们首先将光标设置到显示的列和行,然后使用lcd.print()功能。为了允许文本在重新加载循环之前在屏幕上停留一段时间(因此它是可见的),我们延迟了1000毫秒 .

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

}

项目的完整代码可以在下面找到,也可以在下载部分下附加。

#include <LiquidCrystal_I2C.h>

// 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, 16, 2);

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

}

滚动文本

对于滚动文本,我们将使用 坏桑托斯属于 RandomNerdTutorials.com网站. 此代码允许在第一行显示静态文本,同时在第二行显示滚动文本。

和往常一样,我们首先包括我们将用于草图的库,在本例中是相同的I2C LCD库 .

/*********

Rui Santos

<blockquote class="wp-embedded-content" data-secret="B8ira1D4ho"><a href="https://randomnerdtutorials.com/">Home</a></blockquote><iframe title="“Home” — Random Nerd Tutorials" class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="https://randomnerdtutorials.com/embed/#?secret=B8ira1D4ho" data-secret="B8ira1D4ho" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

*********/

#include <LiquidCrystal_I2C.h>

接下来,我们创建一个I2CLCD库类的实例,其中包含显示器的地址、显示器的列数(本例中为16列)和行数(本例中为2行)作为参数。

LiquidCrystal_I2C lcd(0x27, 16, 2);

接下来,我们创建变量来保存要显示的消息。

String messageStatic = "Static message";

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

接下来,我们创建函数来显示滚动文本。该函数接受四个参数:显示滚动文本的行、要显示的文本、字符移动之间的延迟时间以及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();

}

完成后,我们就去无效循环()功能。我们首先设置光标,然后使用print函数显示静态文本和 滚动文本()函数来显示滚动文本

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);

}

下面提供了该草图的完整代码,也可以在本教程的下载部分中找到。

// Adapted from the code by

// Rui Santos

// https://randomnerdtutorials.com

#include <LiquidCrystal_I2C.h>

// if you don't know your display address, run an I2C scanner sketch

LiquidCrystal_I2C lcd(0x27, 16, 2);

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, 16);

}

演示

确保连接正确,连接必须是devkit上传两张草图中的任何一张。您应该会看到这个显示出现了如下图所示的文本。


演示

今天的教程就到此为止。感谢您遵循本教程。这种实用的LCD显示屏为您的项目提供了一种很好的视觉反馈方式,即使屏幕大小和显示质量受到限制,通过滚动功能,您可以增加可以显示的文本/字符量。

请在评论区随时联系我,与问题和教程的意见。


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。



关键词: 液晶 显示器

相关推荐

技术专区

关闭