Introduction
In this tutorial, we will be showing you how to control the servo motor with the Arduino. By the end of this tutorial you will be able to set up the servo motor so that it sweeps across 180 degrees.
Components you need
- Arduino UNO or similar
- Breadboard
- Jumper Wires
- Servo motor
New Components
The servo motor
The servo motor is a small motor that rotates a set angle.
The servo motor has 3 connections as shown in the diagram below:
The yellow wire connects to the digital pin and is used by the board to set certain angles for the servo motor. The black and red wires are used to power up the motor and connect to GND and to 5V respectively.
Connecting step-by-step
Step 1
Firstly, connect the yellow wire to one of the columns of the breadboard.
Step 2
Connect another pin of the same row to pin 12 on the Arduino using a jumpwire. The number of the pin is again important as this is what we will use when configuring our program.
Step 3
Next, connect GND to the negative power row (blue) of the breadboard and 5V to the positive power row (red).
Step 4
Next, connect the black wire to the negative power row of the breadboard and the red wire to the positive power row.
The final circuit should look similar to the diagram below:
Code
In this section, you will learn how to write the code for controlling the servo motor. You will be able to set an angle for the rotation of the servo motor.
As we did in the previous tutorial, we have put the code below so you can read it once
and then will run you through it.
Turning servo
Code explanation
Add servo library
#include <Servo.h>
Imports the code needed to use the servo
At the beginning of our code, we set the global variables and initial parameters. This is where we include the library for the servo. A library is a collection of pre-written code that you can reuse.
Set the servo pin
#define SERVO_PIN 12
The #define
allows us to give an integer value to a name. This helps make our code easier to read as we can now refer to pin 12
as SERVO_PIN
.
Initialize servo
Servo servo;
Initialises the servo object
Create angle variable
int angle = 0;
Sets the starting angle to 0
Attach servo object to a pin
servo.attach(SERVO_PIN);
Links pin number 12
to the inistialised servo.
Move servo one way
for(angle = 0; angle <= 180; angle++){
servo.write(angle);
delay(15);
}
The first for loop iterates over an angle from 0
to 180
, increasing with a step of 1
.
At every value of angle, we send the angle to the servo so it rotates.
The delay of 15
microseconds is used to give enough time to the servo so that it can rotate.
Move servo back
The second for loop acts the same way as the first but goes from 180
to 0
degrees, resetting the servo to its initial position.
Both these loops run indefinitely as they are in the Arduino's loop()
function.
You now have a program that makes the servo motor sweep an angle of 180 degrees. All you have left to do is upload it to your Arduino board and watch it go!
Exercises
Exercise 1
Change the range of angles and see what happens.
Hint: Read about the for
loop
Exercise 2
Set angles in steps of 5
: 0, 5, 10, 15
... up to 180
Hint: Find out about the update / icrement statement in a for
loop.
Exercise 3
Add a second servo.
Hint: The Arduino power output might not be enough to run 2 servos at once. You might need an external power supply for the servos. Find out how to build this circuit.
Challenge
For those of you that want more of a challenge, try blinking an LED every time a full cycle completes. This will require you to link 2 circuits together.
Hint: read the previous lesson about LEDs.