An ESP32 board, a cheap SPI TFT display, and a few jumper wires are enough to try LVGL on real hardware. I used an ESP32-C6-DevKitC-1 and a 2.8 inch ILI9341 display. Nothing is pre-wired, so you'll see which pin does what. The project is built with ESP-IDF and LVGL 9. At the end you'll have your own widget on the screen.
Prerequisites#
- An ESP32-C6-DevKitC-1. Check that the J5 jumper is fitted. It's in series with the chip's power supply. Without it the power LED is on, but the chip doesn't start.
- A 2.8 inch SPI TFT module with an ILI9341 controller and 240x320 pixels. The common red modules have a touch panel too, but in this guide I wired only the display. Its XPT2046 controller uses SPI, and the generic BSP supports only I²C touch.
- Female-to-female jumper wires, because both the DevKit and the display have header pins.
- A Y-jumper or a small breadboard. VCC and LED of the display both go to 3V3, but the DevKit has only one 3V3 pin, so you need a Y wire. No soldering needed.
- A USB-C cable that carries data. With a charge-only cable the board doesn't show up on your PC. The DevKit has two USB-C ports. Use the one labeled UART.
You don't need a programmer, a soldering iron, or a power supply. The DevKit is programmed and powered through the same USB cable.
Hardware Setup#
Power the module from 3.3 V, not 5 V.
Many of the red 2.8 inch modules have a solder pad marked J1 on the back. When it's closed, the module's own regulator is bypassed, so it works from 3.3 V, but 5 V would go straight to the display. Don't kill your display with this simple mistake! On my module J1 was already closed, and the wiring below assumes that. If yours is open, bridge it with some solder before you power the module from 3V3.
| TFT pin | ESP32-C6 pin | What it does |
|---|---|---|
| VCC | 3V3 | Power, 3.3 V |
| GND | GND | Ground |
| CS | GPIO3 | Chip select |
| RESET | GPIO11 | Hardware reset |
| DC (or RS) | GPIO10 | Data/command select |
| SDI (MOSI) | GPIO7 | Data from the C6 to the display |
| SCK | GPIO6 | SPI clock |
| LED (or BLK) | 3V3 | Backlight, always on |
| SDO (MISO) | not connected | Data from the display, not used here |
The five touch pins (T_IRQ, T_DO, T_DIN, T_CS, T_CLK) are not connected.
I chose these pins on purpose. They avoid the C6's USB pins (GPIO12 and GPIO13) and its strapping pins (GPIO4, 5, 8, 9, and 15). SCK and MOSI are on GPIO6 and GPIO7, the native SPI pins of the C6.
If the display gets its power from somewhere else than the DevKit, still connect the two grounds. Without a common ground it looks like a broken display or a wrong driver.
Check the pin labels on your module before you power it on. These modules are sold by many vendors with the same chips but different pin orders, so follow the labels on your board.
Keep the wires short. Long jumper wires don't carry a fast SPI signal well.
Install ESP-IDF#
ESP-IDF is Espressif's SDK. It has the compiler, the build system, and the libraries for the chips.
Download the ESP-IDF Windows Installer and choose v6.1.1 when it asks for the version.
It installs everything you need: the compiler, Python, CMake, Ninja, and Git. If your connection is slow, use the offline installer.
After the installation you'll find two new entries in the Start menu,
ESP-IDF PowerShell and ESP-IDF Command Prompt. Open one of them and idf.py
works right away. Run all the commands of this guide from that window.
Using WSL? Then install ESP-IDF inside WSL the same way as on Linux, see the Linux / macOS tab.
It downloads a few GB, so it can take a while. When it's done, check that it works. This
should print v6.1.1:
idf.py --versionbashCreate the Project#
I started from lv_esp_idf, LVGL's ESP-IDF project:
git clone https://github.com/lvgl/lv_esp_idf.git
cd lv_esp_idfbashThis gives you master, which follows the latest LVGL. If you want a fixed release, there
are release/v9.5 and release/v9.6 branches too.
A board with its own BSP would be named here. For hand-wired hardware there is
the generic BSP. It doesn't know any board, so you set the pins yourself in menuconfig.
In main/idf_component.yml there is a bsp_replaceme placeholder. The Project Creator would
replace it for you, but here you do it by hand. Replace the line, don't add a new one next
to it:
description: LVGL Example
dependencies:
espressif/esp_bsp_generic: "*"yamlDon't use idf.py add-dependency here. It adds a new line and keeps bsp_replaceme, and then
the build fails because that component doesn't exist. Write the whole file instead.
This one dependency pulls in the display drivers and esp_lvgl_port, which connects those
drivers to LVGL. LVGL itself comes with it too, so you don't need to install it separately.
If you later move to a board with its own BSP, you change one line in
idf_component.yml and skip the menuconfig step. Your LVGL code stays the same.
The generic BSP supports ST7789, ILI9341, and GC9A01 displays over SPI, and TT21100, GT1151, GT911, CST816S, and FT5X06 touch controllers over I²C. If your display uses one of these, you don't need to write any driver code, just the settings below.
Configure the Pins#
idf.py set-target esp32c6
idf.py menuconfigbashset-target prints some "unknown kconfig symbol" warnings, about SPIRAM,
ESP_DEFAULT_CPU_FREQ_MHZ_240 and a few more. The project's defaults are shared with boards
that have PSRAM and a faster CPU. The C6 has neither, so ESP-IDF skips these lines. You can
ignore them.
The generic BSP's settings are here:
Build type ---> Bootloader config ---> Security features ---> Application manager ---> Serial flasher config ---> Partition Table ---> Compiler options ---> Component config ---> Board Support Package (generic) ---> Display --->Turn on Enable display first. It's off by default, and until you turn it on, the other
display settings are hidden. So an empty Display menu is normal.
Now the menu shows the display settings. The pins are under Connection and the mirror
settings under Rotation. Here is the whole menu with both submenus opened and the values
for my wiring. I tested the guide with these settings.
Display ---> [*] Enable display Connection ---> Communication interface (SPI) ---> (6) Display SPI SCLK GPIO (7) Display SPI MOSI GPIO (-1) Display SPI MISO GPIO (3) Display SPI CS GPIO (10) Display SPI DC GPIO (11) Display reset GPIO (-1) Display backlight GPIO Display driver (ILI9341) ---> Rotation ---> [ ] Swap XY [*] Mirror X [ ] Mirror Y Color space (BGR) ---> [ ] Invert colors [ ] Invert brightness level (20) Display pixel clock (MHz) (240) Display width (320) Display height (0) Display horizontal offset (0) Display vertical offset (100) LCD framebuf height [ ] LCD double framebufYou need to change eleven settings:
- the SCLK, MOSI, MISO, CS, DC, and reset pins under
Connection Display driver, set to ILI9341Mirror XunderRotationColor space, set to BGRDisplay widthandDisplay height, set to 240 and 320
The rest can stay as it is.
-1 means not connected. MISO is not connected because nothing is read back from the
display. The backlight is -1 because LED is wired to 3V3, so it's always on.
The clock is in MHz, so it's 20, not 20000000. 20 MHz is the default and it works
well with jumper wires. When the picture is stable you can increase it step by step and keep
the last value that still gives a clean picture.
Width and height are the size of the display the way you use it. The default 320x240 is landscape, but the ILI9341 is 240x320 portrait natively. Portrait is the easiest to start with, because what LVGL draws maps straight to the display without swapping the axes.
If the picture is mirrored or upside down, toggle Mirror X and Mirror Y. If red looks
blue, change Color space. My module needed Mirror X and BGR.
For landscape, set width and height to 320 and 240 and turn on Swap XY. With swapped axes
you'll probably need different mirror flags, so toggle Mirror X and Mirror Y again until
the picture is right.
Invert colors makes the picture look like a negative, my module needs it off.
LCD framebuf height is how many rows LVGL draws at once. The C6 has no PSRAM, so the buffer
is in internal RAM, and the default 100 is a good balance between RAM and speed.
Press S to save and Esc twice to exit.
Build and Flash#
idf.py -p COM3 flash monitorpowershellReplace COM3 with your port. You can find it in Device Manager under
Ports (COM & LPT), or list the ports with:
modepowershellUsing WSL? WSL can't see the COM ports, so you need usbipd-win to pass the board to WSL. Install it once in Windows:
winget install usbipdpowershellPlug in the board and find its bus ID. Look for the USB to UART bridge, usually CP210x or CH340:
usbipd listpowershellShare it once, from a PowerShell opened as administrator. Replace 2-1 with your bus
ID:
usbipd bind --busid 2-1powershellThen attach it to WSL. This doesn't need administrator rights, but you have to do it every time you plug in the board or restart Windows:
usbipd attach --wsl --busid 2-1powershellIn WSL the board is now a normal Linux serial port, so follow the Linux tab, including
the dialout group:
ls /dev/ttyUSB*
idf.py -p /dev/ttyUSB0 flash monitorbashIf attach says "Device busy", a Windows program still uses the port. Close PuTTY, the Arduino serial monitor, or anything else that has it open, and try again.
To exit the monitor press Ctrl+]. Ctrl+C doesn't work here, it's sent to the board.
After flashing, the board restarts and the bundled music player demo starts on the display. These are the important lines of the boot log:
I (23) boot: ESP-IDF v6.1.1 2nd stage bootloader
...
I (336) cpu_start: Unicore app
I (345) cpu_start: cpu freq: 160000000 Hz
...
I (408) spi_flash: detected chip: gd
I (411) spi_flash: flash io: qio
W (413) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
...
I (457) main_task: Calling app_main()
I (458) LVGL: Starting LVGL task
I (459) ili9341: LCD panel create success, version: 2.1.0
I (459) BSP-Gen: Initialize LCD: ILI9341
I (652) app_main: Display LVGL demo
I (891) main_task: Returned from app_main()Hello World#
The demo shows that the wiring works. Now let's replace it with your own code: three color
bands and a label. Open main/display_lvgl_demos_main.c and
replace its content with this:
#include "bsp/esp-bsp.h"
#include "lvgl.h"
static void add_band(lv_color_t color, const char *name, int32_t y)
{
lv_obj_t *band = lv_obj_create(lv_screen_active());
lv_obj_set_size(band, lv_pct(100), 60);
lv_obj_set_y(band, y);
lv_obj_set_style_bg_color(band, color, 0);
lv_obj_set_style_radius(band, 0, 0);
lv_obj_set_style_border_width(band, 0, 0);
lv_obj_t *label = lv_label_create(band);
lv_label_set_text(label, name);
lv_obj_center(label);
}
void app_main(void)
{
bsp_display_start();
bsp_display_backlight_on();
bsp_display_lock(0);
add_band(lv_color_hex(0xFF0000), "RED", 20);
add_band(lv_color_hex(0x00FF00), "GREEN", 90);
add_band(lv_color_hex(0x0000FF), "BLUE", 160);
lv_obj_t *label = lv_label_create(lv_screen_active());
lv_label_set_text(label, "Hello LVGL 9 on ESP32-C6");
lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -40);
bsp_display_unlock();
}cThen build and flash again:
idf.py flash monitorbashThe bands are also a quick color check. If the RED band shows up blue, change Color space
in menuconfig. If the text is mirrored, toggle Mirror X. The FPS counter in the corner is
LVGL's performance monitor, lv_esp_idf turns it on by default.
Keep every LVGL call you make outside the LVGL task between bsp_display_lock() and
bsp_display_unlock(). They take and release a mutex that esp_lvgl_port uses to stop
LVGL's task from redrawing while you change the UI. LVGL's API is not thread-safe, so without
the lock it can work for a while, then freeze or corrupt the screen minutes or hours later.
That's hard to debug.
Troubleshooting#
With hand-wired hardware it's usually the wiring. Check this table before you suspect LVGL.
| Symptom | Usual cause | What to check |
|---|---|---|
| White or noisy screen | Wrong driver, or SPI too fast | Set the clock to 10 MHz and try again |
| Garbage that changes when you touch the wires | Bad signal | Lower the clock, shorten the wires, check the ground |
| Red and blue swapped | RGB vs BGR order | Change Color space under Display. My ILI9341 module needs BGR |
| Picture mirrored or upside down | Mirror flags don't match the module | Toggle Mirror X or Mirror Y under Rotation, not the LVGL rotation. My module needs Mirror X in portrait |
| Picture sideways or squashed | Width, height, and Swap XY don't match | Portrait: 240x320, Swap XY off. Landscape: 320x240, Swap XY on |
lv_scr_act undefined | LVGL 8 code in an LVGL 9 project | Use lv_screen_active() |
Failed to connect ... No serial data received | The chip didn't enter the bootloader | Hold BOOT, press RESET, release BOOT, then flash again |
| Power LED on, but no output on any port | The chip has no power | Fit the J5 jumper |
Device busy when attaching to WSL | A Windows program uses the port | Close PuTTY or any serial monitor, then attach again |
If nothing helps, fill the display with one color using esp_lcd directly. If that doesn't
work either, the problem is the display or the wiring.
References#
- LVGL's widget documentation shows what you can put on the screen
esp_lvgl_porthas buffering options, rotation, and power saving- esp-bsp if you later move to a board with its own BSP
