Introduction: 12V Motor Control With 5V Arduino and NPN Transistor As Speed Control Switch
If you need to control a DC motor that exceeds the max output of your microcontroller then follow this instructable and if you've any questions along the way I'd be happy to help.!
What is covered:
- Parts List and Tools
- Determining the positive terminal of your power supply
- Determining specifications of required components
- Build instructions including images and links circuit io where you can simulate the project before ever building it.!
- Basic Arduino Code to control the motor to Start, Stop and control the speed of your motor using Pulse Width Modulation (PWM)
Parts List and Tools
- Arduino (I've used an Arduino Micro but any Arduino will suffice)
- 12V (or equivalent >5V) DC Motor
- Power Supply (Choose your power supply based on the motors requirement - I found an old 12V power supply in my shed that does the trick)
- NPN Transistor (Refer to Step 2) - BD139 NPN
- Diode to protect transistor for motor flyback (Refer to Step 2) - 1N5819 1A Schottky Barrier Rectifier
- Male to Male Jumper wires
- 1kΩ Resistor
- Multimeter; to determine the positive terminal of the power supply and the minimum current requirement of the motor
- Tape; to mark the power supply positive terminal
Step 1: Test Power Supply Positive Terminal
1. Connect multimeter red wire to the Voltage/Resistance port and the black wire to the ground port
2. Set multimeter to DC voltage
3. Separate the 2 power supply output wires and connect them to each of the multimeter terminals ensuring there's no contact between the ends of the power supply wires
4. Switch on the power supply and read the voltage value on the Multimeter
- If the reading is positive, then the power supply output connected to the Multimeter red lead is the positive terminal
- If the reading is negative (as shown in the image provided), then the power supply output connected to the Multimeter black lead (ground) is the positive terminal
5. Put a piece of tape on your power supply positive wire for future reference when connecting it to your circuit
Step 2: Choosing a Suitable Transistor and Flyback Diode
After determining the positive wire from the power supply in Step 1 we can now determine the spec for our transistor that will be used as the motor control switch and the diode required for motor flyback.
- Place the Multimeter red wire to the Amperage port and set the meter to read Current
- Connect the Multimeter positive terminal (red wire) to the power supply positive wire which we previously marked with tape
- Connect the Multimeter ground (black wire) to one of the motor terminals
- Connect the motors second terminal to the power supply ground wire
- Switch on the power supply and read the current on the Multimeter - This is the Zero Load motor current
- Now switch off the power supply and hold the motor pinion so that it has a high physical resistance
- Switch on the power supply and the motor will fail to start due to the physical resistance. The current reading should now be much higher. This is the Max Load current
The required operating current will be between the Zero Load and the Max Load current so depending on what you want to drive with the motor you can determine a suitable transistor spec.
As I'll be using this motor circuit to power an auger in a future fish feeder project, the Zero Load of 510mA will not be exceeded by more than 100mA so I'm going to use a BD139 NPN transistor (1st image). This transistor has a max current rating of 1Amp. The diode I have used is a 1N5819 1A Schottky Barrier Rectifier which is a type of diode that allows both very fast switching and low forward voltage drop which is perfect for speed control using PWM.
If your required load is higher than the max current spec of any transistors you have then you can split the current across multiple transistors as shown in the following circuit (2nd image): Motor load shared over multiple NPN Transistors - EveryCircuit
Step 3: Build
- Place the Arduino on the breadboard with the pins divided by the board gap
- Place the transistor in a free space on the breadboard away from the Arduino pin lines
- Place the 1 kΩ resistor between Arduino pin 3 to the base pin of the transistor (in this case it's the left pin if looking at the transistor metallic side)
- Setup a ground rail by connecting the Arduino ground, the power supply negative wire and the transistor emitter to a single line on the breadboard
- Connect the Transistor Collector to one of the motor connections. Here's a link to why the transistor should be placed after the motor
- Connect the motors second connection to the positive terminal of the power supply
- Position the diode across the motor input and output breadboard rails ensuring the silver band on the diode is closest to the power supply positive rail
That's it.! It's ready to be controlled!!!
Step 4: Code & Test
- Upload the Arduino code (1st image) which is available in the attached file called Motor_Basic.ino
- Open the Arduino serial monitor via the Arduino ID-->tools-->Serial Monitor (2nd image)
- Watch the PWM value and the state of the motor as it gradually slows down
- Record the value at which the motor is no longer running. This is the minimum PWM frequency at which you can code your motor control with Zero Load.
- Once the code has run through the PWM frequency range from 255 to 0 it will now start incrementing in the opposite direction from 0 to 255. Record the value at which the motor starts to turn. This is the minimum Zero Load motor start frequency.
Note: You can repeat this test when the motor is within a project and under the required load in order to determine the minimum operating start and stop PWM frequency under normal operating conditions within your project.
Now you can use these values and manipulate and insert this basic motor control logic into any project requiring a DC motor control, even if it's for motors of higher power rating than your microcontroller can provide.!
You can test this project out before ever wiring anything up by following this link to a Circuits.io version of this motor control.
If you have any questions relating to any part of this project please ask in the comments section and I'll do my best to provide you with the required information.