In this project you will learn about how to control a small DC Motor using an arduino and a transistor. You will also use an arduino analog output(PWM) to control the speed of the motor by sending a number between 0-255 from digital pin of arduino.
DC motor runs on DC voltage. The toy cars have one or more DC motors to move around. In robotics it is the most popular motor used in various projects and models and robots.It has two power connections to apply the voltage one +ve and the other -ve. If we apply at least 1.5 V DC to the motor it will start moving in a clockwise direction. If we reverse the polarity it start moving in opposite direction.
Firstly we will try to turn ON and OFF the DC motor using n-p-n transistor 2N2222. An n-p-n transistor has three pins namely emitter, base and collector.
In transistor current can flow through collector to emitter depending on the state of the base pin with respect to the emitter. If the base is at higher potential than emitter, we have current flowing from collector to emitter. This can be restricted if the base is grounded. So basically here transistor is used as a switch.
COMPONENTS REQUIRED :-
- Arduino Board
- DC MOTOR(9-12)v
- resistor(1K ohm)
- Potentiometer(10 K Ohm)
HOW TO SET UP THE CIRCUIT :-
CODE REQUIRED :-
Digital pin 3 will be initialized as the motor pin. It is configured as “OUTPUT” pin. The motor turns on when the “HIGH” command is sent and turns off when the “LOW” command is sent to pin 3. Both the times a delay of 1 second is given.
The ON and OFF of the motor can be controlled by incorporating an push button also by editing the code accordingly.
int motorPin = 3; //declare the pin to which the base of the transistor is connected.
pinMode(motorPin, OUTPUT); //intialise pin 3 as the output
NOW WE WILL CONTROL THE MOTOR SPEED BY USING A POTENTIOMETER!
To go further with the DC motor we will now control the speed of the DC motor using PWM(pulse width modulation), which we generated last time. The project using a potentiometer of 10K ohm to control the speed of the motor. As we rotate the pot in clockwise direction the speed of the motor will increase and when we rotate it in anti-clockwise direction the speed of the motor will decrease.
The circuit diagram is same as given above which actually was using the pot. You can assemble the pot experiment by yourself.
CODE REQUIRED AND EXPLANATION :-
int motorPin = 3;
int potPin = A0;
int porValue = 0;
potValue = analogRead(potPin);
int speedLevel = map(potValue, 0, 1023, 0, 255);
We are using analogRead to read the position of the potentiometer from analog pin ‘A0’. The speed of the motor is being changed by using analogWrite command at digital pin 3 which is capable of PWM control. In short, we will be using PWM to control the motor speed which requires analog voltage.
Since the analog reading will be between 0 and 1023, and the analog output will be between 0 and 255, we are again using map function to reset the range. Speed control for DC motors of higher ratings can be obtained by choosing a transistor or MOSFET of higher current and voltage rating than the motor.