Controlling a Motor an NPN Transistor on the Arduino 101
by TechMartian in Circuits > Arduino
6552 Views, 10 Favorites, 0 Comments
Controlling a Motor an NPN Transistor on the Arduino 101
![9D3EPBafTeOSjwyF4+eGyg_thumb_20c.jpg](/proxy/?url=https://content.instructables.com/FIH/9J9R/J52Q522O/FIH9J9RJ52Q522O.jpg&filename=9D3EPBafTeOSjwyF4+eGyg_thumb_20c.jpg)
This is the tenth module in the series of lessons about applications of the Arduino 101, and will focus on the use of DC motors. DC motors are very common in the realm of mobile robotics. Most robots that require some sort of motion, usually for actuating wheels use DC motors since they are relatively cheap and easy to code. This project focus on making the simplest circuit to control a DC motor without a motor driver shield or H-bridge IC, but simply with an NPN transistor
Tools and Materials
![HEzTz3OQQlacBXoxOg4S1w_thumb_20a.jpg](/proxy/?url=https://content.instructables.com/F14/5PD6/J52Q522M/F145PD6J52Q522M.jpg&filename=HEzTz3OQQlacBXoxOg4S1w_thumb_20a.jpg)
- Arduino 101 or Arduino Uno
- Breadboard
- NPN Transistor
- 100Ω Resistor
- DC motor
- 1N4148 Diode
- Jumper Wires
Circuitry
![Screen Shot 2017-07-13 at 11.29.12 PM.png](/proxy/?url=https://content.instructables.com/F6O/GEUB/J52Q51GW/F6OGEUBJ52Q51GW.png&filename=Screen Shot 2017-07-13 at 11.29.12 PM.png)
![%IOIzfYKT5ecnl7Rl51tGQ_thumb_20d.jpg](/proxy/?url=https://content.instructables.com/FGF/KJU0/J52Q522N/FGFKJU0J52Q522N.jpg&filename=%IOIzfYKT5ecnl7Rl51tGQ_thumb_20d.jpg)
Connecting the Arduino power to the breadboard
- Connect the 3.3V pin from the Arduino to the red power rail of the breadboard using a red jumper wire.
- Connect the ground pin from the Arduino to the black power rail of the breadboard using a black jumper wire.
- Connect the 5V pin from the Arduino to an unconnected hole in the breadboard with an orange jumper wire. This will be used to power the DC motor.
Wiring the transistor
- Connect the middle pin to a 100Ω resistor and insures to pin 9 on the Arduino.
- Connect one of the other pins to the ground rail on the breadboard with a black jumper wire.
- The remaining pin of the transistor will be connected to the motor ground with a brown jumper wire.
Wiring the the motor.
- Connect one of the motor wires to the 5V pin you connected with the orange jumper wire previously. Note that when connecting the motors, the order of the wire connection does not matter, it simple means that the motor will turn the opposite direction.
- Connect the other motor wire to the outer leg of the transistor not connected to the ground power rail using a brown jumper wire.
- Connect the diode between the two motor wires with the negative pin marked by a black line on the diode connected to the 5V pin with the orange wire.
Code
![Screen Shot 2017-07-13 at 11.47.36 PM.png](/proxy/?url=https://content.instructables.com/FDL/HZGD/J52Q51W2/FDLHZGDJ52Q51W2.png&filename=Screen Shot 2017-07-13 at 11.47.36 PM.png)
// constant pin for the transistor connected to the motor
const int motorPin = 9;
void setup() { //set motorPin as OUTPUT pinMode(motorPin, OUTPUT); }
void loop() { // milliseconds to turn the motor on int onTime = 3000; // milliseconds to turn the motor off int offTime = 3000;
// turn the motor on (full speed) digitalWrite(motorPin, HIGH); // delay for onTime milliseconds delay(onTime); // turn the motor off digitalWrite(motorPin, LOW); // delay for offTime milliseconds delay(offTime); }
Demo
![DC motor with transistor](/proxy/?url=https://content.instructables.com/F3S/XPOA/J52Q51PG/F3SXPOAJ52Q51PG.jpg&filename=DC motor with transistor)
The DC motor will spin in one direction and stop for 3 seconds, then spin again in full speed. You can also change the speed by using analogWrite (motorPin, motorSpeed);