Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Controlling Servos with Arduino

Created on: 10 September 2012

Arduino makes it so easy to connect and control a servo of the RC hobby type. Only three wires need to be connected between the Arduino board and the servo.

Servos make it possible to animate a project. Most servos will have a shaft that will rotate between 0 and 180 degrees. Servos usually come with various attachments that connect to the shaft allowing the rotational motion of the shaft to be changed to linear motion, i.e. the servo can then perform a pull and push action.

The Arduino Sketches

Two sketches from the examples that are built into the Arduino IDE are used in this tutorial. The first sketch "sweep" rotates the servo shaft from 0 degrees to 180 degrees and back continuously. The second sketch "knob" allows a potentiometer to be used to rotate the servo.

This video shows the two sketches in operation:

Can't see the video? View on YouTube →

The Servo

An analog or digital servo can be used. Make sure that the servo is rated to operate from 5V. In this tutorial, a servo is used that is rated to operate from 4.8V to 5.5V

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual

The Circuit

Connect the red wire from the servo to 5V on the Arduino and the brown wire from the servo to GND on the Arduino. The orange wire from the servo is to be connected to Arduino digital pin 9 for this tutorial.

When running the Knob sketch, a potentiometer is connected with the middle pin connected to Arduino analog pin A0. One of the outer pins of the potentiometer is connected to 5V and the other to GND.

The Sketches

After connecting the servo to the Arduino, load the built-in Sweep sketch to the Arduino board. The sketch can be found in the Arduino IDE under File → Examples → Servo → Sweep

With the servo and potentiometer connected to the Arduino, the Knob sketch can be loaded to the Arduino. This sketch can be found in the Arduino IDE under File → Examples → Servo → Knob