IoT/ESP32 B.Tech/Diploma Beginner

ESP32 CYD: Use Display, Touchscreen and MicroSD Card Simultaneously

Use the display, touchscreen, and microSD card all at once with the ESP32 CYD (Cheap Yellow Display) board. We will save the current touch points on the microSD card.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

In this guide, we’ll show you how to use the display, touchscreen, and microSD card all at once with the ESP32 CYD (Cheap Yellow Display) board. We’ll build a simple example that shows the current touch points on the screen while simultaneously saving them to the microSD card.
Although this is a basic example, it provides a solid foundation for understanding how to combine these features. Using all three functions simultaneously on the CYD board is not as straightforward as it seems due to hardware/library limitations related to the SPI communication protocol. This approach was tested on the
ESP32-2432S028R – 2.8 inch 240×320 with a resistive touchscreen
New to the ESP32 CYD board?
Get Started with this guide:
Getting Started with ESP32 Cheap Yellow Display Board – CYD (ESP32-2432S028R)
Prerequisites
Before proceeding, make sure you follow the next prerequisites.
You must follow all steps, otherwise, your project will not work.
1) Install ESP32 Boards in Arduino IDE
We’ll program the ESP32 using Arduino IDE. Make sure you have the ESP32 boards installed. Follow the next tutorial:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
2) Install TFT and LVGL Libraries
LVGL
(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).
Follow the next tutorial to install and configure the required libraries to use LVGL for the
ESP32 Cheap Yellow Display
using Arduino IDE.
Get started with LVGL using the ESP32 Cheap Yellow Display Board (ESP32-2432S028R)
The MicroSD Card and SPI Communication
Before diving into the example, it’s important to review some key points about the SPI communication protocol on ESP32 boards, particularly the CYD.
The ESP32 supports two separate SPI buses: VSPI and HSPI. These are the default pins for VSPI and HSPI.
SPI
MOSI
MISO
SCLK
CS
VSPI
GPIO 23
GPIO 19
GPIO 18
GPIO 5
HSPI
GPIO 13
GPIO 12
GPIO 14
GPIO 15
Recommended reading:
ESP32 SPI Communication: Set Pins, Multiple SPI Bus Interfaces, and Peripherals (Arduino IDE)
The microSD card on the CYD uses the default VSPI pins:
MicroSD card
GPIO
MOSI
GPIO 23
MISO
GPIO 19
SCLK
GPIO 18
CS
GPIO 5
Recommended reading:
ESP32 Guide for MicroSD Card Module (Arduino IDE)
Shared SPI Bus
Both the display and touchscreen use the SPI communication protocol. In our examples, the display library is configured to use the HSPI bus, while the touchscreen library uses the VSPI bus. This becomes problematic because the
microSD card also uses the VSPI bus (on the default hardware pins), creating a conflict
At first glance, this may not seem like a problem. After all, multiple devices can share the same SPI bus if each one has its own CS (Chip Select) pin.
HOWEVER, the issue arises with the touchscreen, which continuously listens for touch events. Temporarily disabling it while accessing the microSD card isn’t ideal. Additionally, many users report this approach doesn’t work reliably.
Software SPI for the touchscreen
So, what can we do about this?
We know that the ESP32 allows the use of Software SPI (SoftSPI) on any available pins. To solve the conflict, we can use a modified version of the
XPT2046_Touchscreen
library that communicates over SoftSPI instead of hardware SPI. This lets us separate the touchscreen from the VSPI bus and avoid conflicts with the microSD card.
Download the XPT2046_TouchscreenSOFTSPI library
We’ll call this modified version of the library
XPT2046_TouchscreenSOFTSPI
. This library also needs the
SoftSPI
library, which we’ll also install locally.
Click here to download all the project files
(including the main sketch)
This link should download a
.zip
folder with the following files:
XPT2046_TouchscreenSOFTSPI.cpp
XPT2046_TouchscreenSOFTSPI.h
SoftSPI.cpp
SoftSPI.h
The Arduino sketch
Unzip the folder.
Moving the Library Files to the Sketch Folder
The previous folder already contains all the files to run the example. However, if you’re starting a project from scratch, you’ll need to move the library files to the sketch folder, as we’ll explain next.

Open Arduino IDE. Save the current sketch folder (even if empty). File > Save as… to save it to a desired location.

Go to
Sketch
>
Show Sketch Folder
. This will open your current sketch folder. You should move those four library files into this folder.

Your Arduino sketch folder should look like this.

After moving the files, restart Arduino IDE.
Example Sketch (Display + Touchscreen + MicroSD Card)
After having all the required library files in the sketch folder, you can write your Arduino sketch. This is the example that we’ll be running.
THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:
1) ESP32-2432S028R 2.8 inch 240×320 also known as the Cheap Yellow Display (CYD): https://makeradvisor.com/tools/cyd-cheap-yellow-display-esp32-2432s028r/
SET UP INSTRUCTIONS: https://ajvikalabs.com/cyd-lvgl/
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/
SET UP INSTRUCTIONS: https://ajvikalabs.com/esp32-tft-lvgl/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <SPI.h>
/* Install the "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
*** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Ajvika Labs ***
*** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM Ajvika Labs ***
FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://ajvikalabs.com/cyd-lvgl/ or https://ajvikalabs.com/esp32-tft-lvgl/ */
#include <TFT_eSPI.h>

// Copy the "XPT2046_TouchscreenSOFTSPI.h" library provided in the project page: https://ajvikalabs.com/esp32-cyd-display-touchscreen-microsd-card/
#include <XPT2046_TouchscreenSOFTSPI.h>

// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS

SoftSPI *tsSoftSpi;
// Set touch screen to use SOftSpi
XPT2046_TouchscreenSOFTSPI touchscreen(XPT2046_CS, XPT2046_IRQ);

/* Install the "lvgl" library version 9.X by kisvegabor to interface with the TFT Display - https://lvgl.io/
*** IMPORTANT: lv_conf.h available on the internet will probably NOT work with the examples available at Ajvika Labs ***
*** YOU MUST USE THE lv_conf.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM Ajvika Labs ***
FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://ajvikalabs.com/cyd-lvgl/ or https://ajvikalabs.com/esp32-tft-lvgl/ */
#include <lvgl.h>

#include "FS.h"
#include "SD.h"

// SD Reader pins (default VSPI pins)
#define SD_MISO 19
#define SD_MOSI 23
#define SD_SCK 18
#define SD_CS 5
SPIClass Sd_spi = SPIClass(VSPI);

#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320

// Touchscreen coordinates: (x, y) and pressure (z)
int x, y, z;

#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];

// If logging is enabled, it will inform the user about what is happening in the library
void log_print(lv_log_level_t level, const char * buf) {
LV_UNUSED(level);
Serial.prin

Technologies used

ESP32ArduinoBluetoothBLESPIArduino IDE

Hardware components

  • ESP32
  • Arduino
  • SD Card

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.