Pets
Desk companion robot
A little robot whose eyes blink and glance about. Pat its head and it is pleased; pat it three times quickly and it goes dizzy.
- Medium
- about 55 min
- ESP32-S3
- 8 parts
- 12 wires
- 104 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
It feels alive because it does small things at random moments. A blink every few seconds, never at the same interval, is enough to fool the eye.
What you learn
Simple animation, random timing, and spotting a pattern of taps.
How it works
input
Touch sensor (top of 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-robot.ino
// Desk companion robot
// Its eyes blink and glance about. Pat its head and it is pleased; pat it
// three times quickly and it gets dizzy.
#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;
Adafruit_ST7789 tft(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RES);
const uint16_t EYE = 0x07FF; // cyan
const int EYE_Y = 110;
unsigned long nextBlink = 0;
unsigned long nextGlance = 0;
int glance = 0; // -1 left, 0 centre, 1 right
bool wasTouched = false;
unsigned long tapTimes[3] = {0, 0, 0};
void drawEyes(int look, bool closed, bool pleased) {
tft.fillRect(0, 50, 240, 130, ST77XX_BLACK);
for (int side = -1; side <= 1; side += 2) {
int x = 120 + side * 55 + look * 14;
if (closed) {
tft.fillRoundRect(x - 32, EYE_Y - 4, 64, 8, 4, EYE);
} else if (pleased) {
tft.fillCircle(x, EYE_Y + 6, 32, EYE);
tft.fillCircle(x, EYE_Y + 22, 34, ST77XX_BLACK); // leaves an arch: a smiling eye
} else {
tft.fillRoundRect(x - 30, EYE_Y - 38, 60, 76, 18, EYE);
}
}
}
void drawDizzy() {
tft.fillRect(0, 50, 240, 130, ST77XX_BLACK);
for (int side = -1; side <= 1; side += 2) {
int x = 120 + side * 55;
for (int r = 6; r <= 30; r += 8) tft.drawCircle(x, EYE_Y, r, EYE);
}
}
void chirp(int from, int to) {
int step = from < to ? 40 : -40;
for (int f = from; f != to; f += step) {
tone(PIN_BUZZER_POS, f);
delay(12);
}
noTone(PIN_BUZZER_POS);
}
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);
tft.fillScreen(ST77XX_BLACK);
drawEyes(0, false, false);
nextBlink = millis() + 3000;
nextGlance = millis() + 5000;
}
void loop() {
bool touched = digitalRead(PIN_TOUCH_IO) == HIGH;
if (touched && !wasTouched) {
tapTimes[0] = tapTimes[1];
tapTimes[1] = tapTimes[2];
tapTimes[2] = millis();
bool threeQuick = tapTimes[0] != 0 && tapTimes[2] - tapTimes[0] < 1500;
if (threeQuick) {
drawDizzy();
chirp(1600, 400);
delay(1500);
tapTimes[0] = tapTimes[1] = tapTimes[2] = 0;
} else {
drawEyes(0, false, true);
chirp(800, 1600);
delay(700);
}
drawEyes(glance, false, false);
}
wasTouched = touched;
if (millis() > nextBlink) {
drawEyes(glance, true, false);
delay(120);
drawEyes(glance, false, false);
nextBlink = millis() + random(2500, 6000);
}
if (millis() > nextGlance) {
glance = random(-1, 2);
drawEyes(glance, false, false);
nextGlance = millis() + random(3000, 8000);
}
delay(20);
}
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”.