ATmega328P from scratchPart 7 of 12

Build note

Servos: when pulse width is a position

A hobby servo contains a small motor, a gearbox, a position sensor and a control circuit. You send it one signal wire, and the control circuit drives the motor until the measured position matches the width of the pulse you sent.

You do not tell it how hard to push. You tell it where the edge should be, and it holds that position against a light push.

The protocol

Two numbers define everything:

  • 50 Hz frame rate. One pulse every 20 ms.
  • Pulse width between about 1 and 2 ms, mapped across the travel. 1.5 ms is usually the centre.
Pulse Typical position
1.0 ms One end, 0 degrees
1.5 ms Centre, 90 degrees
2.0 ms Other end, 180 degrees

Many servos accept 0.6 to 2.4 ms, and some accept less. Pushing past the mechanical limit makes the motor stall and buzz, which is the sound of a servo being damaged.

The same Timer1, a different period

At a prescaler of 8, one tick is 0.5 microseconds, so a 20 ms frame is 40,000 ticks:

TCCR1A = (1 << WGM11) | (1 << COM1A1);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1   = 40000;   // 20 ms at 0.5 us per tick
OCR1A  = 3000;    // 1.5 ms, centre

The width values fall out of the same conversion:

Pulse Microseconds Ticks
1.0 ms 1000 2000
1.5 ms 1500 3000
2.0 ms 2000 4000

Mapping an angle to a pulse is then one line:

uint16_t pulse = 2000 + ((uint32_t)angle * 2000) / 180;
OCR1A = pulse;

The uint32_t cast prevents the angle multiply from overflowing 16 bits.

Why Timer0 cannot do this

A 20 ms period at 16 MHz is 320,000 clock cycles. Timer0 is 8-bit and cannot count past 255, so it needs a prescaler so large that the resolution for a 1 ms pulse becomes useless. Timer1’s 16 bits are what make the timing workable.

Power is the part that bites

The signal is 5 V and tiny. The motor is neither.

  • Small servos such as an SG90 can sometimes run from a 5 V rail that can supply a few hundred milliamps.
  • Standard servos need their own 5 to 6 V supply. The MG996R can pull close to an amp and peak much higher.
  • The grounds must be joined. The signal is measured against ground, so the servo supply and the chip must share it.

Powering a servo from the same regulator as the chip is the usual cause of a reset in the middle of a movement. The motor pulls the rail down, the chip browns out, and the symptom looks like a software bug.

When it jitters

Jitter is almost always electrical, not code:

  • The supply sags when the motor starts.
  • The servo and the chip do not share a ground.
  • Long, thin power wires add resistance.
  • A 100 to 470 microfarad capacitor across the servo’s supply helps with the first surge.

If the servo instead moves to the wrong angle, the pulse range is probably wrong for that model. Try 600 to 2400 microseconds, and narrow the range until it stops buzzing at the ends.

What the code demonstrates

The example moves to centre, then to both ends, then sweeps slowly across the range and back. The sweep uses short delays between angle steps so the motion is smooth rather than a jump. The same setServoAngle function is the only thing you need to drive a robot arm later.

How this was made

  • Written by: Rupesh, from his own c-08-servomotor notes and code, with AI drafting and editing.
  • Numbers: from the servo pulse convention and the timer arithmetic.
  • AI-assisted sections: structure and copy editing.