{"id":151439,"date":"2024-04-25T13:32:13","date_gmt":"2024-04-25T13:32:13","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=151439"},"modified":"2024-07-30T16:39:50","modified_gmt":"2024-07-30T16:39:50","slug":"esp32-tft-touchscreen-on-off-button-ili9341-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-tft-touchscreen-on-off-button-ili9341-arduino\/","title":{"rendered":"ESP32: TFT Touchscreen On\/Off Button &#8211; 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)"},"content":{"rendered":"\n<p>In this project, you&#8217;ll build a simple graphical user interface (GUI) with the ESP32 and a TFT Touchscreen with 2.8 inch display (240\u00d7320 px) that comes with the ILI9341 driver. The TFT display will have an ON and OFF button that you can use to control an output.<\/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\/03\/ESP32-TFT-display-button-thumbnail.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Touchscreen On and Off Buttons 2.8 inch ILI9341 240\u00d7320 Arduino IDE\" class=\"wp-image-151442\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-display-button-thumbnail.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-display-button-thumbnail.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-display-button-thumbnail.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-display-button-thumbnail.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-display-button-thumbnail.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 guide <a href=\"https:\/\/randomnerdtutorials.com\/touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r\/\" title=\"\">ESP32 Touchscreen On\/Off Button &#8211; Cheap Yellow Display (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><figcaption class=\"wp-element-caption\">* In the VCC pin, you can either use 5V or 3.3V depending if your J1 connection is open or closed (by default it\u2019s usually open).<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/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> and <strong>XPT2046_Touchscreen<\/strong> libraries. To properly use the TFT_eSPI library, you need a configuration file called <span class=\"rnthl rntliteral\">User_Setup.h<\/span> with the right definitions.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-dd9f0a2c0f4f7624c42013ca9859051f\" style=\"color:#cc3333\"><strong>You must follow the next instructions to prepare your Arduino IDE to compile the code provided in this project:<\/strong><\/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\/\">Getting Started with ESP32: TFT LCD Touchscreen Display &#8211; 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Code &#8211; Touchscreen with On\/Off Button<\/h2>\n\n\n\n<p>With the following code, the TFT will display an ON\/OFF button to control an output. When you press the touchscreen with your finger or pen, it should turn on\/off the GPIO 16 (which can have an LED attached).<\/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      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\/\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    Complete project details: https:\/\/RandomNerdTutorials.com\/esp32-tft-touchscreen-on-off-button-ili9341-arduino\/\n*\/\n\n#include &lt;SPI.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\/esp32-tft\/   *\/\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\n\/\/ Note: this library doesn't require further configuration\n#include &lt;XPT2046_Touchscreen.h&gt;\n\nTFT_eSPI tft = TFT_eSPI();\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 320\n#define SCREEN_HEIGHT 240\n#define FONT_SIZE 3\n\n\/\/ Button position and size\n#define FRAME_X 60\n#define FRAME_Y 60\n#define FRAME_W 200\n#define FRAME_H 120\n\n\/\/ Red zone size\n#define REDBUTTON_X FRAME_X\n#define REDBUTTON_Y FRAME_Y\n#define REDBUTTON_W (FRAME_W \/ 2)\n#define REDBUTTON_H FRAME_H\n\n\/\/ Green zone size\n#define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)\n#define GREENBUTTON_Y FRAME_Y\n#define GREENBUTTON_W (FRAME_W \/ 2)\n#define GREENBUTTON_H FRAME_H\n\n\/\/ LED Pin\n#define LED_GREEN 16\n\n\/\/ Touchscreen coordinates: (x, y) and pressure (z)\nint x, y, z;\n\n\/\/ Stores current button state\nbool buttonState = false;\n\n\/\/ Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor\nvoid printTouchToSerial(int touchX, int touchY, int touchZ) {\n  Serial.print(&quot;X = &quot;);\n  Serial.print(touchX);\n  Serial.print(&quot; | Y = &quot;);\n  Serial.print(touchY);\n  Serial.print(&quot; | Pressure = &quot;);\n  Serial.print(touchZ);\n  Serial.println();\n}\n\n\/\/ Draw button frame\nvoid drawFrame() {\n  tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, TFT_BLACK);\n}\n\n\/\/ Draw a red button\nvoid drawRedButton() {\n  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_RED);\n  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_WHITE);\n  drawFrame();\n  tft.setTextColor(TFT_BLACK);\n  tft.setTextSize(FONT_SIZE);\n  tft.setTextDatum(MC_DATUM);\n  tft.drawString(&quot;ON&quot;, GREENBUTTON_X + (GREENBUTTON_W \/ 2), GREENBUTTON_Y + (GREENBUTTON_H \/ 2));\n  buttonState = false;\n}\n\n\/\/ Draw a green button\nvoid drawGreenButton() {\n  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_GREEN);\n  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_WHITE);\n  drawFrame();\n  tft.setTextColor(TFT_BLACK);\n  tft.setTextSize(FONT_SIZE);\n  tft.setTextDatum(MC_DATUM);\n  tft.drawString(&quot;OFF&quot;, REDBUTTON_X + (REDBUTTON_W \/ 2) + 1, REDBUTTON_Y + (REDBUTTON_H \/ 2));\n  buttonState = true;\n}\n\nvoid setup() {\n  Serial.begin(115200);\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 3: touchscreen.setRotation(3);\n  touchscreen.setRotation(1);\n\n  \/\/ Start the tft display\n  tft.init();\n  \/\/ Set the TFT display rotation in landscape mode\n  tft.setRotation(1);\n\n  \/\/ Clear the screen before writing to it\n  tft.fillScreen(TFT_BLACK);\n\n  \/\/ Draw button \n  drawGreenButton();\n\n  pinMode(LED_GREEN, OUTPUT);\n  digitalWrite(LED_GREEN, LOW);\n}\n\nvoid loop() {\n  \/\/ Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor\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    printTouchToSerial(x, y, z);\n\n    if (buttonState) {\n      Serial.println(&quot;ON&quot;);\n      if ((x &gt; REDBUTTON_X) &amp;&amp; (x &lt; (REDBUTTON_X + REDBUTTON_W))) {\n        if ((y &gt; (REDBUTTON_Y)) &amp;&amp; (y &lt;= (REDBUTTON_Y + REDBUTTON_H))) {\n          Serial.println(&quot;Red button pressed&quot;);\n          drawRedButton();\n          digitalWrite(LED_GREEN, HIGH);\n        }\n      }\n    }\n    else {\n      Serial.println(&quot;OFF&quot;);\n      if ((x &gt; (GREENBUTTON_X)) &amp;&amp; (x &lt; (GREENBUTTON_X + GREENBUTTON_W))) {\n        if ((y &gt; (GREENBUTTON_Y)) &amp;&amp; (y &lt;= (GREENBUTTON_Y + GREENBUTTON_H))) {\n          Serial.println(&quot;Green button pressed&quot;);\n          drawGreenButton();\n          digitalWrite(LED_GREEN, LOW);\n        }\n      }\n    }\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-TFT-Touchscreen\/raw\/main\/examples\/ESP32_TFT_Touchscreen_ON_OFF_Button\/ESP32_TFT_Touchscreen_ON_OFF_Button.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\">SPI<\/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;SPI.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 TFT<\/h3>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">TFT_eSPI<\/span> instance:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>TFT_eSPI tft = TFT_eSPI();<\/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, screen height, and font size:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 320\n#define SCREEN_HEIGHT 240\n#define FONT_SIZE 3<\/code><\/pre>\n\n\n\n<p>These next variables have self-explanatory names, they are used to define the frame\/button sizes and coordinates:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Button position and size\n#define FRAME_X 60\n#define FRAME_Y 60\n#define FRAME_W 200\n#define FRAME_H 120\n\n\/\/ Red zone size\n#define REDBUTTON_X FRAME_X\n#define REDBUTTON_Y FRAME_Y\n#define REDBUTTON_W (FRAME_W \/ 2)\n#define REDBUTTON_H FRAME_H\n\n\/\/ Green zone size\n#define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)\n#define GREENBUTTON_Y FRAME_Y\n#define GREENBUTTON_W (FRAME_W \/ 2)\n#define GREENBUTTON_H FRAME_H<\/code><\/pre>\n\n\n\n<p>In this example, we&#8217;ll only be controlling GPIO 16:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define LED_GREEN 16<\/code><\/pre>\n\n\n\n<p>Variables to store the touch coordinates: (x, y) and pressure (z).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int x, y, z;<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">switchState<\/span> stores the current button state:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool buttonState = false;<\/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:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/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(1);<\/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 3: <span class=\"rnthl rntliteral\">touchscreen.setRotation(3);<\/span><\/p>\n\n\n\n<p>Start the TFT display and set the TFT display rotation in landscape mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>tft.init();\ntft.setRotation(1);<\/code><\/pre>\n\n\n\n<p>Set the TFT screen background to black.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>tft.fillScreen(TFT_BLACK);<\/code><\/pre>\n\n\n\n<p>At the start, the LED is set to off, so draw the green button that will turn on the LED when pressed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>drawGreenButton();\npinMode(LED_GREEN, OUTPUT);\ndigitalWrite(LED_GREEN, LOW);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, it constantly checks if the touchscreen was touched.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (touchscreen.tirqTouched() &amp;&amp; touchscreen.touched()) {<\/code><\/pre>\n\n\n\n<p>When it detects that the touchscreen was touched, it will get the (x,y) coordinates and the pressure (z) from the point.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>TS_Point p = touchscreen.getPoint();\n\/\/ Calibrate Touchscreen points with map function to the correct width and height\nx = map(p.x, 200, 3700, 1, SCREEN_WIDTH);\ny = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);\nz = p.z;<\/code><\/pre>\n\n\n\n<p>Call the <span class=\"rnthl rntliteral\">printTouchToSerial<\/span> function to print the touchscreen info in the Serial Monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>printTouchToSerial(x, y, z);<\/code><\/pre>\n\n\n\n<p>Based on the x and y coordinates detected by the touchscreen press, it will check if you pressed within the limits of the ON or OFF button coordinates. Then, it will turn the LED on or off accordingly and draw the right button to display by calling the <span class=\"rnthl rntliteral\">drawRedButton()<\/span> or <span class=\"rnthl rntliteral\">drawGreenButton()<\/span> functions.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (buttonState)  {\n  Serial.println(\"ON\");\n  if ((x &gt; REDBUTTON_X) &amp;&amp; (x &lt; (REDBUTTON_X + REDBUTTON_W))) {\n    if ((y &gt; (REDBUTTON_Y)) &amp;&amp; (y &lt;= (REDBUTTON_Y + REDBUTTON_H))) {\n      Serial.println(\"Red button pressed\");\n      drawRedButton();\n      digitalWrite(LED_GREEN, HIGH);\n    }\n  }\n}\nelse {\n  Serial.println(\"OFF\");\n  if ((x &gt; (GREENBUTTON_X)) &amp;&amp; (x &lt; (GREENBUTTON_X + GREENBUTTON_W))) {\n    if ((y &gt; (GREENBUTTON_Y)) &amp;&amp; (y &lt;= (GREENBUTTON_Y + GREENBUTTON_H))) {\n      Serial.println(\"Green button pressed\");\n      drawGreenButton();\n      digitalWrite(LED_GREEN, LOW);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">printTouchToSerial()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">printTouchToSerial()<\/span> function prints touchscreen info about X, Y, and Pressure (Z) on the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void printTouchToSerial(int touchX, int touchY, int touchZ) {\n  Serial.print(\"X = \");\n  Serial.print(touchX);\n  Serial.print(\" | Y = \");\n  Serial.print(touchY);\n  Serial.print(\" | Pressure = \");\n  Serial.print(touchZ);\n  Serial.println();\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Functions: drawFrame(), drawRedButton(), drawGreenButton()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">drawFrame()<\/span> function draws a frame around the ON or OFF buttons.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Draw button frame\nvoid drawFrame() {\n  tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, TFT_BLACK);\n}<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">drawRedButton()<\/span> and <span class=\"rnthl rntliteral\">drawGreenButton()<\/span> functions create the ON and OFF buttons on the TFT display. It will draw and fill a rectangle based on the x and y coordinates defined at the start of the sketch, as well as writing the ON and OFF text labels.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Draw a red button\nvoid drawRedButton() {\n  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_RED);\n  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_WHITE);\n  drawFrame();\n  tft.setTextColor(TFT_BLACK);\n  tft.setTextSize(FONT_SIZE);\n  tft.setTextDatum(MC_DATUM);\n  tft.drawString(\"ON\", GREENBUTTON_X + (GREENBUTTON_W \/ 2), GREENBUTTON_Y + (GREENBUTTON_H \/ 2));\n  buttonState = false;\n}\n\n\/\/ Draw a green button\nvoid drawGreenButton() {\n  tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_GREEN);\n  tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_WHITE);\n  drawFrame();\n  tft.setTextColor(TFT_BLACK);\n  tft.setTextSize(FONT_SIZE);\n  tft.setTextDatum(MC_DATUM);\n  tft.drawString(\"OFF\", REDBUTTON_X + (REDBUTTON_W \/ 2) + 1, REDBUTTON_Y + (REDBUTTON_H \/ 2));\n  buttonState = true;\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, the TFT display should have an ON button that you can press with 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\/03\/TFT-Touchscreen-Press-Off-Button-ESP32.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"TFT Touchscreen Press Off Button ESP32 Arduino IDE\" class=\"wp-image-150731\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/TFT-Touchscreen-Press-Off-Button-ESP32.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-Touchscreen-Press-Off-Button-ESP32.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>Pressing the ON button turns on the green LED on GPIO 16. If the LED is on, the display should have the OFF button. You can click on it to turn the LED off.<\/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\/03\/ESP32-TFT-LCD-Touchscreen-On-Off-Buttons-Arduino-IDE.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT LCD Touchscreen On Off Buttons Arduino IDE\" class=\"wp-image-150730\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-LCD-Touchscreen-On-Off-Buttons-Arduino-IDE.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-LCD-Touchscreen-On-Off-Buttons-Arduino-IDE.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>The coordinates, pressure, and LED state are also printed on the  Arduino IDE serial monitor for debugging purposes:<\/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=\"937\" height=\"507\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Touchscreen-On-Off-Button-Cheap-Yellow-Display-Arduino-IDE-Serial-Monitor.png?resize=937%2C507&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 TFT Touchscreen On Off Button Cheap Yellow Display Arduino IDE Serial Monitor\" class=\"wp-image-150735\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Touchscreen-On-Off-Button-Cheap-Yellow-Display-Arduino-IDE-Serial-Monitor.png?w=937&amp;quality=100&amp;strip=all&amp;ssl=1 937w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Touchscreen-On-Off-Button-Cheap-Yellow-Display-Arduino-IDE-Serial-Monitor.png?resize=300%2C162&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-TFT-Touchscreen-On-Off-Button-Cheap-Yellow-Display-Arduino-IDE-Serial-Monitor.png?resize=768%2C416&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 937px) 100vw, 937px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to build a basic GUI for the ESP32 with a TFT Touchscreen display.<\/p>\n\n\n\n<p>We also recommend reading our: <a href=\"https:\/\/randomnerdtutorials.com\/cheap-yellow-display-esp32-2432s028r\/\">Getting Started with ESP32 Cheap Yellow Display Board \u2013 CYD (ESP32-2432S028R)<\/a><\/p>\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 project, you&#8217;ll build a simple graphical user interface (GUI) with the ESP32 and a TFT Touchscreen with 2.8 inch display (240\u00d7320 px) that comes with the ILI9341 driver. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32: TFT Touchscreen On\/Off Button &#8211; 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-tft-touchscreen-on-off-button-ili9341-arduino\/#more-151439\" aria-label=\"Read more about ESP32: TFT Touchscreen On\/Off Button &#8211; 2.8 inch ILI9341 240\u00d7320 (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":151442,"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-151439","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\/03\/ESP32-TFT-display-button-thumbnail.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\/151439","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=151439"}],"version-history":[{"count":8,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/151439\/revisions"}],"predecessor-version":[{"id":161229,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/151439\/revisions\/161229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/151442"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=151439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=151439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=151439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}