Build a Robot Friend Who Listens, Talks, and Walks!
A complete, kid-friendly guide to building your own talking robot using a Raspberry Pi 5, a USB microphone and speaker, 8 servo motors for arms and legs, and Google's Gemini AI for a real brain.
1What You're Building
This project turns a Raspberry Pi 5 into the brain of a small robot that can hear a question through a USB microphone, think of an answer using Google's Gemini API, speak the answer out loud through a USB speaker, and move its arms and legs using 8 servo motors — that's 8 degrees of freedom (DOF). Say "wave hello" or "take a step" and it moves. Ask "why is the sky blue?" and it answers, out loud, in its own voice.
Raspberry Pi 5 running Python
USB microphone + speech recognition
Gemini API + USB speaker
8 servos for arms & legs
2Meet the 8 Moving Joints (8 DOF)
"Degrees of freedom" just means the number of joints your robot can move on its own. This robot has 8: one shoulder and one elbow on each arm, and one hip and one knee on each leg.
3Shopping List
Prices are rough US estimates and will vary by store and country. Ask an adult to help order the electronics.
| Item | Qty | Approx. price | Why you need it |
|---|---|---|---|
| Raspberry Pi 5 (4GB or 8GB) | 1 | $60–80 | The robot's brain — runs everything |
| MicroSD card, 32GB Class 10 | 1 | $8 | Stores the operating system |
| USB microphone (clip-on/mini) | 1 | $10 | Lets the robot hear you |
| USB speaker (Pi 5 has no audio jack) | 1 | $10 | Lets the robot talk back |
| PCA9685 16-channel servo driver | 1 | $8 | Controls all 8 servos smoothly over I2C |
| Micro servo motors (SG90 / MG90S) | 8 | $2–4 each | Move each arm and leg joint |
| External 5–6V, 3A+ power supply | 1 | $10 | Powers the 8 servos on their own line |
| 5V/5A USB-C supply or power bank | 1 | $15 | Powers the Raspberry Pi |
| Robot frame (3D-printed, wood, or sturdy cardboard) | 1 | varies | The robot's skeleton |
| Jumper wires (M-M & M-F) | ~20 | $5 | Connects the electronics |
| Small screwdriver, screws, hot glue | 1 set | $5 | Assembling the body |
4Safety First
Grown-up needed
Wiring, soldering (if any), and the first power-up should always be done with an adult. Servos can pinch fingers — keep hands clear when they're moving and powered.
Two separate power supplies
Never power all 8 servos from the Raspberry Pi's 5V pin — it can't supply enough current and may crash or damage the Pi. Always give the servos their own power supply, sharing only a common ground.
5Wiring & Circuit Diagram
The Raspberry Pi talks to a PCA9685 servo driver board over I2C. The driver board then powers and controls all 8 servos, using a separate power supply so the servos never starve the Pi of power.
Pi 5 → I2C → PCA9685 → 8 servo channels. Servo power comes from its own 5–6V supply, sharing ground with the Pi.
| Pi 5 pin | Wire to | PCA9685 pin |
|---|---|---|
| Pin 1 (3.3V) | → | VCC (logic power) |
| Pin 3 (GPIO2 / SDA) | → | SDA |
| Pin 5 (GPIO3 / SCL) | → | SCL |
| Pin 6 (GND) | → | GND |
6Step-by-Step Build
Flash the operating system
Using another computer, open the Raspberry Pi Imager, choose Raspberry Pi OS (64-bit), and click the gear icon to set your Wi-Fi, username, password, and enable SSH before writing it to the microSD card.
First boot & update
Insert the card, power on the Pi, and open a terminal. Run sudo apt update && sudo apt upgrade -y to make sure everything is current.
Turn on I2C
Run sudo raspi-config, go to Interface Options → I2C, and enable it. Reboot afterwards with sudo reboot.
Build the robot's frame
Mount the 8 servos at the shoulders, elbows, hips, and knees of your frame. Screw on the servo horns loosely for now — you'll set their center position in software first.
Wire the circuit
Follow the diagram above: connect the PCA9685 to the Pi's I2C pins, plug the 8 servos into channels 0–7, and connect the separate power supply to the driver board's V+ and GND terminals. Plug the USB mic and speaker into the Pi's USB ports.
Install the software
In the terminal, run:
# system packages sudo apt install python3-pip python3-venv i2c-tools portaudio19-dev -y # check the PCA9685 is detected (look for address 0x40) i2cdetect -y 1 # python environment python3 -m venv robot-env source robot-env/bin/activate pip install google-genai adafruit-circuitpython-servokit \ SpeechRecognition pyaudio gTTS pygame
Get a free Gemini API key
Visit Google AI Studio, sign in, and click "Get API key" to create one for free. Save it as an environment variable instead of pasting it into your code:
echo 'export GEMINI_API_KEY="your_key_here"' >> ~/.bashrc
source ~/.bashrc
Never share your API key or post it online — treat it like a password.
Add the code & calibrate
Save the script from the next section as robot_brain.py. Run it once, then gently adjust each servo horn so "90°" really is the joint's straight, relaxed position before tightening the screws.
Run it!
Activate your environment and start the robot: source robot-env/bin/activate && python3 robot_brain.py. Say hello and see what it does!
7The Robot's Brain (Code)
This script listens with the microphone, sends what it hears to Gemini, speaks the reply out loud, and moves the matching servos when you ask it to wave, walk, or return home.
# robot_brain.py — listens, thinks with Gemini, talks, and moves import os, time import speech_recognition as sr from gtts import gTTS import pygame from adafruit_servokit import ServoKit from google import genai # ---- setup ---- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) MODEL = "gemini-flash-latest" # always points to a current stable model kit = ServoKit(channels=16) SERVOS = { "left_shoulder": 0, "left_elbow": 1, "right_shoulder": 2, "right_elbow": 3, "left_hip": 4, "left_knee": 5, "right_hip": 6, "right_knee": 7, } HOME = {name: 90 for name in SERVOS} pygame.mixer.init() recognizer = sr.Recognizer() mic = sr.Microphone() def move(name, angle): kit.servo[SERVOS[name]].angle = max(0, min(180, angle)) def go_home(): for name, angle in HOME.items(): move(name, angle) def wave_hello(): for _ in range(3): move("right_shoulder", 60); move("right_elbow", 150) time.sleep(0.3) move("right_elbow", 60); time.sleep(0.3) go_home() def walk_forward(steps=2): for _ in range(steps): move("left_hip", 60); move("right_hip", 120); time.sleep(0.4) move("left_knee", 60); move("right_knee", 120); time.sleep(0.4) go_home(); time.sleep(0.2) ACTIONS = {"wave": wave_hello, "walk": walk_forward, "home": go_home} def listen(): with mic as source: recognizer.adjust_for_ambient_noise(source, duration=0.5) audio = recognizer.listen(source, phrase_time_limit=6) try: return recognizer.recognize_google(audio) except (sr.UnknownValueError, sr.RequestError): return "" def speak(text): gTTS(text=text, lang="en").save("reply.mp3") pygame.mixer.music.load("reply.mp3") pygame.mixer.music.play() while pygame.mixer.music.get_busy(): time.sleep(0.1) def ask_gemini(prompt): note = ("You are a friendly robot buddy for kids. Reply in 1-2 short, " "cheerful sentences. If asked to wave, walk, or go home, start " "your reply with that word in brackets, e.g. [wave] Here I go!") resp = client.models.generate_content(model=MODEL, contents=f"{note}\n\nKid said: {prompt}") return resp.text def run_action(reply): for name in ACTIONS: if f"[{name}]" in reply.lower(): ACTIONS[name]() return reply.replace(f"[{name}]", "").strip() return reply def main(): go_home() speak("Hi! I'm your robot buddy. Ask me anything, or tell me to wave or walk!") while True: heard = listen() if not heard: continue if heard.lower() in ("stop", "goodbye", "shut down"): speak("Goodbye! See you next time!"); break reply = ask_gemini(heard) speak(run_action(reply)) if __name__ == "__main__": main()
gemini-flash-latest is an alias that always points to Google's current stable Flash model, so your code keeps working even after Google releases newer versions.
8Testing & Teaching It Tasks
Start simple, then try these example things to say — Gemini will answer questions on its own, and the bracket-tag trick makes it move for the built-in actions.
Want more moves? Add new functions like bow() or dance() to the ACTIONS dictionary in the code, then just ask your robot to do them by name.
9Troubleshooting & FAQ
My servos jitter or reset when the robot talks
This almost always means the servos and the Pi are sharing one power source. Double-check the servos are on their own 5–6V supply and that the grounds are still connected together.
i2cdetect doesn't show the PCA9685
Confirm I2C is enabled in raspi-config, re-check the SDA/SCL wiring isn't swapped, and make sure the board has power on its logic (VCC) pins.
Do I need internet access for this to work?
Yes — both speech recognition and the Gemini API calls need Wi-Fi, since the "thinking" happens in the cloud rather than on the Pi itself.
Can I use fewer than 8 servos to start?
Definitely. Build with 2 or 4 servos first (just the arms, for example), get the voice and Gemini part working, then add the legs once you're comfortable.
Is the Gemini API free to use?
Google AI Studio offers a free tier for the Gemini API that's generous enough for a hobby project like this, though limits and pricing can change — check the current terms on Google's site before you build.
10What to Build Next
📷 Add eyes
Add a Pi Camera and let Gemini describe what the robot sees.
🎭 Give it a personality
Change the instructions sent to Gemini to make it silly, curious, or brave.
🕺 Choreograph a dance
String servo moves together into a full dance routine.
🌈 Add LEDs
Light up while it talks so you can tell when it's "thinking."

Comments
Post a Comment