How to use a H_BRIDGE to control a DC Motor

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to use a H_BRIDGE to control a DC Motor

Post by Shane » Thu Jun 23, 2011 4:51 pm

In this tutorial, we use the L293D H-Bridge to operate a 12VDC motor with the Arduino. This H-Bridge allows us to do several things. First it allows us to interface the motor, which requires much more current and voltage than the output pins can supply, with the Arduino. With minimal connections, it allows us to both control the direction of motor rotation and with the use of the Arduino PWM output it enables us to add a speed control to the motor. In this circuit, we use a SPST switch connected to the L293D to control the speed and a potentiometer connected to Arduino pin A0 to control the PWM output (using the analogWrite function) on pin D9. If you look at the pin layout of the L293D you'll notice that we utilize only one of the 2 H-Bridges on the chip, so two motors could be controlled using only one L293D.
1.jpg
1.jpg (138.7 KiB) Viewed 3591 times

Code: Select all

// Arduino code for the H_BRIDGE TUTORIAL
 int switchPin = 7; // switch input
 int motor1Pin = 3; // H-bridge leg 1 
 int motor2Pin = 4; // H-bridge leg 2 
 int speedPin = 9; // H-bridge enable pin 
 int ledPin = 13; //LED 
 int potPin = 0;
 int speed = 0;

void setup() {
 // set the switch as an input:
 Serial.begin (9600);
 pinMode(switchPin, INPUT); 

 // set all the other pins you're using as outputs:
 pinMode(motor1Pin, OUTPUT); 
 pinMode(motor2Pin, OUTPUT); 
 pinMode(speedPin, OUTPUT);
 pinMode(ledPin, OUTPUT);

 // set speedPin high so that motor can turn on:
 digitalWrite(speedPin, HIGH); 

 // blink the LED 3 times. This should happen only once.
 // if you see the LED blink three times, it means that the module
 // reset itself,. probably because the motor caused a bownout
 // or a short.
 blink(ledPin, 3, 100);
 }

void loop() {
 // if the switch is high, motor will turn on one direction:
 if (digitalRead(switchPin) >0) {
 digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
 digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
 speed = analogRead(potPin);
 speed = 793 + (speed/6);
 analogWrite (speedPin, speed);
 Serial.println(speed);
 delay (50);

 } 
 // if the switch is low, motor will turn in the other direction:
 else {
 digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
 digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
 speed = analogRead(potPin);
 speed = 793 + (speed/6);
 analogWrite (speedPin, speed);
 Serial.println(speed);
 delay (50);
 }
 }

/*
 blinks an LED - SIGNIFIES RESET AND ARDUINO SHUTDOWN
 */
 void blink(int whatPin, int howManyTimes, int milliSecs) {
 int i = 0;
 for ( i = 0; i < howManyTimes; i++) {
 digitalWrite(whatPin, HIGH);
 delay(milliSecs/2);
 digitalWrite(whatPin, LOW);
 delay(milliSecs/2);
 }
 }
2.jpg
2.jpg (194.53 KiB) Viewed 3591 times
3.jpg
3.jpg (203.44 KiB) Viewed 3591 times
4.jpg
4.jpg (183.24 KiB) Viewed 3591 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”