In the previous tutorial, we discussed what a servo motor is, the parts of a servo motor and how a motor works. In this tutorial, we shall discuss how to use a servo motor with arduino. Then, after we have learnt how to use a servo motor with arduino, we will start designing robots with it. Trust me that the tutorials are going to be awesome.
To start with, we shall demonstrate how to use a servo motor with arduino, i.e. how to control servo motor with arduino — a very easy task.
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.
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.
Servo Motor Operation
Like we stated previously, using arduino to control servo motor is very simple with the help of 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.
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.
Another way to operate a servo motor is to operate it with a sensor; the sensor will prompt and direct the motion of the servo. In our next tutorial we shall discuss that. Meanwhile you can watch the video of this tutorial to learn more.