Build it step by step
0 of 7 doneTest the sound sensor
Print readings and clap.
Test the IR sensor
Wave a hand in front.
Test the player
Play each phrase.
Move the head
Sweep the servo.
Join them
Clap or wave to speak.
Add sleep
Rest after a minute.
Decorate
Build a cardboard robot around it.
How do you build a talking robot toy?
A cardboard robot that turns its head with a servo and says things when you clap or wave. You need Arduino Nano, SG90 Micro Servo, Sound sensor module (KY-038), IR obstacle avoidance sensor module, DFPlayer Mini MP3 module, Speaker 8 Ω 0.5 W, SanDisk microSD card 32 GB (A1, Class 10), Battery holder 4 × AA with switch, AA alkaline battery × 4 and Resistor 1 kΩ. It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 10
Parts list
- Development BoardsBuyArduino Nano × 1
- Motors & ActuatorsBuySG90 Micro Servo × 1
- SensorsBuy
- SensorsBuy
- InterfacesBuy
- Motors & ActuatorsBuy
- SBC AccessoriesBuy
- PowerBuy
- BatteriesBuy
- PassivesBuyResistor 1 kΩ × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Sound module VCC and GND | Nano 5V and GND | Power |
| Sound module AO | Nano A0 | Loudness reading |
| IR module VCC and GND | Nano 5V and GND | Power |
| IR module OUT | Nano D2 | Wave detect (LOW when something is near) |
| Servo signal | Nano D9 | Head turn |
| Servo plus and minus | 4 AA pack plus and minus | Servo power |
| DFPlayer TX | Nano D10 | Replies |
| DFPlayer RX | Nano D11 through a 1 kΩ resistor | Commands |
| DFPlayer SPK1 and SPK2 | Speaker | Voice |
| 4 AA pack | Nano VIN and GND; DFPlayer VCC to Nano 5V | Power |
Code
The robot listens for a clap with the sound sensor, which is compared with the room's normal level taken at start-up, and for a wave with the infrared sensor. Either one makes it turn its head to a random angle and play a random phrase from the SD card. It stays quiet for 2.5 seconds between phrases and goes to sleep after a minute. Record the phrases yourself, or use simple sounds, as short MP3 files. For a young child, enclose the batteries behind a cover that needs a screwdriver, keep the cardboard away from heat, and make sure small parts such as the servo horn cannot come off. Keep the volume moderate. Use only the AA pack, not a mains adapter, on a toy. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano 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).
// Talking robot. Libraries: Servo (built in), DFRobotDFPlayerMini. Board: Arduino Nano.
// SD card: phrase files 0001.mp3 to 0010.mp3 in the root folder.
#include <Servo.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
SoftwareSerial mp3(10, 11);
DFRobotDFPlayerMini player;
Servo head;
const int SND = A0, IR = 2;
unsigned long lastActive = 0, lastSpeak = 0;
int baseline = 0;
bool asleep = false;
void setup() {
pinMode(IR, INPUT);
head.attach(9); head.write(90);
mp3.begin(9600); player.begin(mp3); player.volume(20);
randomSeed(analogRead(A5));
long s = 0; for (int i = 0; i < 100; i++) { s += analogRead(SND); delay(5); }
baseline = s / 100; // the room's normal sound level
lastActive = millis();
}
void speak() {
if (millis() - lastSpeak < 2500) return; // do not talk over itself
head.write(random(60, 121)); // turn the head
player.play(random(1, 11));
lastSpeak = lastActive = millis(); asleep = false;
}
void loop() {
bool clap = abs(analogRead(SND) - baseline) > 200; // a loud, sudden sound
bool wave = digitalRead(IR) == LOW; // a hand in front
if (clap || wave) { speak(); }
if (!asleep && millis() - lastActive > 60000) { // sleep after a minute with no one around
head.write(90); asleep = true;
}
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Clap | Head turns and a phrase plays |
| Wave | The same |
| Quiet minute | Robot sleeps |
| Wave again | Wakes up |
If it does not work
Triggers by itself
Turn the sound module's screw down or raise the clap threshold.
No sound
Check the SD card and the 1 kΩ resistor.
Servo twitches
Power it from the AA pack with a common ground.
Resets
Use fresh batteries.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the talking robot toy?
Arduino Nano, SG90 Micro Servo, Sound sensor module (KY-038), IR obstacle avoidance sensor module, DFPlayer Mini MP3 module, Speaker 8 Ω 0.5 W, SanDisk microSD card 32 GB (A1, Class 10), Battery holder 4 × AA with switch, AA alkaline battery × 4 and Resistor 1 kΩ.
How long does the talking robot toy take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the talking robot toy works?
Clap: Head turns and a phrase plays. Wave: The same. Quiet minute: Robot sleeps.
Triggers by itself: what should I check?
Turn the sound module's screw down or raise the clap threshold.
No sound: what should I check?
Check the SD card and the 1 kΩ resistor.
Servo twitches: what should I check?
Power it from the AA pack with a common ground.
Resets: what should I check?
Use fresh batteries.
