Talking skull on an Arduino Uno
A PIR wakes a skull: its eyes light and a servo jaw follows a hand-timed cue list in step with a DFPlayer track, until BUSY says the line is over. The whole build, an Arduino Uno and 9 more parts, 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.
Intermediate About 40 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the Halloween Prop Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $66, free on Mokxi.
What you'll learn
- Detecting motion with a PIR sensor
- Playing a track with a DFPlayer Mini over serial
- Scripting a servo with a list of timed cues
- Using the BUSY pin to know when audio ends
- Dimming LEDs with
analogWrite
- Difficulty: Intermediate
- Time: About 40 minutes
- Board: Arduino Uno R3
- Parts: 11
How it works
When it starts, the skull says hello once. After that, a PIR motion sensor on pin 2 wakes it, but only after 10 seconds of quiet since the last visitor. The eyes (two LEDs on pins 5 and 6) go to full brightness and the DFPlayer plays track 1.
The jaw servo on pin 9 follows a hand-timed cue list called SCRIPT. Each cue gives a time in milliseconds and a jaw angle. It is not tied to the audio itself.
When the cues are used up and the DFPlayer's BUSY pin says the track is over, the jaw shuts and the eyes dim to a low glow.
Parts list
11 parts, plus the jumper wires. They retail for about $40; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × DFPlayer Mini MP3 moduleIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Motion sensorIn Halloween Prop Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × Speaker, 8 ohmIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 1 | Arduino Uno R3 pin 11 |
| DFPlayer Mini MP3 module pin TX | Arduino Uno R3 pin 10 |
| Hobby servo pin PWM | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 5 |
| DFPlayer Mini MP3 module pin BUSY | Arduino Uno R3 pin 3 |
| Motion sensor pin OUT | Arduino Uno R3 pin 2 |
| DFPlayer Mini MP3 module pin VCC | Arduino Uno R3 pin 5V |
| Motion sensor pin VCC | Arduino Uno R3 pin 5V |
| Hobby servo pin VCC | Arduino Uno R3 pin 5V |
| DFPlayer Mini MP3 module pin GND | Arduino Uno R3 pin GND |
| LED, green (1) pin C | Arduino Uno R3 pin GND |
| LED, green (2) pin C | Arduino Uno R3 pin GND |
| Motion sensor pin GND | Arduino Uno R3 pin GND |
| Hobby servo pin GND | Arduino Uno R3 pin GND |
| Resistor, 1k Ω pin 2 | DFPlayer Mini MP3 module pin RX |
| Speaker, 8 ohm pin 1 | DFPlayer Mini MP3 module pin SPK_1 |
| Speaker, 8 ohm pin 2 | DFPlayer Mini MP3 module pin SPK_2 |
| LED, green (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, green (2) pin A | Resistor, 220 Ω (2) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Talking skull: a PIR sees a trick-or-treater, the skull's eyes light up and
// its jaw moves in time with a spoken line played by a DFPlayer Mini.
//
// PIR VCC -> 5 V GND -> GND OUT -> pin 2 (high while it sees movement)
// jaw servo PWM -> pin 9, VCC -> 5 V, GND -> GND
// DFPlayer VCC -> 5 V GND -> GND TX -> pin 10 RX <- 1 k <- pin 11
// BUSY -> pin 3 (low while a track plays), speaker on SPK_1 and SPK_2
// eyes two green LEDs on pins 5 and 6, each through 220 ohms to GND
//
// How the jaw keeps time. There are two honest ways to sync a jaw to a voice.
//
// 1. Follow the audio. Rectify and smooth the speaker signal into an
// envelope, read it on an analog pin, and open the jaw as far as the
// voice is loud. Ready-made "audio servo driver" boards do exactly this.
// It works with any recording, but the jaw chatters on every breath.
// 2. Script it. Listen to the track once, note when each syllable starts,
// and write those moments down as cues. It takes ten minutes per line and
// looks crisp, because you decide when the mouth shuts.
//
// This sketch does the second. SCRIPT below is the cue list for track 001:
// "Welcome... to my HAUNTED house! Heh heh heh heh." Each cue says when, in
// milliseconds from the start of the track, the jaw should be at what angle.
// The simulated DFPlayer plays silence, so there is no audio to follow here;
// a scripted jaw moves exactly as it will on the porch.
//
// The track's real end comes from BUSY, not from the script, so a longer
// recording never leaves the jaw hanging open.
//
// On the bench: a PIR needs about a minute after power-up to settle, and
// fires at random until it has. Give the servo its own 5 V supply with its
// ground joined to the Uno's.
//
// Things to try. Click the PIR. Record your own line, time its syllables, and
// replace SCRIPT. Make the eyes flare on the loud syllables: the cue's angle
// is a good stand-in for how loud it is.
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>
const int PIR_PIN = 2;
const int BUSY_PIN = 3;
const int JAW_PIN = 9;
const int EYE_LEFT = 5;
const int EYE_RIGHT = 6;
const int JAW_SHUT = 5; // degrees
const unsigned long REST_MS = 10000; // quiet time between visitors
struct Cue {
uint16_t at; // ms from the start of the track
uint8_t angle; // jaw angle, JAW_SHUT is closed
};
// "Wel-come... to my HAUN-TED house! Heh heh heh heh."
const Cue SCRIPT[] = {
{0, 30}, {200, 5}, {320, 35}, {560, 5}, // wel-come
{900, 20}, {1040, 5}, {1120, 25}, {1260, 5}, // to my
{1450, 45}, {1700, 10}, {1780, 40}, {1980, 5}, // HAUN-TED
{2100, 35}, {2450, 5}, // house!
{2700, 30}, {2850, 5}, {2950, 30}, {3100, 5}, // heh heh
{3200, 30}, {3350, 5}, {3450, 30}, {3600, 5}, // heh heh
};
const int CUES = sizeof(SCRIPT) / sizeof(SCRIPT[0]);
SoftwareSerial link(10, 11); // RX, TX
DFRobotDFPlayerMini player;
Servo jaw;
bool haveSound = false;
bool talking = false;
unsigned long started = 0;
unsigned long quietSince = 0;
int nextCue = 0;
unsigned int visitors = 0;
void eyes(int level) {
analogWrite(EYE_LEFT, level);
analogWrite(EYE_RIGHT, level);
}
void speak() {
talking = true;
started = millis();
nextCue = 0;
eyes(255);
if (haveSound) player.play(1);
visitors++;
Serial.print("Talking to visitor ");
Serial.println(visitors);
}
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(BUSY_PIN, INPUT);
jaw.attach(JAW_PIN);
jaw.write(JAW_SHUT);
haveSound = player.begin(link);
if (haveSound) {
player.volume(24);
} else {
Serial.println("No DFPlayer answered: the skull will mime");
}
Serial.println("Skull ready, saying hello once");
speak();
}
void loop() {
unsigned long now = millis();
if (talking) {
unsigned long t = now - started;
// Play every cue whose moment has come.
while (nextCue < CUES && t >= SCRIPT[nextCue].at) {
jaw.write(SCRIPT[nextCue].angle);
nextCue++;
}
// Done when the script is spent and the player says the track is over
// (BUSY goes high). Without a player, the script alone decides.
bool playing = haveSound && digitalRead(BUSY_PIN) == LOW;
if (nextCue >= CUES && !playing && t > 500) {
talking = false;
quietSince = now;
jaw.write(JAW_SHUT);
eyes(20);
Serial.println("Done, waiting for the next visitor");
}
return;
}
if (now - quietSince >= REST_MS && digitalRead(PIR_PIN) == HIGH) {
speak();
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
eyes sketch.ino
Sets both eye LEDs to the same brightness with analogWrite, from 0 to 255. The skull uses 255 while talking and 20 while waiting.
void eyes(int level) {
analogWrite(EYE_LEFT, level);
analogWrite(EYE_RIGHT, level);
}speak sketch.ino
Starts a visit. It records the start time, resets the cue position, lights the eyes fully, and plays track 1 if a DFPlayer answered. It also counts the visitor and prints a message.
void speak() {
talking = true;
started = millis();
nextCue = 0;
eyes(255);
if (haveSound) player.play(1);
visitors++;
Serial.print("Talking to visitor ");
Serial.println(visitors);
}setup sketch.ino
Starts the serial ports, parks the jaw at JAW_SHUT, and tries to start the DFPlayer. If it does not answer, the skull will mime without sound. Then it calls speak once to say hello.
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(BUSY_PIN, INPUT);
jaw.attach(JAW_PIN);
jaw.write(JAW_SHUT);
haveSound = player.begin(link);
if (haveSound) {
player.volume(24);
} else {
Serial.println("No DFPlayer answered: the skull will mime");
}
Serial.println("Skull ready, saying hello once");
speak();
}loop sketch.ino
While talking, it moves the jaw to each cue whose time has come. When the script is done, the track has finished and half a second has passed, it stops talking. Otherwise it starts again when the rest time is over and the PIR is high.
void loop() {
unsigned long now = millis();
if (talking) {
unsigned long t = now - started;
// Play every cue whose moment has come.
while (nextCue < CUES && t >= SCRIPT[nextCue].at) {
jaw.write(SCRIPT[nextCue].angle);
nextCue++;
}
// Done when the script is spent and the player says the track is over
// (BUSY goes high). Without a player, the script alone decides.
bool playing = haveSound && digitalRead(BUSY_PIN) == LOW;
if (nextCue >= CUES && !playing && t > 500) {
talking = false;
quietSince = now;
jaw.write(JAW_SHUT);
eyes(20);
Serial.println("Done, waiting for the next visitor");
}
return;
}
if (now - quietSince >= REST_MS && digitalRead(PIR_PIN) == HIGH) {
speak();
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The skull talks at random after power-up This project
A real PIR needs about a minute to settle after power-up. Wait before testing. - The serial monitor says No DFPlayer answered This project
Check that the DFPlayer's TX goes to pin 10, and RX goes through the 1k resistor to pin 11. - The jaw does not match your own recording This project
EditSCRIPT. Listen to the track, note when each syllable starts, and write each time and angle as a cue. - The module stays silent or does not respond to commands. DFPlayer Mini MP3 module
Format the microSD card as FAT32 and name files 0001.mp3, 0002.mp3 and so on. Copy them in the order you want. Check the card is seated in the slot. - Commands are garbled or ignored on the serial link. DFPlayer Mini MP3 module
The module uses 9600 baud and 3.3 V logic on RX. Put a 1 k resistor in series with the Arduino TX line to its RX. Cross the lines: Arduino TX to module RX. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.