{"id":155074,"date":"2024-05-30T13:22:12","date_gmt":"2024-05-30T13:22:12","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=155074"},"modified":"2024-09-03T10:46:54","modified_gmt":"2024-09-03T10:46:54","slug":"lvgl-esp32-tft-touchscreen-display-ili9341-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/lvgl-esp32-tft-touchscreen-display-ili9341-arduino\/","title":{"rendered":"LVGL with ESP32 TFT LCD Touchscreen Display \u2013 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll get started with the LVGL (Light and Versatile Graphics Library) using a TFT LCD Touchscreen Display wired to an ESP32 board. The LVGL is a popular free and open-source embedded graphics library to create awesome UIs for many microcontrollers and displays.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT LCD Touchscreen Display 2.8 inch ILI9341 240px by 320px Arduino IDE LVGL Graphics Library\" class=\"wp-image-155070\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\">We recommend an ESP32 board that has an on-board TFT display, read our <a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\">Guide for LVGL with ESP32 Cheap Yellow Display Board (ESP32-2432S028R)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the TFT LCD Touchscreen Display<\/h2>\n\n\n\n<p>The display we&#8217;re using in this guide is the 2.8. inch TFT LCD that also comes with a touchscreen. The display communicates via SPI communication protocol and uses the ILI9341 driver. You can write text, draw shapes, and display images. The touchscreen also uses the SPI communication protocol.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-display.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-150003\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-display.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-display.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The TFT LCD touchscreen also comes with an SD card interface if you need to load files for your specific project. This display is also available with different screen sizes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>For this project, you need to wire the TFT display and touchscreen pins to the ESP32. Here\u2019s a list of parts you need:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Display-Screen-Wiring.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring TFT LCD Touchscreen display to ESP32\" class=\"wp-image-150007\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Display-Screen-Wiring.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Display-Screen-Wiring.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/2-8-inch-ili9341-tft-240x320\/\" target=\"_blank\" rel=\"noopener\" title=\"\">TFT LCD Touchscreen Display &#8211; 2.8 inch ILI9341 240\u00d7320<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DOIT DEVKIT V1 Board<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Development Boards Review and Comparison<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wiring the TFT LCD Touchscreen Display to the ESP32 Board<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-pins.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring the TFT LCD Touchscreen Display to the ESP32 Board\" class=\"wp-image-150010\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-pins.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-Touchscreen-pins.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Wire the TFT LCD and touchscreen pins to the following ESP32 GPIOs (you must use these exact pins, otherwise the project will not work).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>TFT LCD Touchscreen<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td>T_IRQ<\/td><td>GPIO 36<\/td><\/tr><tr><td>T_OUT<\/td><td>GPIO 39<\/td><\/tr><tr><td>T_DIN<\/td><td>GPIO 32<\/td><\/tr><tr><td>T_CS<\/td><td>GPIO 33<\/td><\/tr><tr><td>T_CLK<\/td><td>GPIO 25<\/td><\/tr><tr><td>SDO(MISO)<\/td><td>GPIO 12<\/td><\/tr><tr><td>LED<\/td><td>GPIO 21<\/td><\/tr><tr><td>SCK<\/td><td>GPIO 14<\/td><\/tr><tr><td>SDI(MOSI)<\/td><td>GPIO 13<\/td><\/tr><tr><td>D\/C<\/td><td>GPIO 2<\/td><\/tr><tr><td>RESET<\/td><td>EN\/RESET<\/td><\/tr><tr><td>CS<\/td><td>GPIO 15<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>VCC<\/td><td>5V (or 3.3V)*<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>* In the VCC pin, you can either use 5V or 3.3V depending if your <strong>J1<\/strong> connection is open or closed (by default it&#8217;s usually open as you can see in the figure below).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>VCC = 5V | J1=OPEN\nVCC = 3.3V | J1=CLOSE<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-touchscreen-J1-connection.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TFT LCD Touchscreen display J1 connection\" class=\"wp-image-150009\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-touchscreen-J1-connection.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-LCD-touchscreen-J1-connection.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Introducing LVGL (Light and Versatile Graphics Library)<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.lvgl.io\/master\/\" target=\"_blank\" rel=\"noopener\" title=\"\">LVGL<\/a> (Light and Versatile Graphics Library) is a free and open-source graphics library that provides a wide range of easy-to-use graphical elements for your microcontroller projects that require a graphical user interface (GUI).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"95\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/logo_lvgl.png?resize=300%2C95&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Logo LVGL Light and Versatile Graphics Library\" class=\"wp-image-155035\"\/><\/figure><\/div>\n\n\n<p><strong>Here are some of its key features:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Blocks: buttons, charts, lists, sliders, images, etc&#8230;<\/li>\n\n\n\n<li>Advanced graphics with animations, anti-aliasing, opacity, smooth scrolling;<\/li>\n\n\n\n<li>Various input devices such as touchpad, mouse, keyboard, encoder, etc&#8230;<\/li>\n\n\n\n<li>Multi-language support with UTF-8 encoding;<\/li>\n\n\n\n<li>Multi-display support, i.e. use multiple TFT, monochrome displays simultaneously;<\/li>\n\n\n\n<li>Fully customizable graphic elements with CSS-like styles;<\/li>\n\n\n\n<li>Hardware independent: use with any microcontroller or display;<\/li>\n\n\n\n<li>Scalable: able to operate with little memory (64 kB Flash, 16 kB RAM);<\/li>\n\n\n\n<li>Written in C for maximal compatibility (C++ compatible) and binding to MicroPython.<\/li>\n<\/ul>\n\n\n\n<p>It also has a wide range of code <a href=\"https:\/\/docs.lvgl.io\/master\/examples.html\" target=\"_blank\" rel=\"noopener\" title=\"\">examples in their documentation<\/a> that you can use: text, buttons, sliders, input fields, keyboard, custom styling, images, arcs, lines, animations, menus, tabs, layouts, tables, and much more&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/LVGL-Library-examples.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"LVGL Library Examples\" class=\"wp-image-155038\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/LVGL-Library-examples.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/LVGL-Library-examples.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-libraries\">Installing Arduino Libraries<\/h2>\n\n\n\n<p>The ESP32 communicates with the TFT Display and Touchscreen using SPI communication protocol. We&#8217;ll be using the <strong>TFT_eSPI<\/strong>, <strong>XPT2046_Touchscreen<\/strong>, and <strong>LVGL 9<\/strong> libraries.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installing the TFT_eSPI Library<\/h4>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open. Search for&nbsp;<strong>TFT_eSPI<\/strong>. Select the <em>TFT_eSPI library by Bodme<\/em>r and install it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"306\" height=\"292\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-TFT_eSPI-library-Bodmer-Arduino-IDE-2.png?resize=306%2C292&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing TFT_eSPI library Bodmer Arduino IDE 2\" class=\"wp-image-149501\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-TFT_eSPI-library-Bodmer-Arduino-IDE-2.png?w=306&amp;quality=100&amp;strip=all&amp;ssl=1 306w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-TFT_eSPI-library-Bodmer-Arduino-IDE-2.png?resize=300%2C286&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Installing the XPT2046_Touchscreen Library<\/h4>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open. Search for&nbsp;<strong>XPT2046_Touchscreen<\/strong>. Select the <em>XPT2046_Touchscreen library by Paul Stoffregen<\/em> and install it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"322\" height=\"275\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-XPT2046_Touchscreen-Library-by-Paul-Stoffregen-Arduino-IDE-2.png?resize=322%2C275&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing XPT2046_Touchscreen Library by Paul Stoffregen Arduino IDE 2\" class=\"wp-image-149502\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-XPT2046_Touchscreen-Library-by-Paul-Stoffregen-Arduino-IDE-2.png?w=322&amp;quality=100&amp;strip=all&amp;ssl=1 322w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Installing-XPT2046_Touchscreen-Library-by-Paul-Stoffregen-Arduino-IDE-2.png?resize=300%2C256&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Installing the LVGL 9 Library<\/h4>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open. Search for&nbsp;<strong>LVGL<\/strong>. Select the <em>LVGL library by kiskegabor<\/em> and install <strong>version 9.2<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"261\" height=\"297\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/09\/Installing-LVGL-version-9_2-Arduino-IDE-software.png?resize=261%2C297&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing LVGL version 9.2 Arduino IDE software\" class=\"wp-image-162110\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Prepare Config Files for TFT_eSPI and LVGL Library<\/h2>\n\n\n\n<p>To properly use the <strong>TFT_eSPI<\/strong> library, you need a configuration file called <span class=\"rnthl rntliteral\">User_Setup.h<\/span> with the right definitions. You also need to prepare the <span class=\"rnthl rntliteral\">lv_conf.h<\/span> file for the LVGL library. We&#8217;ve already prepared these two files so that you don&#8217;t have any configuration issues following our examples. You just need to download them and move them to the correct folders. Follow the next instructions to learn how to do it.<\/p>\n\n\n\n<p><a href=\"#config-file-windows-pc\" title=\"\">a) Preparing the Config Files &#8211; Windows PC<\/a><\/p>\n\n\n\n<p><a href=\"#config-file-mac-os\" title=\"\">b) Preparing the Config Files &#8211; Mac OS<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"config-file-windows-pc\">a) Preparing the Config Files &#8211; Windows PC<\/h3>\n\n\n\n<p>Having all the libraries installed (<span class=\"rnthl rntliteral\">TFT_eSPI<\/span>, <span class=\"rnthl rntliteral\">XPT2046_Touchscreen<\/span>, and <span class=\"rnthl rntliteral\">LVGL<\/span>), start by downloading the <em><strong>User_Setup.h<\/strong> <\/em>configuration file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/configs\/User_Setup.zip\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download .zip folder<\/a> with the <strong><em>User_Setup.h<\/em><\/strong> config file (<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/main\/configs\/User_Setup.h\" target=\"_blank\" rel=\"noopener\" title=\"\">view raw file<\/a>)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"168\" height=\"237\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/User_Setup-file-for-TFT_eSPI-library-Bodmer-Arduino-IDE-2.png?resize=168%2C237&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"User_Setup.h file for TFT_eSPI library Bodmer Arduino IDE 2\" class=\"wp-image-149506\"\/><\/figure><\/div>\n\n\n<p>In your Arduino IDE, go to <strong>File <\/strong>and open the <strong>Preferences<\/strong> menu.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"353\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Windows-PC.png?resize=199%2C353&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open Arduino IDE 2 Preferences Menu Windows PC\" class=\"wp-image-149504\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Windows-PC.png?w=199&amp;quality=100&amp;strip=all&amp;ssl=1 199w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Windows-PC.png?resize=169%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 169w\" sizes=\"(max-width: 199px) 100vw, 199px\" \/><\/figure><\/div>\n\n\n<p>Copy the Arduino IDE &#8220;<strong>Sketchbook location<\/strong>&#8221; path. In my case, it&#8217;s:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Users\\rui_s\\Documents\\Arduino<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"485\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse.png?resize=783%2C485&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open Sketchbook Location Arduino IDE 2 Browse\" class=\"wp-image-149503\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse.png?w=783&amp;quality=100&amp;strip=all&amp;ssl=1 783w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse.png?resize=300%2C186&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse.png?resize=768%2C476&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 783px) 100vw, 783px\" \/><\/figure><\/div>\n\n\n<p>Then, in your Windows PC File Explorer tab enter the sketchbook location path to open the Arduino folder (it&#8217;s usually under the <em>Documents <\/em>folder).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"91\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Browse-to-Arduino-libraries-folder-to-copy-lv_conf-file-Arduino-IDE-2.png?resize=448%2C91&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Browse to Arduino libraries folder to copy lv_conf.h file Arduino IDE 2\" class=\"wp-image-149508\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Browse-to-Arduino-libraries-folder-to-copy-lv_conf-file-Arduino-IDE-2.png?w=448&amp;quality=100&amp;strip=all&amp;ssl=1 448w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Browse-to-Arduino-libraries-folder-to-copy-lv_conf-file-Arduino-IDE-2.png?resize=300%2C61&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 448px) 100vw, 448px\" \/><\/figure><\/div>\n\n\n<p>Open the <em>libraries <\/em>folder:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"357\" height=\"69\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Windows-PC.png?resize=357%2C69&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open the Arduino IDE 2 Libraries Folder Windows PC\" class=\"wp-image-149507\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Windows-PC.png?w=357&amp;quality=100&amp;strip=all&amp;ssl=1 357w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Windows-PC.png?resize=300%2C58&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 357px) 100vw, 357px\" \/><\/figure><\/div>\n\n\n<p>You should see the <em>TFT_eSPI <\/em>library folder there. Open it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"556\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-TFT_eSPI-folder-libraries-Arduino-IDE-2.png?resize=750%2C556&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open TFT_eSPI folder libraries Arduino IDE 2\" class=\"wp-image-149514\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-TFT_eSPI-folder-libraries-Arduino-IDE-2.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-TFT_eSPI-folder-libraries-Arduino-IDE-2.png?resize=300%2C222&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You should be in a similar folder path as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Users\\rui_s\\Documents\\Arduino\\libraries\\TFT_eSPI<\/code><\/pre>\n\n\n\n<p>Copy the <em>User_Setup.h<\/em> file provided earlier and <strong>replace<\/strong> the existing file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"920\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2.png?resize=948%2C920&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Move copy User_Setup.h file to TFT_eSPI library folder Arduino IDE 2\" class=\"wp-image-149509\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2.png?w=948&amp;quality=100&amp;strip=all&amp;ssl=1 948w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2.png?resize=300%2C291&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2.png?resize=768%2C745&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><\/figure><\/div>\n\n\n<p>Then, download the <em><strong>lv_conf.h<\/strong> <\/em>configuration file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/configs\/lv_conf.zip\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download .zip folder<\/a> with the <strong><em>lv_conf.h<\/em><\/strong> config file (<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/main\/configs\/lv_conf.h\" target=\"_blank\" rel=\"noopener\" title=\"\">view raw file<\/a>)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"171\" height=\"230\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/lv_config-file-for-LVGL-library-by-Kisvegabor-Arduino-IDE-2.png?resize=171%2C230&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"lv_config.h file for LVGL library by Kisvegabor Arduino IDE 2\" class=\"wp-image-149505\"\/><\/figure><\/div>\n\n\n<p>Open a similar folder path in your computer as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Users\\rui_s\\Documents\\Arduino\\libraries<\/code><\/pre>\n\n\n\n<p>Move the <em><strong>lv_conf.h<\/strong> <\/em>to the <em>libraries <\/em>folder (<strong>do NOT move it inside the lvgl folder<\/strong>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"725\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2.png?resize=942%2C725&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Move copy lv_conf.h configuration file to Arduino libraries folder Arduino IDE 2\" class=\"wp-image-149511\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2.png?w=942&amp;quality=100&amp;strip=all&amp;ssl=1 942w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2.png?resize=300%2C231&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2.png?resize=768%2C591&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 942px) 100vw, 942px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcred\"><strong>IMPORTANT:<\/strong> other <em>User_Setup.h<\/em> and <em>lv_conf.h<\/em> files available on the internet will probably NOT work with the examples available at Random Nerd Tutorials. You must use the exact files provided in this article.<\/p>\n\n\n\n<p class=\"rntbox rntcorange\"><strong>Note:<\/strong> if you update your libraries, you&#8217;ll need to do this procedure again and place the right configuration files in the right places.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"config-file-mac-os\">b) Preparing the Config Files &#8211; Mac OS<\/h3>\n\n\n\n<p>Having both libraries installed (<span class=\"rnthl rntliteral\">TFT_eSPI<\/span> and <span class=\"rnthl rntliteral\">XPT2046_Touchscreen<\/span>), download the <strong>User_Setup.h<\/strong> configuration file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/configs\/User_Setup.zip\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download .zip folder<\/a> with the <strong><em>User_Setup.h<\/em><\/strong> config file (<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/main\/configs\/User_Setup.h\" target=\"_blank\" rel=\"noopener\" title=\"\">view raw file<\/a>)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"221\" height=\"45\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Download-User_Setup-file-for-TFT_eSPI-library-Bodmer-Arduino-IDE-2.png?resize=221%2C45&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Download User_Setup.h file for TFT_eSPI library Bodmer Arduino IDE 2\" class=\"wp-image-149519\"\/><\/figure><\/div>\n\n\n<p>In your Arduino IDE, open the <strong>Settings <\/strong>menu.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"398\" height=\"446\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Mac-OS.png?resize=398%2C446&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open Arduino IDE 2 Preferences Menu Mac OS\" class=\"wp-image-149524\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Mac-OS.png?w=398&amp;quality=100&amp;strip=all&amp;ssl=1 398w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Arduino-IDE-2-Preferences-Menu-Mac-OS.png?resize=268%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 268w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/figure><\/div>\n\n\n<p>Copy the Arduino IDE &#8220;<strong>Sketchbook location<\/strong>&#8221; path. In my case, it&#8217;s:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/Users\/rui\/Documents\/Arduino<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"461\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse-Mac-OS.png?resize=750%2C461&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open Sketchbook Location Arduino IDE 2 Browse Mac OS\" class=\"wp-image-149525\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse-Mac-OS.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-Sketchbook-Location-Arduino-IDE-2-Browse-Mac-OS.png?resize=300%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>In Finder, type <em>\/Users\/rui\/Documents\/Arduino<\/em> and open that directory.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"47\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Folder-Mac-OS.png?resize=350%2C47&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open the Arduino IDE 2 Folder Mac OS\" class=\"wp-image-149527\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Folder-Mac-OS.png?w=350&amp;quality=100&amp;strip=all&amp;ssl=1 350w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Folder-Mac-OS.png?resize=300%2C40&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/figure><\/div>\n\n\n<p>Open the <em>libraries <\/em>folder.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"247\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=900%2C247&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open the Arduino IDE 2 Libraries Folder Mac OS\" class=\"wp-image-149528\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=300%2C82&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=768%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure><\/div>\n\n\n<p>You should see the <em>TFT_eSPI <\/em>library folder there. Open it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"217\" height=\"40\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-TFT_eSPI-folder-libraries-Arduino-IDE-2-Mac-OS.png?resize=217%2C40&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open TFT_eSPI folder libraries Arduino IDE 2 Mac OS\" class=\"wp-image-149526\"\/><\/figure><\/div>\n\n\n<p>You should be in a similar folder path as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/Users\/rui\/Documents\/Arduino\/libraries\/TFT_eSPI<\/code><\/pre>\n\n\n\n<p>Copy the <em>User_Setup.h<\/em> file provided earlier and <strong>replace<\/strong> the existing file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"395\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?resize=750%2C395&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Move copy User_Setup.h file to TFT_eSPI library folder Arduino IDE 2 Mac OS\" class=\"wp-image-149521\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?resize=300%2C158&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You should now have the <em>User_Setup.h<\/em> file provided on that path.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"378\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?resize=750%2C378&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Move User_Setup.h file to TFT_eSPI library folder Arduino IDE 2 Mac OS\" class=\"wp-image-149523\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-User_Setup-file-to-TFT_eSPI-library-folder-Arduino-IDE-2-Mac-OS.png?resize=300%2C151&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, download the <em><strong>lv_conf.h<\/strong> <\/em>configuration file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/configs\/lv_conf.zip\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download .zip folder<\/a> with the <strong><em>lv_conf.h<\/em><\/strong> config file (<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/main\/configs\/lv_conf.h\" target=\"_blank\" rel=\"noopener\" title=\"\">view raw file<\/a>)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"164\" height=\"47\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Download-lv_config-file-for-LVGL-library-by-Kisvegabor-Arduino-IDE-2.png?resize=164%2C47&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Download lv_config.h file for LVGL library by Kisvegabor Arduino IDE 2\" class=\"wp-image-149518\"\/><\/figure><\/div>\n\n\n<p>Open a similar folder path for the libraries in your computer as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/Users\/rui\/Documents\/Arduino\/libraries<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"247\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=900%2C247&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Open the Arduino IDE 2 Libraries Folder Mac OS\" class=\"wp-image-149528\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=300%2C82&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Open-the-Arduino-IDE-2-Libraries-Folder-Mac-OS.png?resize=768%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure><\/div>\n\n\n<p>Move the <em><strong>lv_conf.h<\/strong> <\/em>to the <em>libraries <\/em>folder (<strong>do NOT move it inside the lvgl folder<\/strong>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"219\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2-Mac-OS.png?resize=900%2C219&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Move copy lv_conf.h configuration file to Arduino libraries folder Arduino IDE 2 Mac OS\" class=\"wp-image-149520\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2-Mac-OS.png?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2-Mac-OS.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Move-copy-lv_conf-configuration-file-to-Arduino-libraries-folder-Arduino-IDE-2-Mac-OS.png?resize=768%2C187&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcred\"><strong>IMPORTANT:<\/strong> other <em>User_Setup.h<\/em> and <em>lv_conf.h<\/em> files available on the internet will probably NOT work with the examples available at Random Nerd Tutorials. You must use the exact files provided in this article.<\/p>\n\n\n\n<p class=\"rntbox rntcorange\"><strong>Note:<\/strong> if you update your libraries, you&#8217;ll need to do this procedure again and place the right configuration files in the right places.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code &#8211; Display Text, Create Buttons and Slider<\/h2>\n\n\n\n<p>The following code displays a simple text in your TFT display and allows you to test the touchscreen using a button and slider. When you press the buttons or slider, it should trigger some events.<\/p>\n\n\n\n<p>Copy the following code to the Arduino IDE and upload it to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n    THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:\n    1) ESP32-2432S028R 2.8 inch 240\u00d7320 also known as the Cheap Yellow Display (CYD): https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\n      SET UP INSTRUCTIONS: https:\/\/RandomNerdTutorials.com\/cyd-lvgl\/\n    2) REGULAR ESP32 Dev Board + 2.8 inch 240x320 TFT Display: https:\/\/makeradvisor.com\/tools\/2-8-inch-ili9341-tft-240x320\/ and https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\n      SET UP INSTRUCTIONS: https:\/\/RandomNerdTutorials.com\/esp32-tft-lvgl\/\n    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n\/*  Install the &quot;lvgl&quot; library version 9.2 by kisvegabor to interface with the TFT Display - https:\/\/lvgl.io\/\n    *** IMPORTANT: lv_conf.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***\n    *** YOU MUST USE THE lv_conf.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***\n    FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https:\/\/RandomNerdTutorials.com\/cyd-lvgl\/ or https:\/\/RandomNerdTutorials.com\/esp32-tft-lvgl\/   *\/\n#include &lt;lvgl.h&gt;\n\n\/*  Install the &quot;TFT_eSPI&quot; library by Bodmer to interface with the TFT Display - https:\/\/github.com\/Bodmer\/TFT_eSPI\n    *** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***\n    *** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***\n    FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https:\/\/RandomNerdTutorials.com\/cyd-lvgl\/ or https:\/\/RandomNerdTutorials.com\/esp32-tft-lvgl\/   *\/\n#include &lt;TFT_eSPI.h&gt;\n\n\/\/ Install the &quot;XPT2046_Touchscreen&quot; library by Paul Stoffregen to use the Touchscreen - https:\/\/github.com\/PaulStoffregen\/XPT2046_Touchscreen - Note: this library doesn't require further configuration\n#include &lt;XPT2046_Touchscreen.h&gt;\n\n\/\/ Touchscreen pins\n#define XPT2046_IRQ 36   \/\/ T_IRQ\n#define XPT2046_MOSI 32  \/\/ T_DIN\n#define XPT2046_MISO 39  \/\/ T_OUT\n#define XPT2046_CLK 25   \/\/ T_CLK\n#define XPT2046_CS 33    \/\/ T_CS\n\nSPIClass touchscreenSPI = SPIClass(VSPI);\nXPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);\n\n#define SCREEN_WIDTH 240\n#define SCREEN_HEIGHT 320\n\n\/\/ Touchscreen coordinates: (x, y) and pressure (z)\nint x, y, z;\n\n#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT \/ 10 * (LV_COLOR_DEPTH \/ 8))\nuint32_t draw_buf[DRAW_BUF_SIZE \/ 4];\n\n\/\/ If logging is enabled, it will inform the user about what is happening in the library\nvoid log_print(lv_log_level_t level, const char * buf) {\n  LV_UNUSED(level);\n  Serial.println(buf);\n  Serial.flush();\n}\n\n\/\/ Get the Touchscreen data\nvoid touchscreen_read(lv_indev_t * indev, lv_indev_data_t * data) {\n  \/\/ Checks if Touchscreen was touched, and prints X, Y and Pressure (Z)\n  if(touchscreen.tirqTouched() &amp;&amp; touchscreen.touched()) {\n    \/\/ Get Touchscreen points\n    TS_Point p = touchscreen.getPoint();\n    \/\/ Calibrate Touchscreen points with map function to the correct width and height\n    x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);\n    y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);\n    z = p.z;\n\n    data-&gt;state = LV_INDEV_STATE_PRESSED;\n\n    \/\/ Set the coordinates\n    data-&gt;point.x = x;\n    data-&gt;point.y = y;\n\n    \/\/ Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor\n    \/* Serial.print(&quot;X = &quot;);\n    Serial.print(x);\n    Serial.print(&quot; | Y = &quot;);\n    Serial.print(y);\n    Serial.print(&quot; | Pressure = &quot;);\n    Serial.print(z);\n    Serial.println();*\/\n  }\n  else {\n    data-&gt;state = LV_INDEV_STATE_RELEASED;\n  }\n}\n\nint btn1_count = 0;\n\/\/ Callback that is triggered when btn1 is clicked\nstatic void event_handler_btn1(lv_event_t * e) {\n  lv_event_code_t code = lv_event_get_code(e);\n  if(code == LV_EVENT_CLICKED) {\n    btn1_count++;\n    LV_LOG_USER(&quot;Button clicked %d&quot;, (int)btn1_count);\n  }\n}\n\n\/\/ Callback that is triggered when btn2 is clicked\/toggled\nstatic void event_handler_btn2(lv_event_t * e) {\n  lv_event_code_t code = lv_event_get_code(e);\n  lv_obj_t * obj = (lv_obj_t*) lv_event_get_target(e);\n  if(code == LV_EVENT_VALUE_CHANGED) {\n    LV_UNUSED(obj);\n    LV_LOG_USER(&quot;Toggled %s&quot;, lv_obj_has_state(obj, LV_STATE_CHECKED) ? &quot;on&quot; : &quot;off&quot;);\n  }\n}\n\nstatic lv_obj_t * slider_label;\n\/\/ Callback that prints the current slider value on the TFT display and Serial Monitor for debugging purposes\nstatic void slider_event_callback(lv_event_t * e) {\n  lv_obj_t * slider = (lv_obj_t*) lv_event_get_target(e);\n  char buf[8];\n  lv_snprintf(buf, sizeof(buf), &quot;%d%%&quot;, (int)lv_slider_get_value(slider));\n  lv_label_set_text(slider_label, buf);\n  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);\n  LV_LOG_USER(&quot;Slider changed to %d%%&quot;, (int)lv_slider_get_value(slider));\n}\n\nvoid lv_create_main_gui(void) {\n  \/\/ Create a text label aligned center on top (&quot;Hello, world!&quot;)\n  lv_obj_t * text_label = lv_label_create(lv_screen_active());\n  lv_label_set_long_mode(text_label, LV_LABEL_LONG_WRAP);    \/\/ Breaks the long lines\n  lv_label_set_text(text_label, &quot;Hello, world!&quot;);\n  lv_obj_set_width(text_label, 150);    \/\/ Set smaller width to make the lines wrap\n  lv_obj_set_style_text_align(text_label, LV_TEXT_ALIGN_CENTER, 0);\n  lv_obj_align(text_label, LV_ALIGN_CENTER, 0, -90);\n\n  lv_obj_t * btn_label;\n  \/\/ Create a Button (btn1)\n  lv_obj_t * btn1 = lv_button_create(lv_screen_active());\n  lv_obj_add_event_cb(btn1, event_handler_btn1, LV_EVENT_ALL, NULL);\n  lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -50);\n  lv_obj_remove_flag(btn1, LV_OBJ_FLAG_PRESS_LOCK);\n\n  btn_label = lv_label_create(btn1);\n  lv_label_set_text(btn_label, &quot;Button&quot;);\n  lv_obj_center(btn_label);\n\n  \/\/ Create a Toggle button (btn2)\n  lv_obj_t * btn2 = lv_button_create(lv_screen_active());\n  lv_obj_add_event_cb(btn2, event_handler_btn2, LV_EVENT_ALL, NULL);\n  lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 10);\n  lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);\n  lv_obj_set_height(btn2, LV_SIZE_CONTENT);\n\n  btn_label = lv_label_create(btn2);\n  lv_label_set_text(btn_label, &quot;Toggle&quot;);\n  lv_obj_center(btn_label);\n  \n  \/\/ Create a slider aligned in the center bottom of the TFT display\n  lv_obj_t * slider = lv_slider_create(lv_screen_active());\n  lv_obj_align(slider, LV_ALIGN_CENTER, 0, 60);\n  lv_obj_add_event_cb(slider, slider_event_callback, LV_EVENT_VALUE_CHANGED, NULL);\n  lv_slider_set_range(slider, 0, 100);\n  lv_obj_set_style_anim_duration(slider, 2000, 0);\n\n  \/\/ Create a label below the slider to display the current slider value\n  slider_label = lv_label_create(lv_screen_active());\n  lv_label_set_text(slider_label, &quot;0%&quot;);\n  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);\n}\n\nvoid setup() {\n  String LVGL_Arduino = String(&quot;LVGL Library Version: &quot;) + lv_version_major() + &quot;.&quot; + lv_version_minor() + &quot;.&quot; + lv_version_patch();\n  Serial.begin(115200);\n  Serial.println(LVGL_Arduino);\n  \n  \/\/ Start LVGL\n  lv_init();\n  \/\/ Register print function for debugging\n  lv_log_register_print_cb(log_print);\n\n  \/\/ Start the SPI for the touchscreen and init the touchscreen\n  touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);\n  touchscreen.begin(touchscreenSPI);\n  \/\/ Set the Touchscreen rotation in landscape mode\n  \/\/ Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 0: touchscreen.setRotation(0);\n  touchscreen.setRotation(2);\n\n  \/\/ Create a display object\n  lv_display_t * disp;\n  \/\/ Initialize the TFT display using the TFT_eSPI library\n  disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));\n  lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);\n    \n  \/\/ Initialize an LVGL input device object (Touchscreen)\n  lv_indev_t * indev = lv_indev_create();\n  lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);\n  \/\/ Set the callback function to read Touchscreen input\n  lv_indev_set_read_cb(indev, touchscreen_read);\n\n  \/\/ Function to draw the GUI (text, buttons and sliders)\n  lv_create_main_gui();\n}\n\nvoid loop() {\n  lv_task_handler();  \/\/ let the GUI do its work\n  lv_tick_inc(5);     \/\/ tell LVGL how much time has passed\n  delay(5);           \/\/ let this time pass\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/main\/examples\/ESP32_LVGL_Basic\/ESP32_LVGL_Basic.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Let&#8217;s take a quick look at the parts of the code that are relevant to this example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Libraries<\/h3>\n\n\n\n<p>Include the <span class=\"rnthl rntliteral\">lvgl<\/span>, <span class=\"rnthl rntliteral\">TFT_eSPI<\/span> and <span class=\"rnthl rntliteral\">XPT2046_Touchscreen<\/span> libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;lvgl.h&gt;\n#include &lt;TFT_eSPI.h&gt;\n#include &lt;XPT2046_Touchscreen.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Initialize Touchscreen<\/h3>\n\n\n\n<p>The following lines set the touchscreen pinout:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define XPT2046_IRQ 36\n#define XPT2046_MOSI 32\n#define XPT2046_MISO 39\n#define XPT2046_CLK 25\n#define XPT2046_CS 33<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">touchscreenSPI<\/span> and <span class=\"rnthl rntliteral\">touchscreen<\/span> instances:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SPIClass touchscreenSPI = SPIClass(VSPI);\nXPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Variables<\/h3>\n\n\n\n<p>Set the screen width and screen height:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 240\n#define SCREEN_HEIGHT 320\n#define FONT_SIZE 2<\/code><\/pre>\n\n\n\n<p>Variables to store the coordinates: (<span class=\"rnthl rntliteral\">x<\/span>, <span class=\"rnthl rntliteral\">y<\/span>) and pressure (<span class=\"rnthl rntliteral\">z<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int x, y, z;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>Start a serial communication with the Serial Monitor at a baud rate of 115200 and print the LVGL library version that you are using:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String LVGL_Arduino = String(\"LVGL Library Version: \") + lv_version_major() + \".\" + lv_version_minor() + \".\" + lv_version_patch();\nSerial.begin(115200);\nSerial.println(LVGL_Arduino);<\/code><\/pre>\n\n\n\n<p>Start the LVGL and assign a callback function for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_init();\nlv_log_register_print_cb(log_print);<\/code><\/pre>\n\n\n\n<p>Start the SPI for the touchscreen and initialize the touchscreen.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);\ntouchscreen.begin(touchscreenSPI);\ntouchscreen.setRotation(2);<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Note:<\/strong> in some displays, the touchscreen might be upside down, so you might need to set the rotation to 0: <span class=\"rnthl rntliteral\">touchscreen.setRotation(2);<\/span><\/p>\n\n\n\n<p>Create the display object and initialize the TFT display using the <span class=\"rnthl rntliteral\">TFT_eSPI<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_display_t * disp;\ndisp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));\nlv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);<\/code><\/pre>\n\n\n\n<p>Initialize an LVGL input device object (touchscreen) and set the callback function that will be triggered when you click the touchscreen.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_indev_t * indev = lv_indev_create();\nlv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);\nlv_indev_set_read_cb(indev, touchscreen_read);<\/code><\/pre>\n\n\n\n<p>Finally, call the <span class=\"rnthl rntliteral\">lv_create_main_gui()<\/span> function to draw the GUI for your touchscreen:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_create_main_gui();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>When running an LVGL example, the <span class=\"rnthl rntliteral\">loop()<\/span> will usually look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lv_task_handler();  \/\/ let the GUI do its work\nlv_tick_inc(5);        \/\/ tell LVGL how much time has passed\ndelay(5);                \/\/ let this time pass<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">lv_create_main_gui()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">lv_create_main_gui()<\/span> function draws the text label, buttons, and sliders. It&#8217;s also where you assign the event handler callback functions that will be triggered when you interact with your display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void lv_create_main_gui(void) {\n  \/\/ Create a text label aligned center on top (\"Hello, world!\")\n  lv_obj_t * text_label = lv_label_create(lv_screen_active());\n  lv_label_set_long_mode(text_label, LV_LABEL_LONG_WRAP);    \/\/ Breaks the long lines\n  lv_label_set_text(text_label, \"Hello, world!\");\n  lv_obj_set_width(text_label, 150);    \/\/ Set smaller width to make the lines wrap\n  lv_obj_set_style_text_align(text_label, LV_TEXT_ALIGN_CENTER, 0);\n  lv_obj_align(text_label, LV_ALIGN_CENTER, 0, -90);\n\n  lv_obj_t * btn_label;\n  \/\/ Create a Button (btn1)\n  lv_obj_t * btn1 = lv_button_create(lv_screen_active());\n  lv_obj_add_event_cb(btn1, event_handler_btn1, LV_EVENT_ALL, NULL);\n  lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -50);\n  lv_obj_remove_flag(btn1, LV_OBJ_FLAG_PRESS_LOCK);\n\n  btn_label = lv_label_create(btn1);\n  lv_label_set_text(btn_label, \"Button\");\n  lv_obj_center(btn_label);\n\n  \/\/ Create a Toggle button (btn2)\n  lv_obj_t * btn2 = lv_button_create(lv_screen_active());\n  lv_obj_add_event_cb(btn2, event_handler_btn2, LV_EVENT_ALL, NULL);\n  lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 10);\n  lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);\n  lv_obj_set_height(btn2, LV_SIZE_CONTENT);\n\n  btn_label = lv_label_create(btn2);\n  lv_label_set_text(btn_label, \"Toggle\");\n  lv_obj_center(btn_label);\n  \n  \/\/ Create a slider aligned in the center bottom of the TFT display\n  lv_obj_t * slider = lv_slider_create(lv_screen_active());\n  lv_obj_align(slider, LV_ALIGN_CENTER, 0, 60);\n  lv_obj_add_event_cb(slider, slider_event_callback, LV_EVENT_VALUE_CHANGED, NULL);\n  lv_slider_set_range(slider, 0, 100);\n  lv_obj_set_style_anim_duration(slider, 2000, 0);\n\n  \/\/ Create a label below the slider to display the current slider value\n  slider_label = lv_label_create(lv_screen_active());\n  lv_label_set_text(slider_label, \"0%\");\n  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">event_handler_btn1()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">event_handler_btn1()<\/span> function is triggered when you click the &#8220;Button&#8221; and it will also display in the Arduino IDE Serial Monitor the number of times the button has been clicked.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int btn1_count = 0;\n\/\/ Callback that is triggered when btn1 is clicked\nstatic void event_handler_btn1(lv_event_t * e) {\n  lv_event_code_t code = lv_event_get_code(e);\n  if(code == LV_EVENT_CLICKED) {\n    btn1_count++;\n    LV_LOG_USER(\"Button clicked %d%\", (int)btn1_count);\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">event_handler_btn2()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">event_handler_btn2()<\/span> is triggered when you click the &#8220;Toggle&#8221; button, it stores the current button state and prints a message with the current state in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Callback that is triggered when btn2 is clicked\/toggled\nstatic void event_handler_btn2(lv_event_t * e) {\n  lv_event_code_t code = lv_event_get_code(e);\n  lv_obj_t * obj = (lv_obj_t*) lv_event_get_target(e);\n  if(code == LV_EVENT_VALUE_CHANGED) {\n    LV_UNUSED(obj);\n    LV_LOG_USER(\"Toggled %s\", lv_obj_has_state(obj, LV_STATE_CHECKED) ? \"on\" : \"off\");\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">slider_event_callback()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">slider_event_callback()<\/span> function is called when you move the slider, it will also display both in the touchscreen and Serial Monitor the latest slider value on a scale from 0 to 100%.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>static lv_obj_t * slider_label;\n\/\/ Callback that prints the current slider value on the TFT display and Serial Monitor for debugging purposes\nstatic void slider_event_callback(lv_event_t * e) {\n  lv_obj_t * slider = (lv_obj_t*) lv_event_get_target(e);\n  char buf&#091;8];\n  lv_snprintf(buf, sizeof(buf), \"%d%%\", (int)lv_slider_get_value(slider));\n  lv_label_set_text(slider_label, buf);\n  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);\n  LV_LOG_USER(\"Slider changed to %d%%\", (int)lv_slider_get_value(slider));\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your board. Go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select <strong>ESP32 <\/strong>&gt; <strong>ESP32 Dev Module<\/strong>. Then, select the right COM port in <strong>Tools <\/strong>&gt; <strong>Port<\/strong>. Finally, click the upload button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"36\" height=\"39\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-ide-2-upload-button.png?resize=36%2C39&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE 2 Upload Button\" class=\"wp-image-146269\" style=\"width:36px;height:auto\"\/><\/figure><\/div>\n\n\n<p>After uploading the code to your board, it should display the sample &#8220;Hello, world!&#8221; text centered at the top.<\/p>\n\n\n\n<p>Press with your finger the first &#8220;Button&#8221;. In the Serial Monitor, it will print the coordinates of your button press, as well as a message with the number of times the button has been clicked: &#8220;Button clicked 1&#8221;, &#8220;Button clicked 2&#8221;, &#8220;Button clicked 3&#8243;&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Display Touchscreen LVGL Library Example\" class=\"wp-image-155023\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, click the &#8220;Toggle&#8221; button, this could be used to control an LED or any other output. This button type has two states (color blue when off and color red when on).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Button-Toggle.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Display Touchscreen LVGL Library Example Button Toggle\" class=\"wp-image-155024\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Button-Toggle.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Button-Toggle.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Drag the slider with your finger, you&#8217;ll see the slider moving and the slider label changing when you move your finger.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Move-Slider.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Display Touchscreen LVGL Library Example Move Slider\" class=\"wp-image-155025\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Move-Slider.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Display-Touchscreen-LVGL-Library-Example-Move-Slider.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>All the events will be printed in your Arduino IDE Serial Monitor for debugging purposes:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"430\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Touchscreen-Display-LVGL-Basic-Example-Text-Label-Button-Slider.png?resize=1024%2C430&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Touchscreen Display LVGL Basic Example Text Label Button Slider\" class=\"wp-image-155029\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Touchscreen-Display-LVGL-Basic-Example-Text-Label-Button-Slider.png?resize=1024%2C430&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Touchscreen-Display-LVGL-Basic-Example-Text-Label-Button-Slider.png?resize=300%2C126&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Touchscreen-Display-LVGL-Basic-Example-Text-Label-Button-Slider.png?resize=768%2C322&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-Touchscreen-Display-LVGL-Basic-Example-Text-Label-Button-Slider.png?w=1161&amp;quality=100&amp;strip=all&amp;ssl=1 1161w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned to get started with the LVGL library with the TFT Touchscreen wired to an ESP32 board. In the next guides, we&#8217;ll explore other features and components of the LVGL library.<\/p>\n\n\n\n<p>If you have an ESP32 Cheap Yellow Display board, <a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\" title=\"\">you can follow this other guide to use the LVGL with the CYD board<\/a>.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. We&#8217;re preparing more guides about the LVGL library, so stay tuned.<\/p>\n\n\n\n<p>Other guides you might like reading:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-tft-touchscreen-display-2-8-ili9341-arduino\/\">ESP32: TFT LCD Touchscreen Display \u2013 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-tft-touchscreen-on-off-button-ili9341-arduino\/\">ESP32: TFT Touchscreen On\/Off Button \u2013 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>To learn more about the ESP32, make sure to take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, and ESP8266 eBook<\/a><\/li>\n\n\n\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/learn-lvgl-esp32-ebook\/\">Learn LVGL: Build GUIs for ESP32 Projects (eBook)<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\" title=\"\"><strong>Free ESP32 Guides and Tutorials<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll get started with the LVGL (Light and Versatile Graphics Library) using a TFT LCD Touchscreen Display wired to an ESP32 board. The LVGL is a popular &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"LVGL with ESP32 TFT LCD Touchscreen Display \u2013 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/lvgl-esp32-tft-touchscreen-display-ili9341-arduino\/#more-155074\" aria-label=\"Read more about LVGL with ESP32 TFT LCD Touchscreen Display \u2013 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":155070,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-155074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/05\/ESP32-TFT-LCD-Touchscreen-Display-2-8-inch-ILI9341-240px-320px-Arduino-IDE-LVGL-Graphics-Library.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/155074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=155074"}],"version-history":[{"count":17,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/155074\/revisions"}],"predecessor-version":[{"id":162153,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/155074\/revisions\/162153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/155070"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=155074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=155074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=155074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}