Week 10: Output Devices

A.k.a. teaching tiny blue servos to pull strings 🧵🎭


🪛 Overview

My output devices are SG90 micro servos, the motors that pull the strings of my final project puppet, Sandy. I started with one servo on an Arduino, moved to the ESP32, and ended up driving all the joints through a PCA9685 servo driver connected to my custom XIAO ESP32-S3 board.

  • • Step 1: one servo on an Arduino, with "moods" sent over Serial
  • • Step 2: one, then two servos straight from the ESP32
  • • Step 3: all the joints through the PCA9685, from my own board

👥 Group Assignment

The group assignment was to measure the power consumption of an output device.

🔗 View Group Assignment Documentation

⚙️ How a Servo Works

A hobby servo has a small motor, a gearbox and a position sensor inside. You don't tell it to "spin"; you tell it which angle to go to. It listens for a pulse every 20 ms (50 Hz), and the width of that pulse sets the angle:

Pulse widthAngle
~0.5 ms0°
~1.5 ms90°
~2.5 ms180°

The SG90 has 3 wires: brown (GND), red (+5V) and orange (signal).

1️⃣ One Servo, Three Moods

I started on an Arduino Uno with the Servo library. To explore how movement can show emotion (which is the whole point of a puppet), I sent a mood over Serial and the servo acted it out:

  • • CALM: slow sway between 70° and 110°
  • • HAPPY: quick bounces between 60° and 120°
  • • ANGRY: random, jerky jumps anywhere between 20° and 160°
else if (incomingData == "ANGRY") {
  for (int i = 0; i < 8; i++) {
    int randomPos = random(20, 160);
    myServo.write(randomPos);
    delay(random(50, 150));
  }
}

2️⃣ Moving to the ESP32

The ESP32 doesn't use the Arduino Servo library, so I generated the PWM signal myself. First with the ESP32's LEDC PWM peripheral at 50 Hz with 8-bit resolution, where a duty value of 6, 19 and 32 gave left, centre and right:

const int servoPin = 18;

void setup() {
  ledcAttachChannel(servoPin, 50, 8, 0);   // 50 Hz, 8-bit
}

void loop() {
  ledcWrite(servoPin, 6);   delay(1000);   // LEFT
  ledcWrite(servoPin, 19);  delay(1000);   // CENTER
  ledcWrite(servoPin, 32);  delay(1000);   // RIGHT
}

Then two servos, by writing the pulses by hand: map the angle to 500–2500 µs, set the pin HIGH for that long, then LOW. I reused the calm / happy / angry idea with the two servos moving against each other.

void writeServo(int pin, int angle) {
  int pulseWidth = map(angle, 0, 180, 500, 2500);
  digitalWrite(pin, HIGH);
  delayMicroseconds(pulseWidth);
  digitalWrite(pin, LOW);
  delay(20);
}
Testing servos in a cardboard mock-up
Swapping my hands for servo motors on the prototype puppet. Thankfully this worked *^____^*

3️⃣ All the Joints: PCA9685

Generating pulses by hand doesn't scale: the board is stuck waiting for each pulse. So for the final project I used a PCA9685, a 16-channel, 12-bit PWM driver. The XIAO just tells it the positions over I²C, and the PCA9685 keeps all the servo signals running by itself.

Servos laid out on cardboard to map the puppet's joints
Mapping out the servos on cardboard first. The final version uses 5: head, both wrists and both knees

🔹 Wiring

XIAO ESP32-S3PCA9685
SDA (D4)SDA
SCL (D5)SCL
3.3VVCC (logic)
5VV+ (servo power)
GNDGND (shared)
Custom XIAO ESP32-S3 PCB
My custom XIAO ESP32-S3 board, which drives the PCA9685

🔹 Channel map (final version)

JointPCA9685 channel
Right wristCH2
Left wristCH13
Left kneeCH15
Right kneeCH0
HeadCH5

🔹 The code

The PCA9685 runs at 50 Hz, and each period is split into 4096 steps. A pulse of 150 steps is 0° and 600 steps is 180°, so an angle is just mapped onto that range. smoothMove() then steps 1° at a time so the strings don't get yanked.

#define SERVO_FREQ      50
#define SERVO_MIN_PULSE 150   // 0°
#define SERVO_MAX_PULSE 600   // 180°

int angleToPulse(int angle) {
  return map(angle, 0, 180, SERVO_MIN_PULSE, SERVO_MAX_PULSE);
}

void setServo(int ch, int angle) {
  angle = constrain(angle, 0, 180);
  pca.setPWM(ch, 0, angleToPulse(angle));
  currentAngles[ch] = angle;
}

void smoothMove(int ch, int target, int stepDelay = 10) {
  int current = currentAngles[ch];
  int step = (target > current) ? 1 : -1;
  while (current != target) {
    current += step;
    setServo(ch, current);
    delay(stepDelay);
  }
}

Each joint uses three positions: limp (0°, string slack), neutral (90°) and full (180°, string pulled). Poses like wave, bow, nod and dance are just sequences of smoothMove() calls.

Mounting and wiring up the servos
The servos pulling Sandy's strings on stage 🎭

🔋 Powering the Servos

Servos draw a lot more current than the microcontroller, especially when several move at once. So the servos get their own external 5V supply on the PCA9685's V+ pin, while the XIAO only powers the PCA9685's logic (3.3V on VCC). All grounds are shared so the signals have the same reference. The full power budget is in Week 15 · System Integration.

✨ What I Learned

  • • A servo is controlled by pulse width, not by voltage or speed
  • • The same motor can look calm, happy or angry depending only on timing and range
  • • A dedicated driver like the PCA9685 frees the microcontroller to do other things (like run Wi-Fi)
  • • Servos need their own power supply and a shared ground
  • • Moving 1° at a time looks much more natural than jumping straight to the target

📁 Design Files

Arduino: one servo, three moods (.ino)
ESP32: one servo with LEDC (.ino)
ESP32: two servos, manual pulses (.ino)
Final: PCA9685 puppet controller (.ino)