Home > Industry Insights >Gear Motor
TECHNICAL SUPPORT

Product Support

How to Control Servo Motor Angle with a Microcontroller: A Complete Step‑by‑Step Guide

Published 2026-04-17

01How to ControlservoMotor Angle with a Microcontroller: A Complete Step‑by‑Step Guide

This guide provides a clear, practical explanation of exactly how a microcontroller controls the rotation angle of a standardservomotor. You will learn the core working principle, the precise electrical signal required, and the complete step‑by‑step method to generate that signal using any common microcontroller. No brand names or specific company products are referenced – only generic, widely applicable principles and real‑world examples are used.

1. Core Principle: Pulse Width Modulation (PWM) Directly Determines the Angle

A standardservomotor doesnotrotate continuously like a regular DC motor. Instead, it moves to a specific angular position (e.g., 0°, 90°, or 180°) and holds that position. The angle is controlled entirely by the width of an electrical pulse sent every 20 milliseconds (ms).

The direct relationship is:

Pulse width between 1.0 ms and 2.0 msAngle between 0° and 180°(for most common servos)

A 1.0 ms pulse → 0° (full counter‑clockwise)

A 1.5 ms pulse → 90° (center position)

A 2.0 ms pulse → 180° (full clockwise)

> Real‑world example:In a hobbyist robotic arm, to make the gripper close completely (0°), the microcontroller sends a repeating 1.0 ms pulse. To open the gripper fully (180°), it sends a 2.0 ms pulse. For a half‑open position (90°), it sends a 1.5 ms pulse.

This signal is calledPulse Width Modulation (PWM)– a method where the microcontroller outputs a digital signal that switches between ON (5V or 3.3V) and OFF (0V) very quickly, and the duration of the ON pulse is what the servo reads.

2. Step‑by‑Step: How the Microcontroller Generates the Correct Pulse

Follow these steps exactly to control any standard servo with any microcontroller:

Step 1: Identify the Servo’s Signal Requirements

Most standard servos operate with:

Voltage:4.8V – 6.0V (power supply,notfrom microcontroller pins)

Control signal voltage:3.3V or 5V (matches microcontroller logic level)

Pulse repetition period:20 ms (50 Hz frequency)

Pulse width range:1.0 ms to 2.0 ms (for 0° to 180°)

Important:Verify your servo’s datasheet for exact pulse width range. Some servos use 0.5 ms to 2.5 ms for 0°–180°, or 1.0 ms to 2.0 ms for 0°–90°. The principle remains identical.

Step 2: Connect the Servo to the Microcontroller Correctly

A standard servo has three wires:

Wire Color (typical) Function Connection
Brown or Black Ground (GND) Microcontroller GND
Red Power (VCC, 5V) External 5V power supply (do not use microcontroller’s 5V pin for more than one small servo)
Orange or Yellow Signal (PWM) Microcontroller PWM‑capable digital pin

Critical safety note:Never power a servo directly from a microcontroller’s 5V pin. A servo can draw 200–1000 mA, which exceeds most microcontroller pin ratings. Use a separate 5V power supply with common ground.

Step 3: Configure the Microcontroller’s PWM Hardware

Every microcontroller has built‑in PWM timers. The configuration requires setting two parameters:

Frequency = 50 Hz(period = 20 ms)

Resolution(typically 8‑bit to 16‑bit, depending on microcontroller)

Example generic calculation (for any microcontroller):

If your PWM resolution is 8‑bit (0 to 255), and the total period is 20 ms (20,000 µs):

1.0 ms pulse → duty cycle = (1.0 ms / 20 ms) × 255 = 12.75 → use 13

1.5 ms pulse → duty cycle = (1.5 ms / 20 ms) × 255 = 19.125 → use 19

2.0 ms pulse → duty cycle = (2.0 ms / 20 ms) × 255 = 25.5 → use 26

Real‑world case:A common 8‑bit microcontroller set to 50 Hz PWM will write a value of 13 to the PWM compare register to achieve 0°, 19 for 90°,and 26 for 180°.

Step 4: Write the Microcontroller Code to Set the Desired Angle

The code logic is always the same across all microcontrollers:

1. Initialize the PWM hardware with 50 Hz frequency.

2. Calculate the required pulse width in microseconds for your target angle using the linear formula:

Pulse width (µs) = 1000 + (angle / 180) × 1000(for 1.0–2.0 ms range)

Angle 0° → 1000 µs

Angle 90° → 1500 µs

Angle 180° → 2000 µs

3. Convert pulse width to the microcontroller’s duty cycle register value.

4. Write that value to the PWM output pin.

Generic pseudocode (adapt to any microcontroller):

// Assume PWM timer is already configured for 50 Hz, 8‑bit resolution int targetAngle = 90; // degrees int pulseWidth_us = 1000 + (targetAngle1000 / 180); int dutyCycleValue = (pulseWidth_us255) / 20000; setPWMDutyCycle(PWM_PIN, dutyCycleValue);

Step 5: Verify with Real‑World Measurements

After uploading the code, use an oscilloscope or a logic analyzer to verify the signal:

Frequency should be 50 Hz(period = 20 ms)

Pulse width should match your calculated value(e.g., 1.5 ms for 90°)

Common real‑world issue:If the servo jitters or does not move, the pulse width is likely incorrect. Measure directly at the signal pin. A 0.05 ms error can cause a 5°–10° deviation.

3. Complete Example: Controlling a Servo from 0° to 180° and Back

Here is a complete, generic example that works on any microcontroller after minor syntax adaptation. The logic is what matters.

Scenario:A simple camera panning platform. The servo should sweep from 0° to 180° and back continuously.

Step‑by‑step implementation logic:

1. Set PWM frequency to 50 Hz.

2. Define a functionsetAngle(angle)that:

Clamps angle between 0° and 180°

Calculates pulse width = 1000 + (angle × 1000 / 180) microseconds

Converts to duty cycle register value

Writes to PWM pin

3. In the main loop:

Increment angle from 0° to 180° in steps of 1°

CallsetAngle(angle)for each step

Wait 15 ms (allows servo to physically move)

Then decrement angle back to 0°

Expected result:The servo smoothly rotates from one end to the other, stopping precisely at each calculated angle.

4. Critical Verification Table: Pulse Width vs. Angle

Use this table for quick verification without calculations:

Desired Angle Required Pulse Width (µs) Duty Cycle @ 50 Hz, 8‑bit (0–255)
1000 13
45° 1250 16
90° 1500 19
135° 1750 22
180° 2000 26

Note:If your servo does not reach 0° or 180° exactly, adjust the minimum and maximum pulse widths slightly (e.g., 950 µs for 0°, 2050 µs for 180°). This is normal due to manufacturing tolerances.

5. Why This Method Is Universal and Reliable

The pulse‑width control method is an industry‑standard analog servo interface used for decades. Every standard servo – regardless of manufacturer – responds to the same 1.0–2.0 ms pulse range. This means:

You can control any standard servo with any microcontroller that outputs 50 Hz PWM.

The same code logic works for microcontrollers from 8‑bit to 32‑bit.

No proprietary libraries or brand‑specific functions are required.

6. Common Real‑World Problems and Their Solutions

Problem Most Likely Cause Solution
Servo does not move at all No power to servo or incorrect ground connection Connect servo power to external 5V supply and common ground with microcontroller
Servo vibrates or jitters Pulse width is unstable or frequency is not exactly 50 Hz Check PWM configuration; use hardware PWM instead of software delay loops
Servo only moves to 0° and 180°, not intermediate angles Code is sending only minimum and maximum pulse widths Verify the calculation formula; measure pulse width with oscilloscope
Servo gets very hot Pulse frequency is too high (e.g., 300 Hz instead of 50 Hz) Reconfigure PWM to exactly 50 Hz (20 ms period)
Servo position drifts over time Power supply voltage is unstable Add a large capacitor (1000 µF) across servo power lines

7. Actionable Conclusion: Your Exact Next Steps

Core principle to remember:

> The servo angle isdirectly proportionalto the pulse width between 1.0 ms and 2.0 ms, repeated every 20 ms. Change the pulse width, and you change the angle – nothing else matters.

Immediate action plan to implement today:

1. Verify your servo’s pulse range– Check its datasheet for min/center/max pulse widths. Most use 1.0/1.5/2.0 ms for 0/90/180°.

2. Set up your microcontroller’s PWM– Configure one PWM pin to 50 Hz frequency. Use hardware PWM, not software bit‑banging, for stable pulses.

3. Write the angle‑to‑pulse conversion formula– Use:pulse_us = 1000 + (angle_deg × 1000 / 180).

4. Test with three known angles– Command 0°, 90°, and 180°. Measure the actual shaft position. Adjust min/max pulse widths if needed.

5. Add a calibration routine– Store your servo’s exact min and max pulse widths in code. This ensures perfect 0°–180° range despite manufacturing variations.

Final validation:After following this guide, you will be able to command any angle from 0° to 180° with ±1° accuracy. The same method works for continuous rotation servos (where pulse width controls speed and direction) and for servos with different angle ranges (e.g., 0°–90° or 0°–270°) – simply adjust the pulse width range accordingly.

Repeat the core truth:A microcontroller controls a servo’s angle by outputting a 50 Hz PWM signal where the ON‑time (pulse width) varies from 1.0 ms (0°) to 2.0 ms (180°). Master this pulse‑to‑angle relationship, and you can control any servo with any microcontroller.

Update Time:2026-04-17

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
+86 0769 8399 3238
 
kpowerMap