Introduction
In this little tutorial, we will see How to connect DC Motor (Direct Current Motor) to an Arduino card, you will also find a programming code for your first tests. and Pinout of H-bridge L293D and DC Motor with pin discription
Direct current motors are devices that transform electrical energy into mechanical energy in the form of angular movement of its axis. As their name indicates, they are powered with direct current, the motor that we will use in this example can be powered from 3V to 5V. In this way, for the motor shaft to start rotating, it is only necessary to apply power (potential difference between its terminals), if the power supply terminals are reversed, the motor rotates in the opposite direction.
How to connect DC Motor to Arduino basically the Arduino is a popular microcontroller platform used to control various electronic devices. One such device is the DC motor. To control a DC motor using Arduino, an H-bridge circuit is required. An H-bridge is an electronic circuit that can be used to control the direction and speed of a DC motor.
Hardware Required
Components | # | Buy From Amazon |
---|---|---|
Arduino UNO | 1 | Buy Now |
DC motor 3V- 5V | 1 | Buy Now |
H-bridge L293D | 1 | Buy Now |
Potentiometer 10KΩ | 1 | Buy Now |
2.1mm Jack Screw Terminal | 1 | Buy Now |
9v DC Adapter (Optional) | 1 | Buy Now |
Jumper Wires | Few | Buy Now |
Breadboard | 1 | Buy Now |
DC Motor Pinout
H-bridge L293D
To control a direct current motor, it is very common to use an H bridge, that is, a circuit that receives 3 digital control signals and, according to the values of said signals, delivers the necessary energy to the motor, through its output pins, so that the motor turns in one direction, in the other or does not turn at all. The 3 digital control signals of an H bridge are:
- Input1: Rotation control signal 1
- Input2: Rotation control signal 2
- Enable: Signal to enable the H bridge
In order for the motor to rotate, the digital control signals INPUT1 and INPUT2 must have opposite values and the ENABLE signal must be at a High logic value. The H bridge that we select for this example is the integrated circuit L293D, it has 2 H bridges and we will use only one, specifically, we will use the control signals INPUT1, INPUT2, and ENANLE1 and we will connect the motor to OUTPUT1 and OUTPUT2.
H-bridge L293D Pinout
L293D Pin Configuration
Pin Name | Description |
Enable 1,2 | This pin enables the input pin Input 1(2) and Input 2(7) |
Input 1 | Directly controls the Output 1 pin. Controlled by digital circuits |
Output 1 | Connected to one end of Motor 1 |
Ground | Ground pins are connected to ground of circuit (0V) |
Ground | Ground pins are connected to ground of circuit (0V) |
Output 2 | Connected to another end of Motor 1 |
Input 2 | Directly controls the Output 2 pin. Controlled by digital circuits |
Vcc2 (Vs) | Connected to Voltage pin for running motors (4.5V to 36V) |
Enable 3,4 | This pin enables the input pin Input 3(10) and Input 4(15) |
Input 3 | Directly controls the Output 3 pin. Controlled by digital circuits |
Output 3 | Connected to one end of Motor 2 |
Ground | Ground pins are connected to ground of circuit (0V) |
Ground | Ground pins are connected to ground of circuit (0V) |
Output 4 | Connected to another end of Motor 2 |
Input 4 | Directly controls the Output 4 pin. Controlled by digital circuits |
Vcc2 (Vss) | Connected to +5V to enable IC function |
Circuit Diagram
Working Explanations
To connect a DC motor to Arduino using an H-bridge, the H-bridge circuit is connected to the motor and the Arduino. The H-bridge circuit controls the voltage polarity and magnitude of the motor, which determines the motor direction and speed. The Arduino sends signals to the H-bridge circuit to control the motor.
Installing Arduino IDE Software
First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow
Code
We load the following code and observe how the speed of the motor shaft changes when turning the potentiometer knob and how the direction of rotation changes when we cross the midpoint of the potentiometer range approximately.
//For more Projects: www.arduinocircuit.com
//How to connect DC Motor to Arduino
const int pinInput1 = 8; //Constant that contains the pin number to which we connect the signal IN1 of the H link L293D
const int pinInput2 = 7; //Constant that contains the pin number to which we connect the signal IN2 of the point H L293D
const int pinEnable = 6; //Constant that contains the pin number to which we connect the signal EN1 of the point H L293D
int valPot = 0;//Variable that will contain the value read in the Analog pin (0 to 1023) to which the potentiometer signal was connected
int valPWM = 0;//Variable that will contain the PWM value (-255 to 255) that will be written to the PWM pin to which we connect the signal EN1 of the point H L293D
void setup() {
//Set the digital pins
pinMode(pinInput1, OUTPUT);
pinMode(pinInput2, OUTPUT);
}
void loop() {
valPot = analogRead(0); // We read the potentiometer
//We convert the read value to be able to use it as the second argument of "analogRead()"
//The sign will be used to define the direction of rotation of the motor
valPWM = map(valPot, 0, 1023, -255, 255);
digitalWrite(pinInput1, valPWM>0); //We establish the direction of the rotation according to the sign of valPWM
digitalWrite(pinInput2, !valPWM>0); //We establish the direction of the rotation according to the sign of valPWM
analogWrite(pinEnable, abs(valPWM));//We set the rotation speed according to the absolute value of valPWM
delay(10);
}
Applications
- Robotics: DC motors are widely used in robotics projects, and controlling them using Arduino can provide precise control.
- Automation: DC motors can be used in automated machines and can be controlled using Arduino to achieve various tasks.
- Automotive: DC motors are used in automotive applications such as electric cars, and Arduino can be used to control them.
- Home Automation: DC motors can be used in home automation projects, such as controlling blinds or curtains, and can be controlled using Arduino.
- Energy Generation: DC motors can be used in energy generation projects, such as wind turbines, and can be controlled using Arduino.
Conclusion
Controlling a DC motor using Arduino and an H-bridge circuit provides an easy and efficient way to control the direction and speed of a motor. It can be used in various applications such as robotics, automation, automotive, home automation, and energy generation projects.