Making Things Move

The Arduino can make things move using electric motors. However, there is a problem with connecting a motor directly to the Arduino - motors need lots of electric current, and the Arduino can't supply that much current.

There are several solutions to this problem. The first and most useful solution is to use a small servo motor. This type of motor can be small enough not to use much current, and the "servo" type of motor doesn't run continuously. Instead, it runs for a short time and moves to a particular position, then stops. Servos are used in a wide variety of applications, the small ones are particularly useful to steer remote controlled vehicles (RC cars and drones). They are also very important for building robots.

A servo must be provided with electrical power, so it must be connected to the +5V and GND pins on the Arduino (red and black wires are traditional). To control how much it moves or rotates, there is a third wire that must be connected to one of the digital outputs that can produce Pulse Width Modulated (PWM) signals. We will use digital pin 6 for this example. See the picture below for the wiring.

Now plug the Arduino into your computer, open the IDE and copy the following sketch:

//Trying out servo control
//Richard Taylor 2015-08-27

#include <Servo.h> //include the servo library

Servo servo; //declare a servo object

void setup()
{
servo.attach(6); //servo control on digital PWM pin 6

}

void loop()
{
for(int i=0; i<=18; i++) {
servo.write(i*10);
delay(1000);
}
}

Arduino comes with a library of useful software functions called Servo.h so that you don't need to worry about the details of the PWM signals. As you see in the code, you simply have to include the library, declare a servo object, attach that servo object to a particular digital output pin, then write an angle in degrees to the servo. The "for loop" writes the values 0, 10, 20, 30,... 180 to the servo with a 1 second (1000 ms) delay between each write. This will make the servo turn at ten degree intervals half way around a circle and then back to zero as the main loop repeats. Servos are usually limited to a small range of angles.

When you have this working, see if you can think of something small and light-weight that you could attach to the servo arm. Then modify the sketch to make that thing move in an interesting way. Can you use levers to amplify the movements?

Back to the main menu.