Driving 12 Servo Motors from an 8051 with No PWM Pins — Building an Obstacle-Avoiding Quadruped Robot
How we generated twelve servo PWM signals from a single timer on an 8051 microcontroller with no PWM hardware, and built a four-legged walking robot with ultrasonic obstacle avoidance and infrared remote control.
"Can software do what the hardware can't?"
Embedded developers run into this question all the time. This article is about an obstacle-avoiding quadruped robot that our lead engineer built with a team of four in the winter of 2006, as the graduation project of an embedded systems course at Daedeok Human Resources Development Institute in Korea. The project is nearly 20 years old, but its core idea — "if the hardware isn't there, build it with a timer" — is still embedded fundamentals today.
The robot from above — three servos per leg, twelve in total
The project at a glance
| Item | Details |
|---|---|
| Period | November 17, 2006 – February 7, 2007 (team of four) |
| MCU | AT89S52 (8051 family, 24 MHz) on a TY52 module board |
| Actuators | 12 × HS-311 servo motors (three per leg; among the cheapest standard servos at the time) |
| Sensor | SRF04 ultrasonic range finder |
| Wireless | Infrared (IR) receiver module |
| Frame | Hand-cut 1.5 mm aluminum sheet |
| Size and power | 16.8 × 18.5 × 14 cm, 5 V 2 A wall adapter (no battery) |
| Toolchain | C, SDCC compiler, ISP programmer |
The robot has three modes, selected with two buttons on the back of the control board:
- Standby: repeats warm-up moves such as sitting down and standing up.
- Remote control: walks forward, backward, turns left/right or stops from a remote.
- Obstacle avoidance: walks on its own and turns when something is in front of it.
Control board and budget
The robot was controlled by a TY52 board, a module built around the 8051 (AT89S52). The two mode buttons are on the back of this board.
The robot from behind — TY52 control board, servo wiring and adapter cable
As a team project in a training course, we had little money for parts. We needed twelve servos, so we chose the inexpensive HS-311, and we could not afford a battery, so the robot ran from a wall adapter — it walked trailing its power cable.
Servos set their angle by pulse length
A servo does not spin continuously like a normal motor; it moves to a set angle and holds it. The angle is set by the length of the pulse (high signal) on its signal wire.
| Pulse length | Servo angle |
|---|---|
| about 0.7 ms | -90° |
| 1.5 ms | 0° (center) |
| about 2.3 ms | +90° |
The pulse must be repeated roughly every 20 ms for the servo to hold its position. A signal that varies the high time within a fixed period is called PWM (Pulse Width Modulation).
Servo wiring — one signal wire per MCU port pin
The problem: the 8051 has no PWM hardware
Most modern microcontrollers include hardware that generates PWM signals. The AT89S52 used in this robot had none. And there were twelve servos to drive.
The naive answer is "one timer per motor", but an 8051 has only a few timers — and one was needed for serial communication and another for the ultrasonic sensor.
One timer was left. It had to produce twelve PWM signals.
The solution: time-sliced PWM — splitting 20 ms twelve ways
The key idea is simple. A servo pulse is at most 2.3 ms long and repeats every 20 ms. So within each 20 ms, send one pulse to each motor in turn.
Time slicing — each motor gets its pulse in turn within the 20 ms period
Here is how it runs:
- On a timer interrupt, set only motor 1's pin high and schedule the next interrupt after motor 1's pulse length.
- On the next interrupt, pull motor 1's pin low, set only motor 2's pin high, and schedule the next interrupt after motor 2's pulse length.
- Repeat through motor 12.
- Finally, hold every pin low for 20 ms minus the time used so far, then start again with motor 1.
There is only one timer, but by changing the time until the next interrupt every time, it produces twelve PWM signals.
The actual code (with extra comments)
On a 24 MHz 8051, the timer counts two million times per second (every 0.5 µs). Pulse values were stored in 0.1 ms units (e.g. 15 = 1.5 ms), and the timer was loaded with 200 × value ticks.
u08 Pwm[12]; // pulse length per motor (0.1 ms units, e.g. 15 = 1.5 ms)
u08 pin = 0; // motor currently receiving its pulse
u16 pwm_sum = 0x63c0; // accumulator that keeps the 20 ms period (0x63c0 = 20 ms start)
void it_timer0(void) interrupt 1
{
TR0 = 0; // pause the timer
P0 = 0; P2 &= 0x0f; // end the previous pulse (all motor pins low)
if (pin == 12) { // all twelve motors have had their pulse
write_THL(pwm_sum); // idle for whatever is left of the 20 ms
pwm_sum = 0x63c0; // prepare the next period
pin = 0;
} else {
pwm_sum += 200 * Pwm[pin]; // add the time just used
set_THL(Pwm[pin]); // next interrupt after this motor's pulse
if (pin < 8) P0 = (0x01 << pin); // motors 0–7: port 0
else P2 |= (0x01 << (pin - 4)); // motors 8–11: upper 4 bits of port 2
pin++;
}
TR0 = 1; // restart the timer
}
The condition this method depends on
Time slicing keeps a steady period only if the twelve pulse lengths add up to no more than 20 ms. If every motor sat at its maximum angle (2.3 ms), the total would be 27.6 ms and the period would stretch.
Adding up the values for the poses the robot actually uses (standing, lying down, walking) gave about 17.8–19 ms, safely within 20 ms. When you solve a hardware limit in software, you must also check under which conditions the solution holds.
Obstacle avoidance: measuring distance by echo time
An SRF04 ultrasonic sensor sits at the front of the robot. It works like a bat: it sends out ultrasound and calculates distance from how long the echo takes to return.
The SRF04 ultrasonic sensor mounted at the front
- A pulse of at least 10 µs on the trigger pin fires the ultrasound, and the echo pin goes high.
- When the echo returns, the echo pin drops low. The time it stayed high is the distance.
- Sound takes about 58 µs to travel 1 cm and back, so dividing the time by 58 gives the distance in centimeters.
To measure that time accurately we used Timer 2's capture feature. The hardware automatically latches the timer value the moment the echo pin falls, so the measurement stays accurate even while the program is busy with something else.
temp = (RCAP2H << 8) | RCAP2L; // timer value captured when the echo ended
distance = temp / 2 - BURST; // 0.5 µs ticks → µs, minus burst time (measured)
distance = distance / 58; // µs → cm
if (distance < 33) walk_r(); // obstacle within 33 cm: turn right
else walk_fwd(); // otherwise walk forward
BURST compensates for the time it takes to emit the ultrasound, and was determined experimentally by measuring real distances. Calibrating by measurement where the datasheet falls short is an essential part of embedded work.
It didn't avoid everything at first
In an obstacle-avoidance test filmed during development, the robot dodged the first obstacle but pushed straight through the second. Sensor readings that look fine on the bench can behave differently once the robot is walking, swaying and turning.
A robot with sensors has to be tested repeatedly while it actually moves, not just on the bench.
Remote control: finishing the job with a broken part
For remote control we used an IR receiver module that turns ordinary remote-control signals into an 8-bit value. Midway through the project, some of the module's output pins (the two lowest bits) were damaged. The remaining pins could not tell the remote's arrow buttons apart.
With no time to source a replacement, we solved it this way:
- We changed the program to use only the three working pins, giving 2³ = 8 distinct commands. Five — forward, backward, left, right and stop — were all we needed.
- Instead of a normal remote, we used a remote-control app on a PDA and taught it the IR codes we wanted, so its buttons mapped to values the three bits could distinguish.
It was a lesson in going back to the real requirement (five commands) and solving it with the resources left when conditions are not perfect.
Walking: moving diagonal legs in pairs
Making four legs walk was harder than expected. At first we simply alternated the legs, and the robot just shuffled in place.
The solution was to move diagonally opposite legs as a pair:
- Lift one diagonal pair (legs 2 and 3)
- Swing the lifted legs forward while the other two push the body ahead
- Lower the legs back to the base pose
- Repeat with the other diagonal pair (legs 1 and 4)
Because two diagonal legs are always on the ground while the other two move, the body stays balanced as it moves forward. Each pose was defined as an array of twelve pulse values, and walking was produced by switching between these arrays in sequence.
Legs and joints cut and bent by hand from aluminum sheet
The frame: cutting and bending aluminum by hand
The body and joints were made by hand from 1.5 mm aluminum sheet. We drew the parts on the sheet, cut them out with a fret saw, smoothed them with files and sandpaper, and bent them in a hand vise. To keep the robot light enough to walk with twelve servos on board, we drilled as many holes in the body as we could.
For the metalwork, a teammate with jewelry-making experience made a big difference. The course brought together people from all walks of life — from students to people who had left their jobs to retrain — and although it was a software course, everyone's background ended up contributing to the finished robot. We learned that mechanical accuracy matters as much as software for a stable gait.
If we built it again today
Nearly 20 years later, here is what we would do differently:
- Use a dedicated PWM chip: a 16-channel PWM driver such as the PCA9685 means the MCU only sends angles — no timer math and no 20 ms constraint.
- Separate, generous servo power: twelve servos pulling at once cause large current spikes. Separating logic and servo power and leaving headroom prevents glitches.
- Add an IMU: measuring tilt to correct the gait keeps the robot upright on uneven ground.
- Modern sensors and connectivity: Bluetooth or Wi-Fi modules allow smartphone control and live sensor readouts.
- Camera streaming: something we badly wanted to add back then but never did. Today a small module such as the ESP32-CAM can stream the robot's view to a phone in real time.
- An onboard battery: a lithium battery with a voltage regulator would let it walk freely without a cable.
Still, the principle of working around hardware limits with timers and interrupts remains valid. In equipment control, where PWM channels run short or unusual signal timing is needed, these software techniques are still used today.
Closing thoughts
This project was the starting point for our lead engineer's later work in equipment control, inspection systems, automotive software and other embedded and Windows application fields. Working within fixed parts and time, checking the conditions, calibrating by measurement, and finding another way when something breaks — that approach is the same on every project today.
If you need equipment control, embedded software or C/C++ application development, contact Springuptech.
※ Photos from the time and a short write-up of this robot are also on our lead engineer's personal blog (in Korean): "4족 로봇 - 4 legged robot".
Jaeyong Roh
Lead engineer at Springuptech · 22 years in software engineering
Have a project related to this topic?
Even if it is just an idea, we will help you work out what is possible.