Built-in project · Cheap Yellow Display (ESP32-2432S028R)

CYD touch paint

Draw on the Cheap Yellow Display with a finger: the XPT2046 touch film on its own SPI pins, a palette and a clear button. The whole build, a Cheap Yellow Display, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $14. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Two SPI buses on one board
  • Reading a resistive touch controller
  • Mapping raw readings to screen pixels
  • Drawing shapes with TFT_eSPI
  • Hit-testing touches against buttons
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Cheap Yellow Display (ESP32-2432S028R)
  • Parts: 1

How it works

The Cheap Yellow Display has a 320 by 240 screen and a touch film. The screen is driven by TFT_eSPI. The touch controller, an XPT2046, sits on its own SPI bus on GPIO 25, 32, 39 and 33, so the sketch starts a separate SPI object for it.

On start it draws a strip across the top: four color swatches and a gray clear button. When you touch the glass, the raw reading is mapped to screen coordinates. A touch in the top 30 pixels picks a color or clears the canvas. Anywhere lower, it paints a small circle in the current color.

Parts list

1 part, plus the jumper wires. They retail for about $14; on Mokxi they're free.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Cheap Yellow Display (ESP32-2432S028R)
// Touch paint: draw on the Cheap Yellow Display with a finger.
//
// The screen is TFT_eSPI with the board's setup. The touch controller, an
// XPT2046, is not on the screen's bus: the board wires it to GPIO 25 (CLK),
// 32 (DIN), 39 (DO) and 33 (CS), so it gets an SPI bus of its own, the way
// the CYD tutorials do it. Press the screen while the sketch runs to draw.

#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>

#define XPT2046_IRQ 36
#define XPT2046_MOSI 32
#define XPT2046_MISO 39
#define XPT2046_CLK 25
#define XPT2046_CS 33

SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
TFT_eSPI tft = TFT_eSPI();

const int kWidth = 320;
const int kHeight = 240;
const uint16_t kInks[4] = {TFT_YELLOW, TFT_CYAN, TFT_MAGENTA, TFT_GREEN};
int ink = 0;

void drawPalette()
{
    for (int i = 0; i < 4; i++) {
        tft.fillRect(i * 40, 0, 40, 30, kInks[i]);
    }
    tft.fillRect(160, 0, 160, 30, TFT_DARKGREY);
    tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
    tft.drawCentreString("clear", 240, 8, 2);
}

void setup()
{
    Serial.begin(115200);
    touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
    touchscreen.begin(touchscreenSPI);
    touchscreen.setRotation(1);

    tft.init();
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    drawPalette();
}

void loop()
{
    if (!touchscreen.tirqTouched() || !touchscreen.touched()) return;
    TS_Point p = touchscreen.getPoint();
    // The raw readings run about 200 to 3700 across the glass.
    int x = map(p.x, 200, 3700, 0, kWidth - 1);
    int y = map(p.y, 240, 3800, 0, kHeight - 1);
    x = constrain(x, 0, kWidth - 1);
    y = constrain(y, 0, kHeight - 1);
    if (y < 30) {
        if (x < 160) {
            ink = x / 40;
        } else {
            tft.fillRect(0, 30, kWidth, kHeight - 30, TFT_BLACK);
        }
        delay(150);
        return;
    }
    tft.fillCircle(x, y, 3, kInks[ink]);
}

Code walkthrough

The key functions of the sketch, and what each one does.

drawPalette sketch.ino

Draws the strip at the top of the screen. A loop fills four 40 by 30 rectangles using the colors in kInks. Then it fills a dark gray rectangle on the right and writes the word clear in it. loop relies on these same positions to decide what a touch means.

void drawPalette()
{
    for (int i = 0; i < 4; i++) {
        tft.fillRect(i * 40, 0, 40, 30, kInks[i]);
    }
    tft.fillRect(160, 0, 160, 30, TFT_DARKGREY);
    tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
    tft.drawCentreString("clear", 240, 8, 2);
}

setup sketch.ino

Starts serial, then the touch SPI bus on its own pins and the touchscreen on it. It then starts the display, sets rotation 1 for landscape, clears to black and calls drawPalette. Both the touch and the screen use rotation 1 so their coordinates agree.

void setup()
{
    Serial.begin(115200);
    touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
    touchscreen.begin(touchscreenSPI);
    touchscreen.setRotation(1);

    tft.init();
    tft.setRotation(1);
    tft.fillScreen(TFT_BLACK);
    drawPalette();
}

loop sketch.ino

Returns early unless the screen is touched. It maps the raw point to 0 to 319 by 0 to 239 with map and constrain. In the top 30 pixels the left half picks an ink and the right half clears the canvas, then it waits 150 ms. Elsewhere it draws a filled circle of radius 3.

void loop()
{
    if (!touchscreen.tirqTouched() || !touchscreen.touched()) return;
    TS_Point p = touchscreen.getPoint();
    // The raw readings run about 200 to 3700 across the glass.
    int x = map(p.x, 200, 3700, 0, kWidth - 1);
    int y = map(p.y, 240, 3800, 0, kHeight - 1);
    x = constrain(x, 0, kWidth - 1);
    y = constrain(y, 0, kHeight - 1);
    if (y < 30) {
        if (x < 160) {
            ink = x / 40;
        } else {
            tft.fillRect(0, 30, kWidth, kHeight - 30, TFT_BLACK);
        }
        delay(150);
        return;
    }
    tft.fillCircle(x, y, 3, kInks[ink]);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Paint appears in the wrong place This project
    The map ranges are the raw touch limits. If the glass reads differently, adjust 200 to 3700 and 240 to 3800 for your panel.
  • Colors work but clear does nothing This project
    Clear is the right half of the top strip, so x must be 160 or more and y under 30.
  • The screen stays dark or shows white. Cheap Yellow Display (ESP32-2432S028R)
    The backlight is on a GPIO pin you must turn on, and the display needs the right driver and pins in its setup file. Use a library configuration made for this board.
  • The RGB LED lights up the opposite way you expect. Cheap Yellow Display (ESP32-2432S028R)
    The onboard RGB LED is active LOW, so write LOW to turn a color on and HIGH to turn it off. Some pins are shared with other board functions.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.