How swerve drive works
Mecanum wheels slide sideways on their rollers. A swerve drive does it differently: every wheel sits in a module that can turn the wheel to any direction (steering) and spin it (driving). Nothing slides. The wheels simply point where they need to go. It is the drive used by many top competition robots, because it pushes hard, does not slip, and goes anywhere.
What one module has to do
Every module has to answer one question, 50 times per second: “In which direction, and how fast, must my wheel roll?”
If the robot only moves (no turning), the answer is the same for all four: the robot’s own velocity. If the robot also turns, each corner moves differently: the corners on the outside of the turn move faster, and in a different direction. A module at position from the centre, on a robot moving with and turning with , must move with
The direction of that arrow is the steering angle. Its length is the wheel speed:
The 180° trick
A wheel pointing at 120° and rolling forward moves the robot exactly like the same wheel pointing at −60° and rolling backward. Both describe the same arrow.
So a steering servo that turns only from −90° to +90° can still point a wheel in every direction: whenever the angle we need is outside its range, add 180° and reverse the motor. A bonus: the wires never twist, because the module never turns more than half a circle.
The firmware also picks, among the two equivalent options, the one closer to where the wheel points now. That keeps the steering movements short.
Two more details that make it smooth
Cosine scaling. A servo needs a moment to turn. If the wheel still points 90° away from where it should, driving it at full speed just scrubs the tyre sideways. So the drive speed is multiplied by : full speed when aligned, zero when 90° off.
Hold the angle when stopped. When the stick is released, the speed is zero and the direction is undefined. The code keeps every wheel where it was instead of snapping back to straight ahead, so the robot does not twitch.
What each part does
| Job | On this platform | Part |
|---|---|---|
| Sense | distance in front, battery voltage | HC-SR04, voltage divider |
| Decide | joystick → four (angle, speed) pairs | ESP32 DevKit, MicroPython |
| Act: steer | point each wheel | four MG90S metal-gear servos through a PCA9685 servo driver |
| Act: drive | spin each wheel | four N20 gear motors through two TB6612FNG drivers |
You drive it from your phone, with the same joystick page as the mecanum platform. No app to install.
What is in this guide
- How swerve drive works (this chapter, free)
- Parts, printing and building the modules
- Power and wiring
- The code, calibrating the steering, and the first drive
- Add a camera (optional): parts, mount and flashing
- Camera: following a colour
- Camera: driving with a vision language model, safely
- Troubleshooting and what next
Chapters 5 to 7 are an optional upgrade: an ESP32-CAM on the front lets the robot follow a coloured ball or drive towards a goal you describe in words. The robot is complete without them.


