Hardware Reference
In-Depth Information
Discussion
This is actually the same code that you used to drive the servo motor (
Recipe 4.1
). The
only difference is how the parameters are set. We are now using the PWM hardware to con-
trol the speed of the motor rather than the position. If the duty cycle is
1
, the voltage to the
motor is on all the time, and it runs at its fastest. If the duty cycle is
0.5
, the voltage is on
half the time, so the motor runs slower. A duty cycle of
0
stops the motor.
You can use this same setup to drive a solenoid. After all, a solenoid is a DC motor that
goes back and forth rather than spinning. Generally, you would drive a solenoid with an on
or off signal, as opposed to using a PWM signal.
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 con-
tinue to spin.