Control a motor using NPN transistor

You may have heard of the transistor, and you may have also learned that it is a major reason for the technological advancement we are now witnessing.


A transistor is a device that can be used as a switch. It is made from a special material and has at least three terminals for easy connection to an external circuit.


In this tutorial, we will learn how to use a NPN transistor to operate a DC motor. It will provide the motor with the necessary power.


Overview


The transistor has three pins: the base, the collector, and the emitter.


The base pin receives the control signal from the Arduino, while the emitter is connected to one of the motor wires to control it. The collector pin will be connected to the Arduino GND pin to complete the circuit.


NPN transistors are normally "off" (disconnected), unless power is applied to the base pin it will turn on.


That means when the base receives a high signal from the Arduino, your motor will be turned ON. But when the base receives a low signal, your motor will be turned OFF.


Getting the items


For this project you will need the following components you can buy them from our store.

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat 2N2222 - NPN Transistor (3 pieces)
1 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR
Sale Off
Voltaat Small Brushed DC Motor
7 QAR

Wiring it up


To set up the NPN transistor with the motor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the NPN transistor, the motor and the Arduino. Once they are connected to each other, connect the Arduino to your computer using the USB cable.


Connections from the NPN transistor:
• NPN emitter pin → Arduino GND pin
• NPN base pin (middle pin) → Arduino pin 3
• NPN collector pin → Motor first wire

Connections from the motor:
• Motor first wire → NPN collector pin
• Motor second wire → Arduino VCC pin (5V)

Coding


The function of this sketch is to control a DC motor using the Arduino with the help of the NPN transistor. The motor will be switched on for one second, then turned off, and the process will be repeated.


This code is basic and straightforward, and no libraries are required.


Let's put it to the test together!


/*
Voltaat learn (https://www.voltaat.com/pages/voltaat-learn)
Link for full tutorial: https://bit.ly/3DyiYYw

Tutorial: Control a motor using NPN transistor

The function of this sketch is to control a DC motor using the Arduino with the help of the NPN transistor.
The motor will be switched on for one second, then turned off, and the process will be repeated

Connections from the NPN transistor:
• NPN emitter pin → Arduino GND pin
• NPN base pin (middle pin) → Arduino pin 3
• NPN collector pin → Motor first wire

Connections from the motor:
• Motor first wire → NPN collector pin
• Motor second wire → Arduino VCC pin (5V)

*/

//Commands inside void setup run once
void setup(){
//Pin 3 is defined as an output
pinMode(3,OUTPUT);
}

//Commands inside void loop run forever
void loop(){
//Turn the motor on
digitalWrite(3,HIGH);
//wait for a second
delay(1000);
//Turn the motor off
digitalWrite(3,LOW);
//wait for a second
delay(1000);
}

Testing it out


After uploading the code to your Arduino board, you will observe the motor turning on and off every one second. You may modify this period by changing the value in the code for the delay function and trying out various outcomes.


What are your thoughts on the ideal application for this project?


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials




Most of us are familiar with the switches used in household devices. A relay is a type of switch that can be connected to an Arduino or any other microcontroller.



In this tutorial, we will learn how to use a PNP transistor to operate a DC motor. It will provide the motor with the necessary power.


In this tutorial, we will learn more about bush buttons and how to use them. We'll use the push button in a simple project to control the Arduino's internal LED and print the current state of the button directly on your commuter.