A-Level Computer Science project, 2025–26
Atlas, a self-balancing desk robot
A two-wheeled robot that keeps itself upright on my desk. It’s built and it balances, using an LQR controller with the gains worked out in MATLAB from a model of the robot. It’s also a revision timer, counting down study sessions on its round screen. Now I’m writing up the documentation.
- Controller
- LQR (it started as PID)
- Gains from
- MATLAB
- Microcontroller
- ESP32-S3
- Tilt sensor
- MPU-6500

Why LQR instead of PID
My first plan was a PID controller on the lean angle, with a complementary filter to estimate the angle. I switched Atlas to an LQR controller instead.
A PID loop on the angle only looks at one number. It can push the wheels to stop the robot falling, but it doesn't know the robot is slowly rolling off across the desk, and its three gains have to be found by trial and error.
LQR looks at all four states at once: how far the wheels have moved, how fast they're going, the lean angle, and how fast it's leaning. I don't guess the gains. I decide how much I care about each state and how much motor effort I'm willing to spend, and the maths gives me the best gains for that trade-off.
I wrote up PID, the state-space model, the A and B matrices and the Riccati equation in Controlling an inverted pendulum. This page is about how it's used on Atlas.
How the controller is designed
Measure the four states
The MPU-6500 gives the lean angle and how fast it's changing. The wheel encoders give how far the robot has moved and how fast.
Model the robot
I modelled Atlas as a body sitting on a pair of wheels and used Newton's laws to write it in state-space form, . The body's moment of inertia comes from treating it as a box, , rather than measuring it.
Choose the weights
Q and R come from Bryson's rule, which sets each weight from the largest value I'm happy to see: 10 cm of drift, 0.5 m/s, 5° of lean, 90°/s, and 4 V on the motors.
Let MATLAB solve it
A MATLAB script solves the Riccati equation and gives the gain vector K. I wrote it without any toolboxes, so it also runs in Octave.
Run it on the robot
Every loop, the ESP32-S3 works out and turns it into a motor voltage. The balance code runs as its own FreeRTOS task on one core, so the screen, sound and Wi-Fi can't hold it up.
The parts
| Part | What I used | What it's for |
|---|---|---|
| Microcontroller | ESP32-S3 dev board (N16R8) | Runs the balance loop and everything else |
| Tilt sensor | MPU-6500 | Accelerometer and gyro for the lean angle and rate. My first IMU board was dead, so this is the replacement |
| Motors | 2 × JGA25-370 with encoders | Geared wheel motors. The encoders measure how far each wheel has turned |
| Motor driver | DRV8833 | Drives the two wheel motors |
| Distance sensor | VL53L0X | Sees obstacles in front |
| Edge sensor | TCRT5000 | Stops it driving forward off the desk |
| Touch | 2 × TTP223 | Buttons on the body |
| Screen | GC9A01, round | The timer and menus |
| Sound | MAX98357A amplifier | Chimes and spoken reminders |
| Firmware | C++ in the Arduino IDE | FreeRTOS tasks for balance, screen and network |
More than balancing
For a Computer Science project it has to do something useful too, so Atlas is also a revision timer. It times study sessions and breaks, plays a chime and a spoken reminder when a break is due, and keeps a weekly chart of how much I've done against a goal.
It also does a full spin on the spot to say hello. That's harder than it sounds, because turning needs its own controller on the wheel encoders, and that has to hold the heading while the LQR loop is busy keeping it upright.
Where it's up to
Last updated October 2026
- Chassis and electronicsBuilt
- LQR design in MATLABDone
- Balancing on the real robotBalancing
- Revision timer on the round screenWorking
- DocumentationWriting it now
- Talking to the light switchLater
What's next
It balances now. Getting there wasn't quick: the gearboxes on the motors have a bit of slack in them, and my first set of gains made it shake badly until I tuned it down. The main job now is the documentation for my Computer Science project, writing up how it works and how I tested it.
Longer term, I'd like it to talk to my light switch over MQTT. I'll add more photos and test results here as I write it up.
Ask me about Atlas