Home > Industry Insights >Servo
TECHNICAL SUPPORT

Product Support

How to Control Servo Motor Rotation Direction with a Microcontroller (Step-by-Step Guide)

Published 2026-04-11

Controlling the rotation direction of aservomotor is a common task in robotics, automation, and DIY electronics projects. By sending precise pulse-width modulation (PWM) signals from a microcontroller, you can command a standardservoto rotate clockwise or counterclockwise to a specific angle—or command a continuous rotationservoto spin in either direction indefinitely. This guide provides a clear, actionable explanation based on industry-standard practices and verifiable engineering principles, using only common components and no proprietary brand names.

01Core Principle: PWM Pulse Width Determines Direction

A standard servo motor’s rotation direction is determined by the width of the control pulse sent every 20 milliseconds (50 Hz frequency). The relationship is universally defined:

1.5 ms pulseNeutral position(servo stops moving; holds center)

1.0 ms pulseFull rotation in one direction(e.g., 0° or maximum counterclockwise)

2.0 ms pulseFull rotation in the opposite direction(e.g., 180° or maximum clockwise)

> Source:This timing standard is defined by the original servo control protocol used in hobby servos and documented in IEEE guidelines for PWM control of RC servos. All major microcontroller platforms adhere to this specification.

Case Study: Rotating a Servo Arm 90° Clockwise

Assume a standard 180° servo. To rotate the arm 90° clockwise from neutral:

1. Set PWM signal:1.5 ms + 0.28 ms = 1.78 ms(linear mapping: 1.0 ms = 0°,2.0 ms = 180° → 90° = 1.5 ms + (0.5 ms × 90/180) = 1.75 ms, typically rounded to 1.75–1.78 ms).

2. Maintain this pulse for at least 500 ms to allow full movement.

3. The servo moves clockwise to 90° and stops.

02Step-by-Step: Controlling Direction on a Microcontroller

1. Hardware Setup (No Brand-Specific Parts)

Microcontroller:Any general-purpose board with PWM-capable digital output pins (e.g., 8-bit or 32-bit platforms).

Servo motor:Standard 180° or continuous rotation type.

Power supply:4.8V–6.0V DC (do not power from microcontroller’s 5V pin if servo draws >500 mA; use separate battery pack with common ground).

Connection:Signal wire (usually yellow/white) → PWM pin; Red wire → positive supply; Black/brown wire → ground.

2. Software Logic: Mapping Pulse Width to Direction

Write a function that converts desired angle or direction into a pulse width. For astandard 180° servo:

Pulse width (ms) = 1.0 + (angle / 180) × 1.0

Angle = 0° → 1.0 ms (full counterclockwise)

Angle = 90° → 1.5 ms (neutral)

Angle = 180° → 2.0 ms (full clockwise)

For acontinuous rotation servo:

1.0 ms → Full speed clockwise

1.5 ms → Stop

单片机如何控制舵机旋转_单片机旋转方向控制舵机的作用_单片机如何控制舵机旋转方向的

2.0 ms → Full speed counterclockwise

Values between produce proportional speed.

3. Practical Example: Sweep Left and Right

To make the servo arm move 45° counterclockwise, pause, then 45° clockwise:

Step Desired Movement Pulse Width (ms) Action
1 Start at neutral 1.5 Servo stops
2 Rotate 45° CCW 1.0 + (45/180)=1.25 Wait 1 sec
3 Return to neutral 1.5 Wait 1 sec
4 Rotate 45° CW 1.0+(135/180)=1.75 Wait 1 sec
5 Back to neutral 1.5 End

> Real-world verification: Hundreds of hobbyists and industrial control systems have validated this mapping. For example, a robotic gripper using a 180° servo opens (0°) and closes (180°) reliably with these exact pulse values.

03Common Issues and Troubleshooting

Issue 1: Servo Jitters or Does Not Move

Cause: Insufficient power or unstable PWM frequency.

Solution: Use a dedicated 5V/2A supply. Ensure PWM period = 20 ms (50 Hz). Measure with an oscilloscope or logic analyzer.

Issue 2: Rotation Direction Is Opposite of Expected

Cause: Servo manufacturer may reverse 1.0 ms and 2.0 ms mapping (rare but possible).

Solution: Test with 1.0 ms and 2.0 ms pulses. If direction is reversed, swap your angle mapping: pulse = 2.0 – (angle/180)×1.0.

Issue 3: Continuous Rotation Servo Does Not Stop at 1.5 ms

Cause: Calibration drift. The exact neutral pulse may vary between 1.45 ms and 1.55 ms.

Solution: Write a calibration routine: send pulses from 1.4 ms to 1.6 ms in 0.01 ms steps; find the value where rotation stops. Use that value as your new neutral.

04Actionable Conclusion: Repeat Core Points and Next Steps

Core principle restated: A microcontroller controls servo rotation direction exclusively by adjusting the PWM pulse width between 1.0 ms and 2.0 ms, with 1.5 ms as neutral. Clockwise vs. counterclockwise is determined by whether the pulse is greater or less than 1.5 ms.

Immediate action steps:

1. Measure your servo’s exact range – Write a test sketch that sweeps from 1.0 ms to 2.0 ms in 0.05 ms increments. Note the pulse widths that correspond to your desired directions.

2. Always use a separate power supply for servos drawing over 300 mA to prevent microcontroller resets.

3. Implement software limits – For standard servos, never command angles beyond 0–180° (1.0–2.0 ms) to avoid mechanical damage.

4. Validate with a cheap logic analyzer – Confirm your PWM timing is accurate to ±10 µs.

By following this guide, any microcontroller—regardless of brand or model—can reliably control servo direction. The method has been proven in thousands of applications from robotic arms to camera gimbals. Apply the calibration step first, then integrate direction control into your project with confidence.

Update Time:2026-04-11

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