How to control a Servo using Pulse Width Modulation (PWM)
One of the major uses of using servos is in the field of robotics where high torque motors are require to make precise movements. Here we look at how to control a servo motor using pulse width modulation with an Arduino board.
The great thing about servo motors is they have been used in radio controlled models for many years and are readily available as well as fairly cheap. The RC servo motors also have quite a high torque for their size.
In this tutorial, we look at how to connect a servo motor to an Arduino board, then program it up to demonstrate how easy it is to move the servo using the Arduino platform.
So, Let’s get started.
What is a Servo motor ?
A servo motor is a device that turns an electrical signal into a rotary movement that can be precisely controlled. The servo motor we are looking at is an RC servo that is commonly used in radio controlled models. Their advantage is they are small, have high talk and are cheap. There are many variants of RC servos with the most common being the standard and micro sizes.
How servo motors work
The servo motor works by using pulse width modulation for control. A standard servo motor can rotate from 0 degrees to 180 degrees. Varying the pulse width between 1ms and 2ms varies the servo position between 0° and 180°. Although this is a general rule, servo motors do differ between manufactures and the 0-180 swing may not always be the case. In the demonstration further down the page, the servo motor I’m using is almost 30 years old, while it does work to some extent, controlling it with the Arduino will rotate it to a maximum of about 170 degrees, even so it is still vary usable. So when you get your servo working, you’ll need to experiment with it to see exactly how it responds to the Arduino programming.
Parts require for this tutorial
- Arduino Uno
- RC servo motor
- Hook up wire
- 12V DC External power supply
A 12VDC external power supply is required as the servo motor does draw a fair amount of current which the USB power may not be able to supply, so to be on the safe side, we will use an external power supply.
Servo Motor Arduino Schematic
The circuit we are using is connected as per the schematic below:
How to program the Servo Control to the Arduino Board
Once the circuit is assembled, we can start the coding. First we want to test the servo to make sure it can move to the 0° and 180° positions. We use the code below to test this by entering it into the Arduino IDE then uploading to the Arduino board.
Test 1 – Position motor to 180° position:
Test 2 – Position motor to 0° position:
Servo Movement Example to loop the motor 0° through to 180°
The servo movement example, sets the initial position of the servo to o degrees, then increments the servo position (degrees) until it reaches 180 degrees, then the program loops and starts over. Watch the video to see the results of this and the servo test codes above.
How to control a Servo using Pulse Width Modulation (PWM) – Summary
RC Servo motors are readily available, versatile and fairly inexpensive. They can be used to precisely move mechanical objects from 0° through to 180° and controlling them using the Arduino platform is easy and simple.
I hope this tutorial was useful to you.
Thanks for spending your time to read it !