So I built this 4-legged walking robot using an ESP32 and a PCA9685 servo driver. The cool part? You don't need a 3D printer - just some basic craft materials. It's honestly a great project for learning how robots walk and balance.
The robot can walk forward and backward, turn left and right, and you control everything through serial commands. I added smooth motion so it doesn't look too jerky, plus there's a calibration mode in case your servos aren't perfectly aligned (spoiler: they never are).
- ESP32 board (pretty much any variant works)
- PCA9685 servo driver (get the Adafruit one or a cheap clone)
- 12 hobby servos I used SG90s, they're like $2 each
- A decent 5V power supply that can push at least 2-3 amps
- Some jumper wires
- Maybe a breadboard if you're prototyping
You can basically use whatever you have lying around:
- Thick cardboard works great (raid your recycling bin)
- Popsicle sticks if you want it to look nice
- Old plastic containers
- Straws or skewers (for leg segments)
- Hot glue gun + glue sticks
- Zip ties or wire (for securing servos)
- Tape, screws, or brass fasteners (for joints)
See ASSEMBLY.md for construction ideas.
Here's the quick version - full diagrams are in WIRING.md if you need them.
| ESP32 Pin | PCA9685 Pin | What it does |\n|-----------|-------------|-------------|\n| 3.3V or 5V | VCC | Powers the board logic |\n| GND | GND | Ground |\n| GPIO 21 (SDA) | SDA | I2C data line |\n| GPIO 22 (SCL) | SCL | I2C clock line |
Each leg has 3 servos (hip, upper leg, lower leg). Front Left uses channels 0-2, Front Right is 3-5, Back Left is 6-8, Back Right is 9-11.
Super important: Don't try to power the servos from the ESP32! You'll fry it. Use a separate 5V power supply connected to the V+ terminal on the PCA9685. Trust me on this one.
Just download it from arduino.cc if you don't have it already.
- Open Arduino IDE
- File → Preferences
- Paste this into "Additional Board Manager URLs":
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - Tools → Board → Board Manager
- Search "ESP32" and install it
Go to Sketch → Include Library → Manage Libraries, then search for and install:
- Adafruit PWM Servo Driver Library
That's it. The Wire library should already be there.
- Open
quadruped_robot.ino - Pick your ESP32 board from Tools → Board
- Pick your port from Tools → Port
- Hit the upload button
Once you upload the code, open the Serial Monitor (set it to 115200 baud) and you can control the robot with these commands:
| Command | What it does |
|---|---|
F |
Walks forward |
B |
Walks backward |
L |
Turns left |
R |
Turns right |
S |
Stops moving |
N |
Goes back to standing position |
C |
Opens calibration mode |
Look, cheap servos are never perfect. If your robot looks wonky when it stands up, here's how to fix it:
-
Send
Cto enter calibration mode -
All servos will go to 90° (neutral position)
-
Now you can adjust them:
- Type
0-11to pick which servo you want to adjust - Hit
+to nudge it one way - Hit
-to nudge it the other way - Type
pto see all your current offsets - Type
xwhen you're done
- Type
-
Write down your offsets and put them in
config.hso they stick around
You can mess with a bunch of settings in config.h:
- Servo pulse limits if your servos are different
- How fast it walks (the delay timings)
- Where the legs go when standing, lifting, walking
- Permanent servo offset values
- Make sure your power supply is actually 5V and can handle at least 2 amps
- Double-check the I2C wires (SDA to GPIO21, SCL to GPIO22)
- The PCA9685 address should be 0x40 unless you changed it
- Check the Serial Monitor - it might tell you what's wrong
- You probably need more power - get a beefier 5V supply
- Check for loose wires
- Try adjusting SERVOMIN and SERVOMAX in the config file
- Use calibration mode to fix wonky servos
- Make sure the servo horns are screwed on straight
- Check that all the legs are about the same length
- Move your battery around until it balances better
- Just flip the offset in calibration mode
- Or change the position values in config.h
The robot walks using a "diagonal gait" - basically the front-left and back-right legs move together, then the front-right and back-left move. It's the same way dogs and horses walk, and it's pretty stable.
The code uses a state machine, which is just a fancy way of saying the robot knows what it should be doing (standing, walking, turning, etc.) and switches between those states.
Once you get this working, here are some fun additions:
- Stick an ultrasonic sensor on it so it doesn't run into stuff
- Add speed control
- Try different walking patterns (tripod gait, wave gait, etc.)
- Add Bluetooth or WiFi so you can control it from your phone
- Make it autonomous
hexapod/
├── quadruped_robot.ino # The main code
├── config.h # All the settings
├── README.md # You're reading it
├── WIRING.md # How to wire everything
└── ASSEMBLY.md # How to build the body
That's pretty much it! Build something cool and don't be afraid to experiment with the code. If you break something, you can always re-upload it.