Built-in project · LilyGO TTGO T-Display

T-Display hello

The TTGO T-Display writes on its 135x240 screen with LilyGO’s TFT_eSPI setup and counts presses of its two buttons. The whole build, a TTGO T-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.

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

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

The circuit runs here once the page has loaded.

What you'll learn

  • Drawing text and shapes with TFT_eSPI
  • Active-low buttons
  • Detecting a new press, not a held button
  • Redrawing only the part that changes
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: LilyGO TTGO T-Display
  • Parts: 1

How it works

The TTGO T-Display has a 135x240 screen and two buttons on the board, so there is nothing to wire. The sketch draws "T-Display" in cyan with a magenta bar under it, then shows two counters, left and right.

The buttons are on GPIO 0 and GPIO 35, and both read LOW when pressed. The sketch counts a press only when a button goes from up to down, so holding does not keep counting. Each press updates the screen and prints a message to serial.

Parts list

1 part, plus the jumper wires. They retail for about $12; 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 · LilyGO TTGO T-Display
// Hello: the T-Display's screen through TFT_eSPI, with LilyGO's setup built
// in, and its two buttons: GPIO 0 and GPIO 35, LOW when pressed.

#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

int left = 0;
int right = 0;
bool leftWas = false;
bool rightWas = false;

void drawCounts()
{
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.setTextPadding(100);
    tft.drawString(String("left  ") + left, 20, 80, 4);
    tft.drawString(String("right ") + right, 20, 110, 4);
}

void setup()
{
    Serial.begin(115200);
    pinMode(0, INPUT_PULLUP);
    pinMode(35, INPUT);

    tft.init();
    tft.setRotation(1); // landscape, 240 wide
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_CYAN, TFT_BLACK);
    tft.drawString("T-Display", 20, 20, 4);
    tft.fillRect(20, 54, 200, 4, TFT_MAGENTA);
    drawCounts();
}

void loop()
{
    bool l = digitalRead(0) == LOW;
    bool r = digitalRead(35) == LOW;
    if (l && !leftWas) {
        left++;
        Serial.println("left button");
        drawCounts();
    }
    if (r && !rightWas) {
        right++;
        Serial.println("right button");
        drawCounts();
    }
    leftWas = l;
    rightWas = r;
    delay(20);
}

Code walkthrough

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

drawCounts sketch.ino

Draws the two lines "left" and "right" with their counts. Text padding of 100 pixels erases the old digits first, so a shorter number does not leave ghost characters behind.

void drawCounts()
{
    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.setTextPadding(100);
    tft.drawString(String("left  ") + left, 20, 80, 4);
    tft.drawString(String("right ") + right, 20, 110, 4);
}

setup sketch.ino

Starts serial at 115200, sets GPIO 0 as INPUT_PULLUP and GPIO 35 as INPUT, and starts the screen with tft.init(). It sets landscape rotation, clears to black, draws the title and bar, and calls drawCounts.

void setup()
{
    Serial.begin(115200);
    pinMode(0, INPUT_PULLUP);
    pinMode(35, INPUT);

    tft.init();
    tft.setRotation(1); // landscape, 240 wide
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_CYAN, TFT_BLACK);
    tft.drawString("T-Display", 20, 20, 4);
    tft.fillRect(20, 54, 200, 4, TFT_MAGENTA);
    drawCounts();
}

loop sketch.ino

Reads both buttons, where LOW means pressed. If a button is pressed now but was not last time, it adds one to that count, prints a message and redraws. It then remembers the state for next time and waits 20 ms.

void loop()
{
    bool l = digitalRead(0) == LOW;
    bool r = digitalRead(35) == LOW;
    if (l && !leftWas) {
        left++;
        Serial.println("left button");
        drawCounts();
    }
    if (r && !rightWas) {
        right++;
        Serial.println("right button");
        drawCounts();
    }
    leftWas = l;
    rightWas = r;
    delay(20);
}

Troubleshooting

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

  • A single press counts many times This project
    The sketch compares l with leftWas, and r with rightWas. Keep updating those at the end of loop.
  • Old digits stay on the screen This project
    Keep tft.setTextPadding(100) so each redraw clears the old text.
  • The screen stays blank or garbled. LilyGO TTGO T-Display
    Configure TFT_eSPI for the TTGO T-Display in User_Setup_Select.h. The backlight is on GPIO 4. Pick the right ESP32 board in the IDE.
  • A button or pin does not work as expected. LilyGO TTGO T-Display
    The two buttons are on GPIO 35 and GPIO 0. GPIO 35 is input only and has no internal pull-up. GPIO 0 held at power up enters download mode. Pins are 3.3 V.

Change it and keep it

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