In this tutorial, we shall discuss how to use a servo motor with Arduino. In the previous tutorial, we discussed what a servo motor is, the parts of a servo motor and how a motor works. After we have learnt how to use a servo motor with Arduino in this tutorial, we will start designing robots with it. Trust me that the tutorials are going to be awesome.
We shall demonstrate how to use a servo motor with Arduino, i.e. how to control a servo motor with Arduino — a very easy task.
Before we start, let’s discuss a bit about servo motors.
A servo motor is a self-contained sophisticated electromechanical device that rotates parts of a machine or robot with high efficiency and great accuracy. A servo motor can move slowly and at the same time deliver large torque with great precision and accuracy. This is the reason why it is utilized in robot design, industrial automation, control surface positioning in remote control vehicles, etc.
Above, we stated that servo motors are utilized in robot design, industrial automation, etc. a servo motor is utilized in so many devices and systems, and the list is as follows:
- Radio-controlled model car, airplane, or helicopter,
- Rotating a shaft connected to the engine throttle.
- Regulates the speed of a fuel-powered car or aircraft.
- Electronic devices such as DVD and Blu-ray Disc players use servos to extend or retract the disc trays.
- In autonomous cars, servos manage the car’s speed. The list goes on. In robots servo motor is an indispensable component.
Below is the circuit connection for the design:
The arduino code to operate a servo motor is very simple, however, before we write the code to operate the servo motor we will first list out the various arduino functions used to operate and control a servo motor.
List of Arduino Functions to Use a Servo Motor
attach(): this function is used to attach the servo signal pin to an arduino pin. E.g. my_servo.attach(12), where my_servo is the servo variable name, 12 is the arduino pin on which the servo is attached.
write(): this is the function used to write information to the servo motor. Here, what we write to the servo motor is the desired angle from 0° to 180°. E.g. my_servo.write(90). This code will rotate the servo motor shaft to 90° position.
writeMicroseconds(): this function is used to send PWM signal in microseconds to the servo motor to move it. This is actually the basic way of operating a servo motor. We have talked about it in the previous tutorial tutorial on How servo mottor works. Most servo motors with 0°-180° angles of rotation are mapped to 1000 – 200 microseconds.
E.g. writeMicroseconds(1500) will rotate the servo motor shaft to 90° position.
read(): this function is used to read the last angular value written to a servo variable. Syntax is my_servo.read(). It will return the angle of servo motor written last.
attached(): this function checks if the servo variable is attached to a pin. It returns a true if actually the servo variable is attached to a pin, and a false if otherwise. Syntax: my_servo.attached().
detach(): this function detaches a servo variable from its pin. Syntax: my_servo.detach().
Please note that, “my_servo” as we used here can be any variable name or variable identifier you choose. Learn how to name variables in arduino.
How to Use or Operation a Servo Motor
Like we stated previously, using Arduino to control a servo motor is very simple with the help of the Arduino servo motor library. The Arduino servo motor library comes preinstalled in the Arduino Integrated Development Environment (IDE). However, you can still install the library yourself if you want to. You can check out our tutorial on how to intall Arduino library.
Arduino Sketch for Controlling a Servo Motor
Open the Arduino IDE and navigate to Files > Examples > Servo > Sweep. See image below.
Click on the sweep button and you will see the window shown below.
The code lines of the sketch are explained in the comment parts.
When you upload the sketch into your Arduino board, the servo will turn to the 180° position in 1° steps and turn back to the 0° position in 1° steps. This step-wise rotation is made possible thanks to the “for loop function”. The operation continues without stopping because the code for the process is in the loop function of the sketch. This is a basic way of demonstrating how a servo motor works.