Hardware Reference
In-Depth Information
Discussion
Servo motors are often used in radio-controlled airplanes or cars. They generally don't
spin, but rather turn only 180°, just enough to control an elevator on a wing or steer wheels
on a car.
The servo is controlled by a PWM signal, which is controlled by the
analogWrite()
function, which sends a pulse 50 times each second (50 Hz or every 20 ms). The width of
the pulse determines the position of the motor. A short pulse (1 ms, for example) sends the
motor angle to zero degrees. A long pulse (2 ms, for example) sets the angle to 180 de-
grees.
angle every time it is called. The
setInterval()
function schedules
sweep()
to be
called every quarter second (250 ms) to step the servo to a new position.
At the end of the code, the
process.on()
function detects when the user has pressed ^C
(Ctrl-C), stops the timer, and turns off the motor. If you don't turn off the motor, it will
keep buzzing, even if it isn't moving.
Not all GPIO pins support the PWM hardware;
Recipe 3.4
discusses which pins you can
use.
NOTE
If your servo motor needs to run off 5 V, wire it as shown in
Figure 4-2
. The
banded
wire at-
taches the Bone's 5 V power supply (
P9_7
) to the power supply on the servo. We are still con-
trolling it with a 3.3 V signal.
Here, we move the
banded
power wire from the 3.3 V power supply to the Bone's 5 V power
supply on pin
P9_7
. If the Bone's built-in 5 V doesn't supply enough current, or if you need a
higher voltage, connect your servo's power supply wire to an external power supply.