0% found this document useful (0 votes)
58 views3 pages

H - Bridge Tutorial::: Code

This tutorial uses an L293D H-Bridge to control the direction and speed of a 12VDC motor with an Arduino. The H-Bridge allows bidirectional control of the motor using minimal connections. It interfaces the higher power motor with the Arduino's output pins. A switch controls the direction and a potentiometer connected to the Arduino controls the PWM output and motor speed. The code demonstrates using the H-Bridge to change the motor's direction based on the switch and vary its speed using the potentiometer and PWM output.

Uploaded by

rclab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views3 pages

H - Bridge Tutorial::: Code

This tutorial uses an L293D H-Bridge to control the direction and speed of a 12VDC motor with an Arduino. The H-Bridge allows bidirectional control of the motor using minimal connections. It interfaces the higher power motor with the Arduino's output pins. A switch controls the direction and a potentiometer connected to the Arduino controls the PWM output and motor speed. The code demonstrates using the H-Bridge to change the motor's direction based on the switch and vary its speed using the potentiometer and PWM output.

Uploaded by

rclab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

H_BRIDGE TUTORIAL::

In this tutorial, we use the L293D H-Bridge to operate a 12VDC motor with the Arduino. This HBridge 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.
Click HERE for video clip.
CODE:
// 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);
}
}

You might also like