The last kind of shield that we are going to make is a motor speed controller. To build this shield, you will need the following parts:
- DC Motor
- NPN Power Transistor
- 1 kohm Resistor
- Jumper Wires
- Battery Connector
- Screw Terminal Connector
- LED (optional)
- 1 kohm Resistor (optional)
The simplest kind of speed controller uses a Pulse Width Modulation (PWM) signal. This signal can be generated by any of the PWM pins on an Arduino. Unfortunately the digital pins can only output a maximum of 40 mA. This isn’t enough to power most motors. So we need to use an external power source such as a battery pack.
When working with an external power source, you need to use a power transistor. By connecting the PWM pin on the Arduino to the base pin of a NPN transistor, you are able to control the motor with the signal from the Arduino.
In addition to the transistor, you will need to add a resistor to limit the current going to the base pin of the transistor. You will also need to add a diode in parallel with the motor. This will help to protect the Arduino from voltage spike that may occur when the motor turns off.
Optionally, you can also add an LED in parallel with the motor to give a visual indication of the speed setting. I included this in my shield.
First connect the positive wire from the battery pack connector to the circuit board adjacent to the Vin pin. Then solder the end of the wire to the Vin header pin. Connect the negative wire from the battery connector to the board adjacent to the GND pin and connect the two together.
Next mount the transistor onto the board and connect the emitter pin to the GND pin on the Arduino.
Add a jumper wire to connect these pins to one terminal of a screw connector. Connect the base pin to one of the PWM pins on the Arduino with a 1 kohm resistor. Lastly connect the collector pin to one of the terminals of a screw connector with a jumper wire. Connect the other terminal of the screw connector back to the Vin pin.
If you want to add an LED as a visual indicator, you need to connect it between the PWM pin and GND. Take a resistor and connect one end to the same PWM pin that is connected to the base of the transistor. Connect the other side of the resistor to the anode of an LED. Connect the cathode of the LED to the GND pin.
Now your motor speed controller shield is complete. Just connect the motor to the screw connector. Use the analogWrite command to send a PWM signal to the motor and control its speed.