In this lesson, we are going to learn how to control both the direction and speed of a small DC motor using an Arduino and the L293D motor driver chip, so first of all we need to understand what is a DC motor and what is a L293D chip.
Arduino IDE (version 1.6.4+)
A DC Motor is a type of electric motor that converts DC electrical power to mechanical power i.e. a DC supply is converted to rotation or movement. DC motors are one of the commonly used motors in different applications like electronic toys, power tools, portable fans, etc.
DC Motors are further classified in to different types like series, shunt and compound and each type is used in different areas of applications. Some DC motors are also used in Robotic and Industrial applications for their easy control and precision.
Since DC motors are generally associated with small to medium applications, where the system mainly consists of a Microcontroller as the main processing unit, controlling and driving a DC motor is very important. This is because, driving a motor directly using the microcontroller is not advised (sometimes not possible) as the current from the Microcontroller is very small (usually less than 30mA). Before you connect the circuit, check this link for how to power the dc motor correctly, thanks for the www.sharetechnote.com!
The DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.
Microcontrollers and single board computers cant directly drive motors or inductive loads. A transistor or a MOSFET can be used but what if you want to be able to reverse a motor? That is where the H-bridge comes in!
An H-bridge is an integrated circuit that allows voltage to each output to be applied both ways. Each output can be configured either a positive or a ground in the circuit meaning a motor can be driven in either direction with relative ease.
As the name suggests, L293D is a quadruple H-bridge, high current motor driver IC. It can be used to drive two motors at a time in both the directions with an output current of 600mA for each motor. L293D IC is designed to drive relays, DC motors, stepper motors and other inductive loads with high current and high voltage requirements.
1,2EN: To activate the channel 1 and 2 we supply +5v to this pin.
3,4EN: To activate the channel 3 and 4 we supply +5v to this pin.
Vcc1: Input voltage to derive the internal circuit (darligton array) = 4.5 to 36 v
Vcc2: Supply/Output to appear at output = 4.5 to 36 v
1A: Channel-1 Input Pin
2A: Channel-2 Input Pin
3A: Channel-3 Input Pin
4A: Channel-4 Input Pin
1Y: Channel-1 Output Pin
2Y: Channel-2 Output Pin
3Y: Channel-3 Output Pin
4Y: Channel-4 Output Pin
More info about the L293D chip, please check this link. thanks for the www.microcontroller-project.com!
Build the circuit as below:
Overhere, you could replace the battery box with the power module
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.
int motorpin1 = 3; //define digital output pin no. int motorpin2 = 4; //define digital output pin no. void setup () { pinMode(motorpin1,OUTPUT); //set pin 3 as output pinMode(motorpin2,OUTPUT); // set pin 4 as output } void loop () { digitalWrite(motorpin1,LOW); digitalWrite(motorpin2,HIGH); }
A few seconds after the upload finishes, you can see that the DC motor turns in one direction. To drive the motor in opposite direction you just need to put HIGH instead of LOW and vice versa.
In this example, we will show how to control the direction and speed of a small-sized direct current (DC) motor) by using the driver chip L293D and the Arduino board.
Build the circuit as last example.
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.
const int motorIn1 = 3; // the one pin of the motor attach to pin 3 const int motorIn2 = 4; // the another pin of the motor attach to pin 4 void setup() { pinMode(motorIn1,OUTPUT); //initialize the motorIn1 pin as output pinMode(motorIn2,OUTPUT); //initialize the motorIn2 pin as output } void loop() { clockwise(200); //rotate clockwise delay(1000); //wait for a second counterclockwise(200); //rotate counterclockwise delay(1000); //wait for a second } //The function to drive motor rotate clockwise void clockwise(int Speed) { analogWrite(motorIn1,Speed); //set the speed of motor analogWrite(motorIn2,0); //stop the motorIn2 pin of motor } //The function to drive motor rotate counterclockwise void counterclockwise(int Speed) { analogWrite(motorIn1,0); //stop the motorIn1 pin of motor analogWrite(motorIn2,Speed); //set the speed of motor }
A few seconds after the upload finishes, the DC motor will start to rotate about, and the speed will change accordingly.
In this example, we use a potentiometer to control the speed of the motor and a push button to control the direction. Moving forwards, this hardware and code can be adapted to make a small driving robot.
Build the circuit as below:
This H-Bridge is capable of driving two separate DC motors so we are only going to use half of the chip. Pins 1 – 7 can be left disconnected. Pin 8 Is our Motor Power Input so we are going to connect it directly to the power supply module.
Both pins 9 and 16 need to be connected to 5VDC from the Arduino. Pin 9 is our “Enable” pin for output 3 and 4 – This enables the side of the motor controller we are using; for this tutorial just tie it directly to the 5VDC power from the Arduino so that it is permanently switched on. Pin 16 is our H-Bridge’s internal power.
Before we give the Arduino power or plug in any motor power it is always a good idea to go over all of the connections to make sure there are no wires in the wrong spot – sometimes that can make for a very expensive mistake! For now, we will leave the batteries out while we program the Arduino.
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up the following sketch onto your Arduino.
int switchPinFwd = 7; //Input from the switch when in the Forward position int potentiometerIn; //variable to hold the potentiometer input int fwdPin = 3; //Logic level output to the H-Bridge (Forward) int revPin = 4; //Another logic level output to the H-Bridge (Reverse) void setup() { // put your setup code here, to run once: pinMode(switchPinFwd, INPUT_PULLUP); pinMode(fwdPin, OUTPUT); //Set the forward pin to an output pinMode(revPin, OUTPUT); //Set the forward pin to an output } void loop() { // put your main code here, to run repeatedly: potentiometerIn = analogRead(A0); int output = potentiometerIn / 4; //divide the potentiometer input by 4 so it can be used in the AnalogWrite function if(digitalRead(switchPinFwd) == HIGH) //Check to see if the pin is high or low { //If the pin is HIGH, it must be set to forward analogWrite(fwdPin, output); //Output our potentiometer value on the forward pin. } else { //Otherwise the switch must be set to Reverse analogWrite(revPin, output); //Output our potentiometer value on the forward pin. } delay(25); }
A few seconds after the upload finishes, plug the motor power supply in so the motor has power and after a few seconds try adjusting the potentiometer to adjust the motor speed. When changing directions, ensure the motor is stopped as it is not a good idea to reverse the polarity of a motor while running. Going forward, code could be added to this to prevent the user from changing direction unless the throttle is at 0.
DownLoad Url osoyoo.com