Motor drivers are needed because the pins of a micro-controller cannot source the required current to run a motor.
The arduino can provide a maximum 200mA per pin and can sink a total of 400mA current.
You will damage the micro - controller or pins if you try to draw more current. A motor cannot run on such small current. Thus an amplifier circuit or a relay is needed. To drive the motor in both directions , an H-bridge is needed.
Lo and behold ! Let me present to you L293D. It is a transistor implementation of both an amplifier and an H-bridge , in other words , the solution to our problem.
This is what an h-bridge looks like:
This is the pin layout of L293D:
It can run two motors.
Vss needs to be connected to 5v for either h-bridge to run.
Enable1, if connected to 5v enables the left h- bridge.
Enable2, is for right H- bridge.
Gnd pins go to ground.
Vs is the voltage you want to give the motor.
WIRE IT UP
Created Using Fritzing
THE PROGRAM
#define M1pin1 9
#define M1pin2 10
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
Clockwise(); // motor turns in clockwise direction
delay(2000); // Arduino sleeps for 2 seconds
AntiClockwise(); // motor turns in anticlockwise direction
delay(2000);
}
void Clockwise()
{
digitalWrite(M1pin1, HIGH); // digitalWrite is a predefined function to control pin state.
digitalWrite(M1pin2, LOW);
}
void AntiClockwise()
{
digitalWrite(M1pin2, HIGH);
digitalWrite(M1pin1, LOW);
}
If You Are looking for speed control :
// You need to connect input wires to a pmw enabled pins on Arduino. These are identified by ~ sign on the arduino board. Notice 9 has a ~ before it on the board.
#define M1pin1 9
#define M1pin2 10
int pmw=128;
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
Clockwise(); // motor turns in clockwise direction
delay(2000); // Arduino sleeps for 2 seconds
AntiClockwise(); // motor turns in anticlockwise direction
delay(2000);
}
void Clockwise()
{
analogWrite(M1pin1, pmw); // analogWrite is a predefined function to control pin state.
digitalWrite(M1pin2, LOW);
}
void AntiClockwise()
{
analogWrite(M1pin2, pmw);
digitalWrite(M1pin1, LOW);
}
//pmw can take values between 0 and 256. At 256, it will run at max speed. At 128, it will run at half the speed.
No comments:
Post a Comment