FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Controlling the Direction of a Brushed Motor with an H-Bridge

            An H-Bridge can control two brushed motors. Figure 8-10 shows the connections for the L293D H-Bridge IC, you can also use the SN754410 which has the same pin layout.In the sketch in this recipe’s Solution, a single motor is controlled using the IN1 and IN2 pins; the EN pin is permanently HIGH because it is connected to +5V.

The following sketch controls both motors together:
/*
* Brushed_H_Bridge_simple2 sketch
* commands from serial port control motor direction
* + or - set the direction, any other key stops the motors
*/
const int in1Pin = 5; // H-Bridge input pins
const int in2Pin = 4;
const int in3Pin = 3; // H-Bridge pins for second motor
const int in4Pin = 2;
void setup()
{
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
Serial.println("+ - sets direction of motors, any other key stops motors");
}
void loop()
{
if ( Serial.available()) {
char ch = Serial.read();
if (ch == '+')
{
Serial.println("CW");
// first motor
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,HIGH);
//second motor
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,HIGH);
}
else if (ch == '-')
{
Serial.println("CCW");
digitalWrite(in1Pin,HIGH);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);
digitalWrite(in4Pin,LOW);
}
else
{
Serial.print("Stop motors");
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
}
}
}

No comments:

Post a Comment