How to Control Servo Motor with Arduino

Servo motors are widely used in various applications, such as robotics, automation, and remote-controlled devices, where precise motion control is required. Arduino is a popular microcontroller platform that makes it easy to control servo motors. This article will guide you through the process of using Arduino to control a servo motor, explaining the necessary hardware components, connections, and programming steps to successfully achieve precise and customizable motor control.

1. Hardware Components

To control a servo motor with Arduino, you will need the following components:
- Arduino board (e.g., Arduino Uno, Arduino Nano, or Arduino Mega)
- Servo motor (usually comes with three wires: power, ground, and signal)
- Jumper wires
- Breadboard (optional, for easy prototyping)

2. Hardware Connections

Follow these steps to wire the servo motor to the Arduino board:
a. Connect the servo motor's power wire (typically red) to the 5V pin on the Arduino board.
b. Connect the servo motor's ground wire (typically black or brown) to one of the GND pins on the Arduino board.
c. Connect the servo motor's signal wire (typically orange or yellow) to one of the Arduino board's digital pins, such as pin 9. This pin should support Pulse Width Modulation (PWM).

3. Arduino Programming

To control the servo motor using Arduino, you need to write a program that sends the appropriate control signals to the motor. Here's a step-by-step guide to create an Arduino sketch for servo motor control:
a. Open the Arduino IDE on your computer.
b. Start a new sketch and include the built-in Servo library by adding the following line at the beginning of your code:
c. Instantiate a Servo object and define the digital pin you connected to the servo motor's signal wire:
Servo myservo;
int servoPin = 9;
d. Initialize the servo motor in the setup() function by calling the attach() method:
void setup() {
e. Write a loop() function to control the servo motor movement. To change the motor's position, call the write() method on the Servo object using the desired angle (in degrees) as a parameter:
void loop() {
  for (int angle = 0; angle <= 180; angle++) {
  for (int angle = 180; angle >= 0; angle--) {
This example will sweep the servo motor back and forth between 0 and 180 degrees.
f. Once you have completed the sketch, connect your Arduino board to your computer, select the correct board and port in the Arduino IDE, and upload the code.

4. Customizing Servo Motor Control

The provided example demonstrates a simple servo motor sweep; however, you can customize the Arduino code to enable more advanced or tailored control:
- Adjust the sweep range: Change the loop() function's angle limits to move within the desired range of motion.
- Multiple servo motors: Instantiate and control additional Servo objects, connecting their signal wires to different digital pins that support PWM.
- Responsive control: Use input devices like potentiometers, buttons, or sensors to change the servo motor's position based on user interaction or environmental feedback.


Controlling a servo motor with Arduino is a straightforward and versatile process that involves wiring the motor to the appropriate pins, creating and uploading the appropriate code, and customizing the code as needed to achieve the desired motion control. With this guide, you can easily incorporate servo motor control into your Arduino projects, enabling a wide range of applications including robotics, automation, and interactive devices.