DC motors are widely used in a variety of applications, from simple toys to complex industrial machines. Controlling the speed and direction of a DC motor can be done using an Arduino microcontroller and a few additional components such as a transistor and a diode. In this blog post, we will be discussing how to control a DC motor with an Arduino, a transistor, and a diode.
Circuit Diagram: The first step in controlling a DC motor with an Arduino is to create a circuit diagram. The circuit diagram should include the Arduino, the transistor, the diode, and the DC motor. The transistor is used to amplify the current flowing through the motor, and the diode is used to protect the transistor from voltage spikes caused by the motor.
Connecting the components: Once the circuit diagram has been created, the next step is to connect the components. The Arduino should be connected to the transistor, and the transistor should be connected to the diode and the DC motor. The diode should be connected in reverse bias, with the anode connected to the collector of the transistor and the cathode connected to the motor.
Writing the code: After the components have been connected, the next step is to write the code for the Arduino. The code should include instructions to control the speed and direction of the motor. The code should also include instructions to read the state of the motor and update the speed and direction as needed.
Uploading the code: Once the code has been written, it should be uploaded to the Arduino using a USB cable. The Arduino will then execute the code and control the speed and direction of the motor.
Testing the motor: After the code has been uploaded, the motor should be tested to ensure that it is working properly. The speed and direction of the motor should be adjusted as needed, and the circuit should be checked for any issues.
This tutorial is useful any time when a DC motor is required in projects with an Arduino or a microcontroller. Thus not only Arduino but any microcontroller such as PIC or AVR microcontrollers can be interfaced using the technique shown here. Any general BJT transistor such as BC547, 2N2222A, 2N3904 and standard diodes such as 1N4148, 1N4001 or 1N4007 can be used.
The reason for using transistor and diode is that, using transistor we can supply more current to the motor, and more importantly the transistor together with diode will prevent the damage of the microcontroller pin. A DC motor is an inductor coil which can store energy after the power supply is cut off for the motor. The stored energy can go into the microcontroller pin which can damage it.
DC motor control using Arduino and BJT
The following shows the circuit schematic for connecting Arduino with a DC motor with transistor and diode.
In the above circuit diagram, standard transistor BC547 is used. The Arduino PWM pin PD6 is connected to the transistor base via 1KOhm resistor. The DC motor is connected at the transistor collector. The diode used here is standard 1N4001 diode and is connected across the DC motor as shown in the circuit diagram. The 9V battery is used here. This battery is connected as shown with positive terminal at one end of the motor and the negative terminal to the emitter side of the transistor. The battery power voltage used depends upon the motor power requirement. The transistor is used here as a switch.
Simple program to run DC motor
Below is a simple Arduino program code to run the DC motor. In this code we simply send high and low signal to the Arduino pin PD6. This signal reaches the base of the transistor, gets amplified and reaches the collector which then turns on the motor. Actually the transistor opens and closes the conducting path for the current flow from the battery positive to negative terminal(conventional flow).
// pin alias name for the motor
int motorPin = 6;
void setup() {
// Define motor pin as output
pinMode(motorPin, OUTPUT);
}
void loop(){
// Turn motor on
digitalWrite(motorPin, HIGH);
// Wait 500 ms
delay(500);
// Turn motor off
digitalWrite(motorPin, LOW);
// Wait another 500 ms
delay(500);
}
In the above code we have setup the PD6 as the motor pin. Then in the main loop we consecutively send HIGH and LOW signal using the digitalWrite() function with 500ms delay between them.
DC motor speed control using PWM from Arduino
In the above example we have sent digital high and low pulses to run the DC motor. This is not effective way to run a DC motor. With the use of PWM(Pulse Width Modulation), we will have more control over the speed of DC motors. The following program code illustrates how PWM can be used to control the DC motor speed.
// pin alias name for the motor
int motorPin = 6;
void setup() {
// PWM pins don't require the pinMode() function
}
void loop(){
// Turn motor on to maximum
analogWrite(motorPin, 255);
// Wait 500 ms
delay(500);
// Turn motor to 1/2 power
analogWrite(motorPin, 127);
// Wait 500 ms
delay(500);
// Turn motor off
analogWrite(motorPin, 0);
// Wait 500 ms
delay(500);
}
In the above code, the pin 6 or the PD6 pin is set as motorPin which is just alias name. Then in the main loop we have used the analogWrite() function to send PWM signal to the motor pin. The analogWrite() function takes two parameters- one is the Arduino pin and the PWM level. Since Arduino is 8 bit microcontroller(ATmega328p), the PWM level ranges from 0 to 255. The PWM level of 255 means 100% duty cycle, 127 means 50% duty cycle and 0 means 0% duty cycle.
In summary, controlling a DC motor with an Arduino, a transistor, and a
diode involves creating a circuit diagram, connecting the components,
writing the code, uploading the code, and testing the motor. By
following these steps, you can easily control the speed and direction of
a DC motor using an Arduino microcontroller. Here it was shown a simple way to control a DC motor using Arduino with transistor and diode. The tutorial Speed and direction control of DC motor using Arduino Fast PWM shows how we can control DC motor speed and direction using L298N motor driver.
how can i make a tricopter drone using bluetooth arduino transistors resistors 9v battery motors and propellers but without motor bridge please help