A servo motor is small motor that allow the precise control of the angular position of its shaft.  Mini servo motors are used in all kinds of toys, RC cars and airplanes as well as robots.  Using a mini servo motor, you can control the direction of your robot or vehicle.

Note:  Mini servos have a maximum angle of 180 degrees.

Let's give it a try!


Three wires - What are they

You connect and control your servo using only 3 wires:

  • Brown: The brown wire is the ground (GND) and you always connect it to the GND pins.
  • Red: Power supply.  Just like all other red wires, you connect this one to the 5V power supply.  This wire will power the motor.
  • Orange/Yellow: This is the signal that controls the exact angle of the servo.  The signal sent is using PWM.  We will learn more about PWM in a later edition.  For now, you will be using commands available in the library.



Connect your servo as shown on page 20 of the PodPi magazine edition #1.  Make sure to connect the signal wire to pin 11.  Only certain pins of your Arduino board support the PWM protocol.  pin 11 is one of them.  If you look at your Arduino (without the shield), you will see some pins with their number and a little tilde (~) sign.  These are the PWM compatible pins.  If you want to control multiple servo motors, each servo signal wire needs to be connected to a PWM pin.

The code

var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var servo = new five.Servo({pin:11});

Control Functions/Methods

The Servo object in your code has multiple methods available for you to control it.  methods are function that can be called with specific arguments that will perform a specific task.  For example, .on() and .off() are two different methods of the Led object that are used to turn an LED ON and OFF.

Here are the methods available for Servo:

.sweep() - It will sweep the servo back and forth

.sweep({range:[x, y], interval: i, step: s}) - same as the sweep() method, but with additional arguments as follow:

  • [x, y] replace x and y with the minim and maximum angles you want to sweep for
  • iis an integer used to set the time interval in milliseconds to reach the minimum and maximum angles
  • s is an integer used to specify the number of steps to take during a sweep

.to(angle) - This method will set the servo to the angle specified in the value angle.  An angle can only be a value from 0 to 180.

.stop() - Used to stop the movement of a servo.