
玩ESP32的人迟早会碰到一块屏幕。不管你是做温湿度计、桌面时钟、离线的仪表盘还是想给蓝牙控制的小项目加一个可视化界面LCD屏几乎是绕不开的组件。而网上关于ESP32驱动LCD的教程要么只讲了某个屏的零散接线要么一上来就扔给你一份库文件让你自己琢磨真正能把SPI、8080并口、引脚选择、代码配置这些事讲清楚的反而很少。这篇文章想做的就是这样一件事从最常用的SPI屏开始再到速度更快的8080并口屏把选型、接线、驱动配置、排错这些环节完整走一遍让手里同时拿着ESP32和TFT屏幕的你不再对着资料发愁。我写这篇内容的时候按“从简单到复杂”的顺序来组织。只要你之前点亮过LED或者跑通过一个Blink例程就能跟上节奏如果你已经能玩转SPI设备也能在并口屏的性能优化、DMA配置这些章节里找到可用的细节。好直接开始。1. 看清需求LCD屏驱动到底在解决什么问题1.1 从“点灯”到“点亮屏幕”单色屏、TF
T屏与分辨率很多新手拿到一块LCD屏第一反应是“屏幕是不是就该像手机一样插上电就显示画面”。实际上LCD屏跟单片机之间并没有一个通用的“视频线”。你看到的是MCU通过一组通信协议把像素数据一列一列、一行一行地写到屏幕控制芯片内部的显存里再由控制芯片驱动液晶分子翻转最终显示出画面。按照显示屏的类型常见的有两种。一种是段码屏或者字符屏比如电子表上那种数字显示驱动方式比较原始一般用HT1621这类芯片去扫描另一种就是大家常说的TFT彩色屏像1.8寸ST7735、2.4寸ILI9341这类屏内部有完整的像素矩阵每一个像素点由RGB三色子像素组成。这篇文章主要说的是后者。分辨率决定了你有多少个像素点要处理。128x160的1.8寸屏总共20480个像素320x240的2.4寸屏总共76800个像素。如果你用RGB565格式也就是一个像素占2字节一帧数据量分别是40KB和150KB。这个数字看起来不起
眼但放到8位或者部分32位MCU上传输方式和速度就成了关键问题。这也是为什么驱动屏之前我们要先把接口方案想清楚。1.2 SPI、8080并口、I2C三种常见接口到底差在哪屏和MCU之间的“道路”决定了你能以多快的速度把像素数据搬运到显存里。目前LCD接口大致有三类。I2C接口在低分辨率屏上比较常见比如0.96寸OLED以及部分小尺寸TFT。I2C只需要两根信号线SDA和SCL接线最省但速度通常在400kHz到1MHz之间。对于需要频繁刷新动态数据、切换页面、做动画的彩色TFT屏来说I2C完全是瓶颈流畅度会很差。换句话说I2C适合“静态显示”或者“每隔几秒变一下”的场景。SPI接口是大多数TFT屏的选择。它至少需要SCLK、MOSI、DC、CS、RST这五根线。SPI可以跑20MHz到80MHz一帧320x240的RGB565数据在40MHz下大概需要30ms左右实际刷新率能到20到30fp
s对绝大部分界面交互是足够的。8080并口则是老牌LCD接口从单片机驱动彩屏时代就存在。它用DB0到DB15这样的多根数据线并行传输数据配合WR、RD、CS、DC、RST这些控制线。优势是每个时钟周期可以传8位或者16位数据所以传输速率远高于SPI特别适合像ESP32这类引脚资源充足的主控在需要流畅动画或者高分辨率画面时首选。缺点是占用引脚多接线复杂而且代码里的引脚映射一旦错一位画面就乱得没法看。1.3 选型前必看的3个关键参数决定一块屏幕是否适合你的项目我建议先看这三个参数。第一个是控制器型号。同样的物理尺寸和分辨率屏背后的驱动IC可能完全不同。1.8寸屏常见ST7735S1.3寸或1.54寸常见ST77892.4寸到3.5寸常见ILI93414寸以上还有ILI9488、ST7796等。驱动库基本都是按IC型号来适配的买屏之前一定要确认型号否则代码容易不兼容。第二个是接口类型。有些屏只支
持SPI有些屏通过电阻选择可以切换SPI和8080并口还有一些只支持并口。接口类型直接影响你能用的引脚数量和传输效率。买屏时看商品详情页的“接口方式”或者直接问卖家要规格书规格书里通常会写清楚芯片型号、工作电压、接口定义和背光参数。第三个是背光与供电电压。多数TFT屏的逻辑电压是3.3V背光LED灯串通常需要单独限流控制。如果你用的是5V供电的Arduino板子还要注意电平转换如果你用ESP323.3V IO可以直接驱动大多数屏幕控制IC但背光电流仍要单独处理。别小看第三点很多人屏幕能亮但颜色发暗、或者模块发烫都是供电和背光环节出了问题。2. 硬件选型与接线实操SPI屏和8080并口屏各需要哪些线2.1 常见LCD型号盘点与选购建议先聊选购。以我自己买过的几块屏为例1.8寸ST7735S128x160SPI接口价格便宜适合做小体积仪表但分辨率低、可视角度一般。1.54寸ST7789240x
240SPI接口方屏显示效果好交互界面可以做得比较精致是小项目里很平衡的尺寸。2.4寸ILI9341320x240SPI或8080接口老牌经典资料最全新手出现问题容易搜到解决方案。3.5寸ILI9488480x320通常为8080接口SPI模式对通信速率要求很高一般建议用并口或者RGB接口。选购时尽量选“模块化”的屏也就是厂家已经帮忙把电平转换、背光限流电阻、甚至触摸芯片都集成好的成品模块。这样你只需要把模块的引脚接到主控上即可。如果买的是裸屏还得自己配转接板比较麻烦。另外像“1.8寸TFT LCD分辨率128x160”这种词搜索时很有用你可以直接拿它去找对应的型号和库示例。2.2 ESP32/ESP32-S2引脚分配与接线表重点来了。ESP32和ESP32-S2的引脚资源不同接线前最好先看一眼你手里的开发板原理图确认引脚是否被板载Flash、LED、USB等占用了。下面是基于Arduin
o生态的一套稳妥接法大家可以直接照着接也可以按自己的板子调整。SPI接线以2.4寸ILI9341为例LCD引脚ESP32开发板ESP32-S2开发板VCC3.3V3.3VGNDGNDGNDCSGPIO5GPIO5RSTGPIO4GPIO4DCGPIO2GPIO2MOSI/SDAGPIO23GPIO35SCLK/SCLGPIO18GPIO36LED背光3.3V串电阻或GPIO控制3.3V串电阻或GPIO控制这里有几个细节要注意。ESP32-S2的VSPI/FSPI映射跟ESP32不完全一样默认SPI引脚在不同库中可能不同很多库允许自己在配置里定义所以最终以代码为准。另外ESP32-S2没有传统意义上的GPIO23/18作为SPI默认引脚所以我建议了GPIO35/36这些是ESP32-S2开发板上比较常用的可用引脚。8080并口接线以2.4寸ILI9341并口模块为例16位数据线LCD引脚ES
P32开发板说明CSGPIO5片选RSTGPIO4复位DCRSGPIO2数据/命令选择WRGPIO15写信号RDGPIO16读信号不读屏可接高电平或GPIO控制DB0-DB7GPIO12, 13, 14, 25, 26, 27, 17, 16低8位数据线DB8-DB15GPIO22, 21, 19, 18, 23, 33, 32, 35高8位数据线LED3.3V串电阻或GPIO背光并口接线容易出错的地方是高8位和低8位一定要跟库里的定义一致。如果某一位接反颜色会变得像花屏一样离谱这时候优先检查每一根数据线是否按代码里的宏定义一一对应。另外如果你的屏是8位并口而不是16位可以省略高8位数据线但驱动库初始化时要改成8位模式。2.3 供电与背光最容易忽略的两个坑第一坑是背光。LCD屏模块上的LED脚标着背光如果直接用3.3V和GND去驱动大尺寸屏的背光可能亮度不足或电流过大。很多模块上已经有背光
限流电阻直接接3.3V没问题但有些模块没有长时间满电流运行会发烫、减寿。稳妥做法是接一个IO口通过NPN三极管或者MOS管控制背光配合PWM调节亮度这样还能实现自动息屏。最简单的PWM亮度控制代码pinMode(LED_PIN, OUTPUT); analogWrite(LED_PIN, 128); // 0-255128为半亮度如果模块直接接3.3V亮度太高、或想软件调光就用一颗NPN三极管搭一个低边开关集电极接背光LED负极发射极接地基极串1k电阻接PWM引脚。这种接法能让你用PWM实现平滑的亮度渐变比单纯开关背光实用得多。第二坑是总线电平。有些屏幕控制器同时支持3.3V和5V逻辑但很多TFT裸屏的IO电平上限是3.3V。ESP32本身是3.3V IO所以直连没问题。但如果你的板子带5V的电平比如老款Arduino最好加电平转换否则长期使用可能损伤屏幕时序电路。别小看这个坑很多人一开始
用5V板子点亮了结果用几天屏幕就出问题。3. 驱动代码实现从初始化到显示一张图片3.1 驱动框架选型TFT_eSPI、Arduino_GFX还是LovyanGFX硬件接好接下来就是软件。Arduino生态里LCD驱动库里最有名的三家是TFT_eSPI、Arduino_GFX和LovyanGFX。TFT_eSPI是老牌库由Bodmer维护对ST7735、ST7789、ILI9341等大量IC支持很好。它的特点是通过修改User_Setup.h这个配置文件来定义引脚和屏幕型号不用改代码适合新手。缺点是配置时容易忘记改文件或者搞错IC型号。Arduino_GFX是moononournation维护的库API跟Adafruit GFX接近支持设备很多也支持并口和RGB屏配置方式比TFT_eSPI更灵活一些。LovyanGFX主打性能和更多高级功能比如DMA传送、双缓冲、多屏操作在ESP32上跑分很
高。缺点是学习曲线稍陡API风格跟传统GFX不太一样。我的建议如果是新手第一次点亮屏幕优先用TFT_eSPI因为它网上资料最多教程也最完整。如果是做稍微复杂一点的界面、或者需要特别流畅的动画可以试试LovyanGFX。如果你的项目以后要接LVGL那LovyanGFX和TFT_eSPI都有对应的LVGL移植示例不用担心兼容性。3.2 TFT_eSPI配置与SPI接线验证以ESP32 2.4寸ILI9341 SPI为例先安装TFT_eSPI库然后找到Arduino的libraries文件夹下的TFT_eSPI/User_Setup.h确认这些宏的值#define ILI9341_DRIVER #define TFT_CS 5 #define TFT_DC 2 #define TFT_RST 4 #define TFT_MISO 19 // 通常不用读屏的话也可不接 #define TFT_M
OSI 23 #define TFT_SCLK 18如果你的屏只写不读绝大多数应用都不需要读MISO可以不接但很多屏模块上仍有MISO引脚悬空即可。如果模块上MOSI和SDA两个标号都有通常SDA就是MOSI。配置好之后可以先用官方示例中的graphicstest来验证。刷进去如果看到连续的色块、线条滚动说明接线和初始化没问题如果白屏、花屏就进入后面的排错章节。有一个小提示TFT_eSPI的User_Setup.h里还有多个驱动宏比如ST7789_DRIVER、ST7735_DRIVER如果屏是ST7789但又没改驱动宏会显示不出正确颜色或者花屏反之把ILI9341_DRIVER改成ST7789_DRIVER同时确认显示屏分辨率设置比如ST7789有240x240和240x320两种变体。这类“同尺寸不同IC”的情况是最容易踩的坑买屏时多问卖家一句“主控是什么”能帮你省下大量排查时间。3.
3 8080并口模式下的代码差异如果你的屏是8080并口模块那么TFT_eSPI也支持但配置方式和SPI不同。比如在User_Setup.h里要这样#define ILI9341_DRIVER #define TFT_CS 5 #define TFT_DC 2 #define TFT_RST 4 #define TFT_WR 15 #define TFT_RD 16 #define TFT_D0 12 #define TFT_D1 13 #define TFT_D2 14 #define TFT_D3 25 #define TFT_D4 26 #define TFT_D5 27 #define TFT_D6 17 #define TFT_D7 16 #define TFT_D8 22 #define TFT_D9 21 #define TFT_D10 19 #define TFT_D11
18 #define TFT_D12 23 #define TFT_D13 33 #define TFT_D14 32 #define TFT_D15 35然后TFT_eSPI会自动启用8位或16位并行模式前提是这些宏都定义在User_Setup.h里。需要注意的是并口模式不经过SPI外设而是用GPIO直接模拟总线时序所以代码中对引脚的定义非常关键。部分板子如果同时使用并口和WiFi要注意高位引脚是否和PSRAM、Flash等冲突ESP32的GPIO16/17等通常没问题但不同开发板差异很大。还有一点8080并口模式下有些库会默认启用“读回”功能也就是通过RD引脚读取屏幕显存内容。如果你的接线没接RD或者没有接读取方向的那根线有可能导致初始化卡住。解决办法是把RD引脚接高电平或者在配置里关闭相关读回。3.4 显示中文、图片与动态数据的完整流程先说说显示中文。TFT_eSPI默认不支持中文文
字需要通过字库文件把字模转换成像素数据。最简单的方案是用LVGL的字体工具或者“img2lcd”之类的工具生成字模再用drawBitmap把字模画到屏幕上。如果你不想折腾字模也可以用Adafruit GFX的字体机制但中文字库文件很大不适合直接放在小Flash的板子上。这里给一个比较实用的方案用TFT_eSPI的pushImage函数显示图片配合在线图片转换工具把JPG/PNG转成RGB565数组。比如一张64x64的图标转换后会生成一个uint16_t数组显示时tft.pushImage(x, y, 64, 64, image_data);动态数据部分比如显示温湿度或者时间要在循环里反复调用drawString或者setCursor。注意刷屏区域的频率控制tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setCursor(10, 20); tft.
本文来源:http://www.lpqz.cn/news/80229
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Anyone who works with ESP32 will sooner or later run into a display. Whether you are building a temperature and humidity meter, a desktop clock, an offline dashboard, or adding a visual interface to a small Bluetooth-controlled project, an LCD screen is almost impossible to avoid. Yet the online tutorials on driving an LCD with ESP32 either cover only scattered wiring for one particular panel, or simply hand you a library file and leave you to figure it out on your own. Very few really explain SPI, the 8080 parallel interface, pin selection, and code configuration clearly. That is exactly what this article aims to do: starting with the most common SPI displays and then moving on to the faster 8080 parallel displays, we will walk through the whole process of component selection, wiring, driver configuration, and troubleshooting, so that you, with an ESP32 and a TFT screen in hand, no longer have to stare at documentation in frustration. I have organized the material from simple to complex. As long as you have previously lit up an LED or run a Blink example, you can follow along; and if you are already comfortable with SPI devices, you will also find useful details in the sections on parallel-display performance tuning and DMA configuration. All right, let us get started. 1. Understanding the Need: What Problem Does LCD Driving Actually Solve? 1.1 From Blinking an LED to Lighting Up a Screen: Monochrome Displays, TFT
Displays and Resolution. Many beginners see an LCD for the first time and wonder whether it should simply display an image as soon as you plug it in, just like a phone. In reality, there is no universal video cable between an LCD and a microcontroller. What you see is the MCU writing pixel data column by column and row by row, through a set of communication protocols, into the display memory inside the screen controller chip; the controller then drives the liquid-crystal molecules to twist, and finally an image appears. Displays come in two common types. One is the segment or character display, such as the digits on an electronic watch; its driving method is fairly primitive and usually uses a chip like the HT1621 to scan the segments. The other is what everyone calls a TFT color display, such as the 1.8-inch ST7735 or the 2.4-inch ILI9341. This kind of screen has a complete pixel matrix inside, and each pixel is made up of red, green, and blue subpixels. This article mainly deals with the latter. Resolution determines how many pixels you have to process. A 1.8-inch 128x160 panel has 20,480 pixels in total, while a 2.4-inch 320x240 panel has 76,800 pixels. If you use the RGB565 format, in which one pixel takes two bytes, the data for one frame is 40 KB and 150 KB respectively. These figures do not look very impressive.
However, once you move to an 8-bit or some 32-bit MCUs, the transfer method and speed become critical issues. That is why we need to settle on an interface scheme before we start driving a display. 1.2 What Is the Difference Between SPI, 8080 Parallel, and I2C? The road between the screen and the MCU determines how fast you can move pixel data into the display memory. LCD interfaces today fall roughly into three categories. I2C is common on low-resolution displays, such as 0.96-inch OLEDs and some small TFTs. It needs only two signal lines, SDA and SCL, so it uses the fewest pins, but its speed is usually between 400 kHz and 1 MHz. For a color TFT that needs to refresh dynamic data frequently, switch pages, or run animations, I2C is a complete bottleneck and the smoothness will be poor. In other words, I2C suits static display or scenes that change only every few seconds. SPI is the choice of most TFT screens. It requires at least five lines: SCLK, MOSI, DC, CS, and RST. SPI can run at 20 MHz to 80 MHz; one frame of 320x240 RGB565 data takes roughly 30 ms at 40 MHz, giving an actual refresh rate of about 20 to 30 fps
which is more than enough for the vast majority of interface interactions. The 8080 parallel interface is the old-school LCD interface, present since the days when microcontrollers first drove color screens. It transfers data in parallel over multiple data lines such as DB0 through DB15, together with control lines WR, RD, CS, DC, and RST. Its advantage is that each clock cycle can carry 8 or 16 bits, so the transfer rate is far higher than SPI. It is especially suitable for main controllers with ample pin resources like the ESP32, and is the first choice when you need smooth animation or high-resolution images. The drawbacks are the large number of pins, the complicated wiring, and the fact that if a single pin in the code mapping is wrong, the picture becomes a hopeless mess. 1.3 Three Key Parameters to Check Before Choosing a Display. To decide whether a screen suits your project, I suggest looking at these three parameters first. The first is the controller model. Two panels with the same physical size and resolution may have completely different driver ICs inside. A 1.8-inch screen commonly uses the ST7735S, a 1.3-inch or 1.54-inch screen the ST7789, a 2.4-inch to 3.5-inch screen the ILI9341, and screens above 4 inches may use the ILI9488, ST7796, and others. Driver libraries are basically adapted by IC model, so always confirm the model before buying a screen, or the code may not be compatible. The second is the interface type. Whether a screen supports
only SPI, can switch between SPI and the 8080 parallel interface through resistor selection, or supports the parallel interface only — all of this directly affects how many pins you can use and how efficient the transfer will be. When buying a screen, check the interface method on the product detail page or simply ask the seller for the datasheet; a datasheet usually states the chip model, operating voltage, interface definition, and backlight parameters clearly. The third parameter is the backlight and supply voltage. Most TFT panels use 3.3 V logic, and the backlight LED string usually needs a separate current-limiting control. If you are using a 5 V Arduino board, you also need to pay attention to level shifting; with the 3.3 V I/O of the ESP32 you can directly drive most display controller ICs, but the backlight current still has to be handled separately. Do not overlook this third point: many people get a screen to light up but find the colors dim, or the module gets hot — these are almost always problems in the power supply and backlight stages. 2. Hardware Selection and Wiring in Practice. Which wires does an SPI screen or an 8080 parallel screen need? 2.1 Common LCD Models and Buying Advice. Let us talk about purchasing first. Take a few screens I have bought as examples: the 1.8-inch ST7735S, 128x160, with an SPI interface, is cheap and good for small instruments, but it has low resolution and mediocre viewing angles. The 1.54-inch ST7789 offers 240 x 240 resolution,
with an SPI interface and a square shape; it displays well, allows fairly refined interfaces, and is a well-balanced size for small projects. The 2.4-inch ILI9341, 320x240, supports SPI or 8080; it is a classic with the most documentation, so beginners can easily find solutions when problems arise. The 3.5-inch ILI9488, 480x320, usually uses the 8080 interface; its SPI mode places very high demands on communication speed, so a parallel or RGB interface is generally recommended. When buying, try to choose a modular screen — a finished module where the manufacturer has already integrated level shifting, backlight current-limiting resistors, and even a touch chip. Then you only need to connect the module pins to the main controller. If you buy a bare panel, you will have to prepare an adapter board yourself, which is more troublesome. Search terms such as 1.8-inch TFT LCD 128x160 resolution are very useful; you can use them to find the corresponding model and library examples. 2.2 ESP32/ESP32-S2 Pin Assignment and Wiring Table. Here is the important part. The ESP32 and ESP32-S2 have different pin resources, so before wiring you should look at the schematic of your development board and confirm whether pins are occupied by on-board Flash, LEDs, USB, and so on. Below is a reliable wiring scheme based on the Arduino
platform; you can follow it directly or adapt it to your own board. SPI wiring, taking a 2.4-inch ILI9341 as an example: LCD Pin | ESP32 Dev Board | ESP32-S2 Dev Board. VCC: 3.3 V / 3.3 V. GND: GND / GND. CS: GPIO5 / GPIO5. RST: GPIO4 / GPIO4. DC: GPIO2 / GPIO2. MOSI/SDA: GPIO23 / GPIO35. SCLK/SCL: GPIO18 / GPIO36. LED backlight: 3.3 V through a resistor or GPIO control / 3.3 V through a resistor or GPIO control. A few details need attention here. The VSPI/FSPI mapping of the ESP32-S2 is not exactly the same as that of the ESP32, and the default SPI pins may differ between libraries; many libraries let you define them in the configuration, so the code is the final authority. Also, the ESP32-S2 does not have the traditional GPIO23/18 as default SPI pins, so I suggest GPIO35/36, which are more commonly available on ESP32-S2 boards. 8080 parallel wiring, taking a 2.4-inch ILI9341 parallel module as an example, with 16 data lines: LCD Pin | ESP32 Dev Board
Description: CS = GPIO5 (chip select); RST = GPIO4 (reset); DC/RS = GPIO2 (data/command select); WR = GPIO15 (write signal); RD = GPIO16 (read signal; if you never read from the screen, tie it high or control it with a GPIO); DB0-DB7 = GPIO12, 13, 14, 25, 26, 27, 17, 16 (low 8 data bits); DB8-DB15 = GPIO22, 21, 19, 18, 23, 33, 32, 35 (high 8 data bits); LED = 3.3 V through a resistor or GPIO for backlight. The most error-prone part of parallel wiring is that the high and low 8-bit groups must match the definitions in the library exactly. If one bit is reversed, the colors go wildly wrong, like a scrambled screen; in that case, first check that every data line corresponds one-to-one with the macro definitions in the code. Also, if your screen uses an 8-bit rather than a 16-bit parallel interface, you can omit the high 8 data lines, but you must change the driver library initialization to 8-bit mode. 2.3 The Two Most Overlooked Pitfalls: Power and Backlight. The first pitfall is the backlight. The LED pin on an LCD module is the backlight; if you drive the backlight of a large screen directly from 3.3 V and GND, the brightness may be insufficient or the current excessive. Many modules already have a backlight current-limiting resistor built in,
and connecting those directly to 3.3 V is fine; but some modules do not have one, and running at full current for a long time will make them hot and shorten their life. A safer approach is to connect an I/O pin through an NPN transistor or MOSFET to control the backlight, and use PWM to adjust the brightness — this also lets you implement automatic screen-off. The simplest PWM brightness control code is: pinMode(LED_PIN, OUTPUT); analogWrite(LED_PIN, 128); // 0-255, 128 is half brightness. If the module is too bright when connected directly to 3.3 V, or if you want software dimming, use an NPN transistor as a low-side switch: connect the collector to the negative side of the backlight LED, the emitter to ground, and the base through a 1k resistor to the PWM pin. This arrangement lets you use PWM for smooth brightness fading, which is far more practical than simply switching the backlight on and off. The second pitfall is bus voltage levels. Some display controllers support both 3.3 V and 5 V logic, but the I/O upper limit of many bare TFT panels is 3.3 V. The ESP32 itself uses 3.3 V I/O, so a direct connection is fine. But if your board runs at 5 V logic, such as an older Arduino, it is best to add level shifting, or long-term use may damage the display timing circuits. Do not underestimate this pitfall; many people start out
by using a 5 V board to light the screen up, only to find the display develops problems after a few days. 3. Driver Code Implementation: From Initialization to Displaying an Image. 3.1 Choosing a Driver Framework: TFT_eSPI, Arduino_GFX, or LovyanGFX. Once the hardware is wired, the next step is software. The three best-known LCD driver libraries in the Arduino ecosystem are TFT_eSPI, Arduino_GFX, and LovyanGFX. TFT_eSPI is an established library maintained by Bodmer and supports a large number of ICs such as the ST7735, ST7789, and ILI9341. Its characteristic is that you define pins and the screen model by editing the User_Setup.h configuration file instead of changing code, which suits beginners. The downside is that it is easy to forget to edit the file or to pick the wrong IC model. Arduino_GFX is maintained by moononournation; its API is close to Adafruit GFX, it supports many devices, and also supports parallel and RGB displays, making its configuration somewhat more flexible than TFT_eSPI. LovyanGFX focuses on performance and more advanced features such as DMA transfers, double buffering, and multi-display operation, and it scores very high in benchmarks on the ESP32.
The downside is a somewhat steeper learning curve and an API style that differs from traditional GFX libraries. My advice: if you are lighting up a screen for the first time, start with TFT_eSPI, because it has the most documentation and the most complete tutorials online. If you are building a slightly more complex interface, or need especially smooth animation, try LovyanGFX. If your project will later use LVGL, both LovyanGFX and TFT_eSPI have corresponding LVGL porting examples, so there is no need to worry about compatibility. 3.2 TFT_eSPI Configuration and SPI Wiring Verification. Take an ESP32 with a 2.4-inch ILI9341 over SPI as an example. First install the TFT_eSPI library, then find TFT_eSPI/User_Setup.h in the Arduino libraries folder and check the values of these macros: #define ILI9341_DRIVER, #define TFT_CS 5, #define TFT_DC 2, #define TFT_RST 4, #define TFT_MISO 19 (usually it can be left unconnected if you never read from the display), #define TFT_MOSI
23, #define TFT_SCLK 18. If your screen is write-only, most applications do not need to read MISO, so you can leave it unconnected; many display modules still have a MISO pin, which can simply be left floating. If a module is labeled both MOSI and SDA, usually SDA is MOSI. Once configured, you can verify with the official graphicstest example. Flash it, and if you see continuous color bars and scrolling lines, the wiring and initialization are fine; if you get a white or scrambled screen, go to the troubleshooting section later. One small tip: the User_Setup.h file of TFT_eSPI also contains multiple driver macros such as ST7789_DRIVER and ST7735_DRIVER. If you have an ST7789 panel but forget to change the driver macro, it will show wrong colors or a scrambled image. Conversely, change ILI9341_DRIVER to ST7789_DRIVER and also confirm the resolution setting, since the ST7789 comes in 240x240 and 240x320 variants. This situation of the same size with a different IC is the most common trap; asking the seller one more question, what the controller chip is, can save you a great deal of troubleshooting time.
3.3 Code Differences in 8080 Parallel Mode. If your screen is an 8080 parallel module, TFT_eSPI supports it too, but the configuration differs from SPI. In User_Setup.h you would write something like this: #define ILI9341_DRIVER, #define TFT_CS 5, #define TFT_DC 2, #define TFT_RST 4, #define TFT_WR 15, #define TFT_RD 16, #define TFT_D0 12, #define TFT_D1 13, #define TFT_D2 14, #define TFT_D3 25, #define TFT_D4 26, #define TFT_D5 27, #define TFT_D6 17, #define TFT_D7 16, #define TFT_D8 22, #define TFT_D9 21, #define TFT_D10 19, #define TFT_D11
18, #define TFT_D12 23, #define TFT_D13 33, #define TFT_D14 32, #define TFT_D15 35. Then TFT_eSPI will automatically enable 8-bit or 16-bit parallel mode, provided all these macros are defined in User_Setup.h. Note that parallel mode does not go through the SPI peripheral; GPIO pins are used to emulate the bus timing directly, so pin definitions in the code are critical. If a board uses the parallel interface and Wi-Fi at the same time, check whether the high-order pins conflict with PSRAM, Flash, and so on. On the ESP32, GPIO16/17 and similar pins are usually fine, but boards differ widely. One more point: in 8080 parallel mode, some libraries enable read-back by default, reading the display memory contents through the RD pin. If your wiring does not connect RD, or does not include the line for the read direction, initialization may hang. The solution is to tie the RD pin high or disable the relevant read-back in the configuration. 3.4 The Complete Flow for Displaying Chinese Text, Images, and Dynamic Data. First, displaying Chinese text. TFT_eSPI does not support Chinese characters by default.
You need a font library file to convert glyphs into pixel data. The simplest approach is to use the LVGL font tool or a tool such as img2lcd to generate the glyph data, and then draw it on the screen with drawBitmap. If you do not want to bother with glyphs, you can use the Adafruit GFX font mechanism, but Chinese font files are very large and not suitable for boards with small Flash. Here is a more practical solution: use the pushImage function of TFT_eSPI to display images, combined with an online image conversion tool that turns JPG/PNG into an RGB565 array. For example, a 64x64 icon becomes a uint16_t array after conversion, and you display it with tft.pushImage(x, y, 64, 64, image_data); For dynamic data, such as temperature, humidity, or time, call drawString or setCursor repeatedly inside the loop. Pay attention to how often you refresh the screen area: tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setCursor(10, 20); tft.
本文来源:http://www.lpqz.cn/news/80229
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。