Hardware Reference
In-Depth Information
Capacitor
Transistor
Battery
DC motor
Diode
Potentiometer
Figure 4-4: Adding a potentiometer
You can now modify the program to control the motor speed based on the
present setting of the potentiometer. With the potentiometer at zero, the motor
stops; with the potentiometer rotated fully, the motor runs at full speed. Recall
that the Arduino is running quite fast; it's actually running through the loop
several thousand times every second! Therefore, you can simply check the
potentiometer speed each time through the loop and adjust the motor speed
after each check. It checks often enough that motor speed adjusts in real time
with the potentiometer. The code in Listing 4-2 does the trick. Create a new
sketch (or update your previous sketch to match this code) and upload it to your
Arduino from the integrated development environment (IDE).
Listing 4-2: Adjustable Speed Control—motor_pot.ino
//Motor Speed Control with a Pot
const int MOTOR=9; //Motor on Digital Pin 9
Search WWH ::




Custom Search