Home > Industry Insights >Servo
TECHNICAL SUPPORT

Product Support

How to Control a Servo Motor with Arduino: A Complete Step-by-Step Guide

Published 2026-04-03

Controlling aservomotor with an Arduino board is one of the most common and useful tasks in electronics projects. This guide provides a clear, practical, and verified method to connect and program aservousing the standard Arduinoservolibrary. You will learn the exact wiring, the working code, and how to troubleshoot typical issues—all based on real-world scenarios and official documentation.

01What You Need (Common Setup)

Most hobbyist servo motors (e.g., 9g micro servos or standard size servos like SG90, MG90S, or MG995) work with any Arduino board (Uno, Nano, Mega, etc.). For this guide, we assume:

An Arduino board (Uno is the most common example)

One standard 5V servo (common torque range: 1.8kg/cm to 13kg/cm)

Jumper wires (male-to-female for direct connection)

A separate 5V-6V power supply (optional but recommended for larger servos – explained in Section 4)

No specific brand names are required– the instructions apply to all standard analog servos that operate on a 50Hz PWM signal (pulse width from 500µs to 2500µs).

02Wiring – The Only Correct Connection

Follow this exact wiring to avoid damage or erratic behavior.

Servo Wire Color Function Connect to Arduino Pin
Brown or Black Ground (GND) GND
Red Power (VCC, 5V) 5V pin (or external power)
Orange or Yellow Signal (PWM) Digital pin 9 (or any PWM-capable pin)

Critical rule:Never power a servo directly from the Arduino’s 5V pin if the servo draws more than 200mA during operation. For most 9g servos, direct Arduino 5V is safe for testing. For larger servos (e.g., MG995), use an external 5V-6V power supply and connect the servo’s ground to the Arduino’s ground (common ground).

03The Standard Code – Copy and Run

The Arduino Servo library is pre-installed in the official IDE. This code moves the servo from 0° to 180° and back, repeatedly.

#includeServo myServo; // create servo object int servoPin = 9; // signal pin connected to servo void setup() { myServo.attach(servoPin); // attaches the servo on pin 9 } void loop() { myServo.write(0); // move to 0 degrees delay(1000); // wait 1 second myServo.write(90); // move to 90 degrees (center) delay(1000); myServo.write(180); // move to 180 degrees delay(1000); }

To control the servo to a specific angle(e.g., 45°), simply usemyServo.write(45);. The valid range is 0 to 180.

04Common Real-World Problems and Fixes (Based on Typical User Cases)

Case 1: Servo jitters or does not move

Cause:Insufficient power. A standard 9g servo draws 200–300mA when moving; a large servo draws up to 1A. Arduino’s onboard 5V regulator (max ~500mA) cannot handle it.

Fix:Use an external 5V power supply (e.g., 4xAA batteries or a 5V 2A adapter). Connect the supply’s positive to servo red wire, supply’s ground to Arduino GND, and servo ground to same GND.

Case 2: Servo moves only in one direction

Cause:Signal pin not correctly attached or wrong pin number.

how to control servo with arduino_how to control servo with arduino_how to control servo with arduino

Fix:Verify thatmyServo.attach(pin)uses a PWM-capable pin. On Arduino Uno, pins 3,5,6,9,10,11 support PWM. Pin 9 and 10 are recommended.

Case 3: Servo gets hot quickly

Cause:The servo is forced against a mechanical stop, or the pulse width range is incorrect for that servo model.

Fix:Adjust the minimum/maximum pulse width usingmyServo.attach(pin, min, max)where min defaults to 544µs (0°) and max to 2400µs (180°). Some servos require 500–2500µs. Check your servo’s datasheet.

05Advanced Control – Smooth Motion (No Library Required)

For precise speed control, you can manually generate the PWM signal. The servo expects a 50Hz signal (20ms period). A 1ms pulse = 0°, 1.5ms = 90°, 2ms = 180°. This code sweeps smoothly:

int servoPin = 9; int pulseWidth = 1500; // microseconds, start at 90° void setup() { pinMode(servoPin, OUTPUT); } void loop() { // sweep from 1000µs to 2000µs (0° to 180°) for(pulseWidth = 1000; pulseWidth = 1000; pulseWidth -= 10) { digitalWrite(servoPin, HIGH); delayMicroseconds(pulseWidth); digitalWrite(servoPin, LOW); delay(20 - pulseWidth/1000); } delay(1000); }

06Verifying Your Setup – A Quick Test Plan

1. Power check:With only the servo connected (no code uploaded), the servo should stay still and not get warm.

2. Signal test:Upload the standard code from Section 3. Observe if the servo rotates 0° → 90° → 180° → repeat.

3. Load test:Gently hold the servo horn while it moves. It should produce noticeable torque but not stall. If it stalls, increase power supply current.

07Core Principles to Remember (Repeated for Emphasis)

Correct wiringis non‑negotiable: signal to PWM pin, power to adequate supply, common ground.

Always use the Servo libraryfor simplicity and reliability. Only switch to manual pulse control when you need speed ramping.

Never exceed the servo’s voltage rating(typically 5V–6V). Higher voltage will destroy the internal control circuit.

Protect your Arduinoby never drawing more than 200mA from its 5V pin for a servo.

08Actionable Next Steps

1. Build the basic circuiton a breadboard using a 9g servo and an Arduino Uno. Use the code in Section 3 to verify movement.

2. Add a potentiometerto control the angle: read analog value from a pot (pin A0), map it to 0–180, thenmyServo.write(mappedValue).

3. Incorporate into your project– common applications include robotic arms, camera gimbals, steering mechanisms, and automatic door openers.

By following this guide, you now have a verified, repeatable method to control any standard servo with an Arduino. The same principles apply to all Arduino‑compatible boards. Always refer to your servo’s datasheet for exact pulse width limits and current requirements.

Update Time:2026-04-03

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