{"id":150728,"date":"2024-04-23T17:11:22","date_gmt":"2024-04-23T17:11:22","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=150728"},"modified":"2024-06-25T22:43:08","modified_gmt":"2024-06-25T22:43:08","slug":"touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r\/","title":{"rendered":"ESP32 Touchscreen On\/Off Button &#8211; Cheap Yellow Display (ESP32-2432S028R)"},"content":{"rendered":"\n<p>In this project, you&#8217;ll learn how to create a simple graphical user interface (GUI) with the ESP32 Cheap Yellow Display (ESP32-2432S028R). 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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Touchscreen On and Off Buttons Cheap Yellow Display ESP32-2432S028R\" class=\"wp-image-150895\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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\">If you have a standalone TFT Touchscreen Display 2.8 inch with ILI9341 driver, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-tft-touchscreen-on-off-button-ili9341-arduino\/\">you can follow this guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the ESP32 Cheap Yellow Display &#8211; CYD (ESP32-2432S028R)<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP32-2432S028R<\/a> development board has become known in the maker community as the &#8220;<em>Cheap Yellow Display<\/em>&#8221; or CYD for short. This development board, whose main chip is an ESP32-WROOM-32 module, comes with a 2.8-inch TFT touchscreen LCD, a microSD card interface, an RGB LED, and all the required circuitry to program and apply power to the board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display CYD Board ESP32-2432S028R front\" class=\"wp-image-149592\" style=\"width:750px;height:auto\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.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-Cheap-Yellow-Display-CYD-Board-ESP32-2432S028R-front.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p>This is a very versatile board to build GUIs for your IoT projects, and is much more convenient and practical than using a separate ESP32 board with a TFT screen.<\/p>\n\n\n\n<p><strong>Where to buy?<\/strong><\/p>\n\n\n\n<p>You can click on the link below to check where to buy the ESP32 Cheap Yellow display and its price in different stores.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/cyd-cheap-yellow-display-esp32-2432s028r\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP32-2432S028R \u2013 2.8 inch 240\u00d7320 Smart Display TFT with Touchscreen &#8211; Maker Advisor<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">More information about the CYD board GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cheap-yellow-display-cyd-pinout-esp32-2432s028r\/\">ESP32 Cheap Yellow Display (CYD) Pinout (ESP32-2432S028R)<\/a>.<\/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\/cheap-yellow-display-esp32-2432s028r\/\">Getting Started with ESP32 Cheap Yellow Display<\/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 green RGB LED on the back of the board.<\/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      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\/\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\/touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r\/\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\/cyd\/ *\/\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\/\/ RGB LED Pins\n#define CYD_LED_BLUE 17\n#define CYD_LED_RED 4\n#define CYD_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(CYD_LED_GREEN, OUTPUT);\n  digitalWrite(CYD_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(CYD_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(CYD_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_CYD_ON_OFF_Button\/ESP32_CYD_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>Assign the RGB LED GPIOs (this is the LED at the back of the board). In this example, we&#8217;ll only be controlling the green part of the LED (i.e. GPIO 16), but you can control other GPIOs to generate different colors.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define CYD_LED_BLUE 17\n#define CYD_LED_RED 4\n#define CYD_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(CYD_LED_GREEN, OUTPUT);\ndigitalWrite(CYD_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(CYD_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(CYD_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\" 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\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R-Arduino-IDE.jpg?quality=100&#038;strip=all&#038;ssl=1\"><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\/Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R-Arduino-IDE.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Touchscreen On Off Button Cheap Yellow Display ESP32-2432S028R Arduino IDE\" class=\"wp-image-150733\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R-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\/Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p>Pressing the ON button turns on the green RGB LED at the back of the board (it turns on GPIO 16).<\/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-Cheap-Yellow-Display-RGB-green-LED-ON-ESP32-2432S028R.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Cheap Yellow Display RGB green LED ON ESP32-2432S028R\" class=\"wp-image-150732\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-Cheap-Yellow-Display-RGB-green-LED-ON-ESP32-2432S028R.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-Cheap-Yellow-Display-RGB-green-LED-ON-ESP32-2432S028R.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>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\/Touchscreen-Press-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Touchscreen Press On Off Button Cheap Yellow Display ESP32-2432S028R\" class=\"wp-image-150734\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Touchscreen-Press-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Touchscreen-Press-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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 Cheap Yellow Display board with an On\/Off button.<\/p>\n\n\n\n<p>If it&#8217;s your first time using this board, 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>Other tutorials you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/lvgl-cheap-yellow-display-esp32-2432s028r\/\" title=\"\">LVGL with ESP32 Cheap Yellow Display Board (ESP32-2432S028R)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cheap-yellow-display-cyd-pinout-esp32-2432s028r\/\">ESP32 Cheap Yellow Display (CYD) Pinout (ESP32-2432S028R)<\/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\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project, you&#8217;ll learn how to create a simple graphical user interface (GUI) with the ESP32 Cheap Yellow Display (ESP32-2432S028R). The TFT display will have an ON and OFF &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Touchscreen On\/Off Button &#8211; Cheap Yellow Display (ESP32-2432S028R)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/touchscreen-on-off-button-cheap-yellow-display-esp32-2432s028r\/#more-150728\" aria-label=\"Read more about ESP32 Touchscreen On\/Off Button &#8211; Cheap Yellow Display (ESP32-2432S028R)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":150895,"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":[276,281,277,299,264],"tags":[],"class_list":["post-150728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","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-Touchscreen-On-Off-Button-Cheap-Yellow-Display-ESP32-2432S028R.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\/150728","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=150728"}],"version-history":[{"count":10,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150728\/revisions"}],"predecessor-version":[{"id":159631,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150728\/revisions\/159631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/150895"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=150728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=150728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=150728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}