Projects / Desk robot with expressive eyes

Desk robot with expressive eyes

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Draw one eye

    A rounded box.

  2. ESP32

    Add pupils

    Disc that glances around.

  3. ESP32

    Add blinking

    Random intervals.

  4. ESP32KY038

    Add the sound moods

    Wide eyes on loud noise.

  5. ESP32TTP223

    Add touch

    Happy arches.

  6. ESP32

    Add sleep

    After a minute.

  7. ESP32KY03859°

    Add the head turn

    Servo on loud sounds.

In short

How do you build a desk robot with expressive eyes?

A tiny desk robot shows animated eyes on an OLED, turns to sounds and reacts to touch. You need ESP32 DevKit V1, 1.3 inch OLED (SH1106, I2C), Sound sensor module (KY-038), Touch sensor module (TTP223), SG90 Micro Servo and USB A to micro-USB cable (data). It takes 7 steps and about 240 minutes.

Level
Intermediate
Time
About 240 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
OLED SDA and SCLESP32 GPIO21 and GPIO22I2C
OLED VCC and GNDESP32 3V3 and GNDPower
Sound module AOESP32 GPIO34Loudness
Sound module VCC and GNDESP32 3V3 and GNDPower
Touch module OUTESP32 GPIO27Touch
Touch module VCC and GNDESP32 3V3 and GNDPower
Servo signalESP32 GPIO18Head turn
Servo plus and minusESP32 VIN (5 V) and GNDServo power from the USB 5 V

Code

A robot becomes a character when it reacts, and the reactions here are only a few rules: blink now and then at random intervals, glance around, perk up with wide eyes and a head turn when a loud sound is heard, smile when touched, and fall asleep after a minute of being ignored. The eyes are drawn with a few shapes, so the code stays short, and the pupils drift one step per frame so the movement looks smooth. The sound module gives loudness, not direction, so the head turns by a random amount, which looks lively without pretending to find the source. Power the servo from the board's 5 V line and keep the head light, since a stalled servo draws a lot. Set the head so it cannot press against its stops. Keep it on a desk, away from small children, because the head turns by itself. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Desk robot: animated eyes on a 1.3 inch SH1106 OLED, perks up at loud sounds, smiles when touched, sleeps when ignored.
// Libraries: U8g2, ESP32Servo. Board: ESP32 Dev Module.
#include <U8g2lib.h>
#include <Wire.h>
#include <ESP32Servo.h>
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
Servo head;
const int SOUND = 34, TOUCH = 27, SERVO = 18;
enum Mood { NORMAL, HAPPY, ALERT, SLEEP };
Mood mood = NORMAL;
unsigned long moodSince = 0, lastStimulus = 0, nextBlink = 0, blinkUntil = 0;
int lookX = 0, targetX = 0;
int baseline = 0;
void drawEyes() {
  u8g2.clearBuffer();
  int cx[2] = {38, 90}, cy = 32;
  for (int i = 0; i < 2; i++) {
    if (mood == SLEEP || millis() < blinkUntil) { u8g2.drawBox(cx[i] - 16, cy - 1, 32, 3); continue; }
    if (mood == HAPPY) {                                            // happy arches
      for (int k = 0; k < 4; k++) u8g2.drawLine(cx[i] - 16, cy + 8 - k, cx[i], cy - 8 - k);
      for (int k = 0; k < 4; k++) u8g2.drawLine(cx[i], cy - 8 - k, cx[i] + 16, cy + 8 - k);
      continue;
    }
    int h = (mood == ALERT) ? 44 : 34;
    u8g2.drawRBox(cx[i] - 16, cy - h / 2, 32, h, 8);
    u8g2.setDrawColor(0);
    u8g2.drawDisc(cx[i] + lookX, cy, 6);                            // pupil
    u8g2.setDrawColor(1);
  }
  if (mood == SLEEP) { u8g2.setFont(u8g2_font_6x10_tf); u8g2.drawStr(100, 12, "z Z"); }
  u8g2.sendBuffer();
}
void setMood(Mood m) { mood = m; moodSince = millis(); }
void setup() {
  u8g2.begin();
  pinMode(TOUCH, INPUT);
  head.attach(SERVO); head.write(90);
  delay(500);
  baseline = analogRead(SOUND);
  lastStimulus = millis();
}
void loop() {
  int level = abs(analogRead(SOUND) - baseline);
  if (digitalRead(TOUCH)) { setMood(HAPPY); lastStimulus = millis(); }
  else if (level > 400 && mood != HAPPY) { setMood(ALERT); lastStimulus = millis(); head.write(random(60, 121)); }   // perks up and turns its head
  if ((mood == HAPPY || mood == ALERT) && millis() - moodSince > 2500) { setMood(NORMAL); head.write(90); }
  if (mood == NORMAL && millis() - lastStimulus > 60000UL) setMood(SLEEP);
  if (mood == SLEEP && millis() - lastStimulus < 60000UL) setMood(NORMAL);
  if (millis() > nextBlink) { blinkUntil = millis() + 120; nextBlink = millis() + random(2000, 6000); targetX = random(-8, 9); }
  lookX += (targetX > lookX) - (targetX < lookX);                   // glance around smoothly
  drawEyes();
  delay(30);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
IdleBlinks and glances every few seconds
ClapWide eyes and a head turn
Touch the padSmiling eyes
Left alone 60 sEyes close with z Z

If it does not work

Always wide-eyed

Raise the sound threshold.

Servo jitters

Power it from 5 V, not 3.3 V.

Blank display

Check the I2C address or the OLED type.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the desk robot with expressive eyes?

ESP32 DevKit V1, 1.3 inch OLED (SH1106, I2C), Sound sensor module (KY-038), Touch sensor module (TTP223), SG90 Micro Servo and USB A to micro-USB cable (data).

How long does the desk robot with expressive eyes take to build?

About 240 minutes for a intermediate maker, in 7 steps.

How do I know the desk robot with expressive eyes works?

Idle: Blinks and glances every few seconds. Clap: Wide eyes and a head turn. Touch the pad: Smiling eyes.

Always wide-eyed: what should I check?

Raise the sound threshold.

Servo jitters: what should I check?

Power it from 5 V, not 3.3 V.

Blank display: what should I check?

Check the I2C address or the OLED type.