Pets
Desk cat with moods
A small cat whose face is drawn on a screen. Touch its head and it is happy and meows; leave it alone and it dozes off.
- Medium
- about 60 min
- ESP32-S3
- 8 parts
- 12 wires
- 107 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The face is not a picture file. It is drawn from circles, triangles and lines, so changing a mood means changing a few numbers. This is the gentlest way into screen graphics.
What you learn
Drawing shapes on a colour screen, reading a touch sensor, and making sound with tone().
How it works
input
Touch sensor (head)
decides
ESP32-S3
output
TFT 1.54 in
Buzzer
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 12
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
desk-cat.ino
// Desk cat with moods
// Touch its head and it is happy and meows. Leave it alone and it falls asleep.
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
const int PIN_TFT_SCL = 12;
const int PIN_TFT_SDA = 11;
const int PIN_TFT_RES = 14;
const int PIN_TFT_DC = 13;
const int PIN_TFT_CS = 10;
const int PIN_TOUCH_IO = 4;
const int PIN_BUZZER_POS = 17;
const unsigned long SLEEPY_AFTER_MS = 20000; // no touch for this long: sleepy
Adafruit_ST7789 tft(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RES);
const uint16_t FUR = 0xFD20; // orange
const uint16_t DARK = 0x0000;
const uint16_t PINK = 0xFB56;
enum Mood { CALM, HAPPY, SLEEPY };
Mood mood = CALM;
unsigned long lastTouch = 0;
void drawFace(Mood m) {
tft.fillScreen(DARK);
// ears
tft.fillTriangle(40, 90, 70, 20, 105, 75, FUR);
tft.fillTriangle(200, 90, 170, 20, 135, 75, FUR);
// head
tft.fillCircle(120, 135, 85, FUR);
// eyes
if (m == HAPPY) {
tft.fillCircle(85, 120, 16, DARK);
tft.fillCircle(155, 120, 16, DARK);
tft.fillCircle(85, 128, 16, FUR); // cut the bottom: a smiling arc
tft.fillCircle(155, 128, 16, FUR);
} else if (m == SLEEPY) {
tft.fillRect(68, 120, 34, 5, DARK);
tft.fillRect(138, 120, 34, 5, DARK);
} else {
tft.fillCircle(85, 120, 12, DARK);
tft.fillCircle(155, 120, 12, DARK);
tft.fillCircle(89, 116, 4, 0xFFFF);
tft.fillCircle(159, 116, 4, 0xFFFF);
}
// nose and mouth
tft.fillTriangle(112, 150, 128, 150, 120, 160, PINK);
tft.drawLine(120, 160, 106, 172, DARK);
tft.drawLine(120, 160, 134, 172, DARK);
// whiskers
tft.drawLine(60, 155, 20, 145, DARK);
tft.drawLine(60, 165, 20, 172, DARK);
tft.drawLine(180, 155, 220, 145, DARK);
tft.drawLine(180, 165, 220, 172, DARK);
if (m == SLEEPY) {
tft.setTextColor(0xFFFF);
tft.setTextSize(3);
tft.setCursor(190, 30);
tft.print("z");
}
}
void meow() {
for (int f = 700; f < 1200; f += 25) {
tone(PIN_BUZZER_POS, f);
delay(8);
}
for (int f = 1200; f > 600; f -= 20) {
tone(PIN_BUZZER_POS, f);
delay(10);
}
noTone(PIN_BUZZER_POS);
}
void setMood(Mood m) {
if (m == mood) return;
mood = m;
drawFace(mood);
}
void setup() {
pinMode(PIN_TOUCH_IO, INPUT);
pinMode(PIN_BUZZER_POS, OUTPUT);
SPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
tft.init(240, 240);
tft.setRotation(2);
drawFace(mood);
lastTouch = millis();
}
void loop() {
if (digitalRead(PIN_TOUCH_IO) == HIGH) {
lastTouch = millis();
if (mood != HAPPY) {
setMood(HAPPY);
meow();
}
} else if (millis() - lastTouch > SLEEPY_AFTER_MS) {
setMood(SLEEPY);
} else if (millis() - lastTouch > 3000) {
setMood(CALM);
}
delay(30);
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit ST7735 and ST7789 Library: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.