Controlling the Speed of a DC Motor
You want to control the speed of a DC motor, using your Raspberry Pi.
You can use the same design as Recipe 9.4. It is, however, a good idea to place a diode across the motor to prevent voltage spikes from damaging the transistor or even the Raspberry Pi (Figure 10-5). The 1N4001 is a suitable diode for this (see “Transistors and Diodes” on page 381). The diode has a stripe at one end, so make sure that this is the right way around.
For low-power motors (less than 200 mA), you can base the design on the one used for a relay in Recipe 9.5. You will need:
• 3V to 12V DC motor
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• 1kΩ resistor (see “Resistors and Capacitors” on page 380)
• Transistor 2N3904 (<see “Transistors and Diodes” on page 381)
• Diode 1N4001 (see “Transistors and Diodes” on page 381)
• Power supply with voltage to match the motor
If you are only using a low-power DC motor (less than 200 mA), you can use a smaller (and cheaper) transistor (Figure 10-6).
You can probably get away with powering a small motor from the 5V supply line on the GPIO connector. If you find that the Raspberry Pi crashes, use an external power supply, as shown in Figure 10-5.
To control the speed of the motor, you can use the program from Recipe 9.8 (gui_slider.py). You can also download the program from the Code section of http://www.raspberrypicookbook.com, where it is called gui_slider.py. Note that this program uses a graphical user interface, so you can’t run it from SSH. You must run it from the windowing environment on the Pi itself or via remote control using VNC (Recipe 2.8).
Discussion
This is actually the same circuit as you used to switch a relay in Recipe 9.5, except with the motor in place of the relay coil. See Recipe 9.4 for a description of how it works.
See Also
This design only controls the motor’s speed. It can’t control its direction. For that, you need to see Recipe 10.4.
For more information on using a breadboard and jumper wires with the Raspberry Pi, see Recipe 8.10.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-4. Resistors and capacitors | |
270Ω 0.25W resistor | Mouser: 293-270-RC |
470Ω 0.25W resistor | Mouser: 293-470-RC |
1kΩ 0.25W resistor | Mouser: 293-1k-RC |
3.3kΩ 0.25W resistor | Mouser: 293-3.3k-RC |
4.7kΩ 0.25W resistor | Mouser: 293-4.7k-RC |
10 kΩ trimpot | Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF |
Photoresistor | Adafruit: 161, SparkFun: SEN-09088 |
220nF capacitor | MCM: 31-0610, Mouser: 80-C322C224M5U5HA |
Table A-5. Transistors and diodes | |
FQP30N06 N-channel MOSFET transistor | SparkFun: COM-10213, Adafruit: 355 |
2N3904 NPN bipolar transistor | SparkFun: COM-00521, Adafruit: 756 |
1N4001 diode | SparkFun: COM-08589, Adafruit: 755 |