Published 2026-04-07
This guide provides everything you need to precisely control the rotation angle of a standard microservousing a microcontroller. The core principle is simple: theservo’s output shaft position is determined by the width of a pulse signal. For most common microservos, a pulse width of 1.5 milliseconds (ms) centers the shaft at 90°, 1.0 ms moves it to 0°, and 2.0 ms moves it to 180°. However, real-world servos vary. This article gives you verified methods, code examples, and calibration steps to achieve accurate degree-by-degree control without relying on any specific brand.
AMicro Servocontains a small DC motor, a feedback potentiometer, and a control circuit. The circuit compares the incoming pulse width to the potentiometer’s position. When the pulse width matches the desired position, the motor stops. The relationship between pulse width and angle is linear within the servo’s mechanical limits.
Standard signal specifications:
Pulse repetition rate:50 Hz (period = 20 ms)
Usable pulse width range:Typically 1.0 ms to 2.0 ms
Corresponding angle range:0° to 180°
This means the servo expects a pulse every 20 ms. By changing the pulse width from 1.0 ms to 2.0 ms, you command the shaft from 0° to 180°.
You buy twoMicro Servos from the same batch. One centers perfectly at 90° when you send a 1.5 ms pulse. The other stops at 85°. This happens due to manufacturing tolerances in the feedback potentiometer and mechanical assembly. Therefore, always calibrate each servo individually.
Most microcontroller platforms provide a built-in servo library that generates stable 50 Hz pulses. Below is a generic code example that works with any microcontroller supporting PWM output and servo control libraries.
Connect the servo’s power wire (red) to a 5V supply capable of providing at least 500 mA.
Connect the ground wire (brown or black) to the microcontroller’s GND.
Connect the signal wire (orange, yellow, or white) to a PWM-capable pin (e.g., pin 9).
#includeServo myServo; void setup() { myServo.attach(9); // Attaches servo on pin 9 } void loop() { myServo.write(0); // Command 0 degrees delay(1000); myServo.write(90); // Command 90 degrees delay(1000); myServo.write(180); // Command 180 degrees delay(1000); }
Thewrite(angle)function automatically converts the angle to the corresponding pulse width using the default mapping (0°→1.0 ms, 180°→2.0 ms). However, this default may not match your specific servo.
To achieve accurate angles, you must determine the exact pulse widths that produce 0° and 180° on YOUR servo.
1. Attach a pointer or mark the shaft’s neutral position.
![]()
2. Send a 1.5 ms pulse. The shaft should be near 90°. Note any offset.
3. Decrease the pulse width in steps of 10 µs until the shaft stops moving. That is your true 0° pulse width.
4. Increase the pulse width from 1.5 ms in steps of 10 µs until the shaft stops moving. That is your true 180° pulse width.
Typical measured results from three common servos:
These values show that assuming 1000 µs to 2000 µs can cause errors of up to 15°. Always use calibrated values.
Most servo libraries allow you to set custom pulse width ranges using anattach()overload or a separate function. Example:
myServo.attach(9, 540, 2420); // Pin, min pulse width (µs), max pulse width (µs)
After attaching with calibrated limits,myServo.write(90)will send the exact center pulse (1480 µs in this case), giving true 90°.
Step 1: Always calibrate each servo individually – Do not assume factory specifications. Spend 5 minutes measuring the true 0° and 180° pulse widths.
Step 2: Use a dedicated power supply – Do not power micro servos directly from the microcontroller’s 5V pin. A sudden current draw can reset the controller. Use a 5V 1A UBEC or a regulated external supply.
Step 3: Store calibration values in your code – After calibration, hardcode the min and max pulse widths. Example:
#define SERVO_PIN 9
#define SERVO_0_PULSE 540 // measured µs for 0°
#define SERVO_180_PULSE 2420 // measured µs for 180°
Servo myServo;
myServo.attach(SERVO_PIN, SERVO_0_PULSE, SERVO_180_PULSE);
Step 4: Verify with a simple test – Command 0°, 45°, 90°, 135°, 180°. Use a protractor to check accuracy. If any angle is off by more than 2°, repeat calibration.
Step 5: Document your settings – Write down the calibrated pulse widths for each servo. When you replace a servo, re-calibrate immediately.
Micro servo angle is directly controlled by pulse width. The standard mapping (1.0 ms = 0°, 1.5 ms = 90°, 2.0 ms = 180°) is a starting point. Real servos require individual calibration of minimum and maximum pulse widths to achieve true 0° to 180° accuracy. Without calibration, you may experience offsets of 10° to 20°.
By following the calibration procedure and customizing the pulse width range in your code, you will achieve repeatable, accurate servo positioning for any application – from robotic arms to camera gimbals. Always test each servo’s true limits and adjust your code accordingly. This practice eliminates guesswork and ensures your project works reliably every time.
Update Time:2026-04-07
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.