Me 223 Lab 9 H-Bridge Motor Control L293D S16

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB 9 H bridge basics and motor

control
Purpose
Introduce students to feedback controlling, function modifiers, basic Arduino coding and Hbridges.
Equipment:
Arduino
L293D H-bridge (provided, return at the end of lab)
Small DC motor
Piece of adhesive tape for the motor shaft
Wires
Ultrasonic distance sensor (provided, return at the end of lab)
Introduction:
Part of the 223 course is exposing you to different ways to make things move and how to get
readings into a system, e.g. your Arduino. In this lab we combine both by taking a distance
reading from the ultrasonic distance sensor and then applying that output through an H-bridge to
make a DC motor turn in two different directions depending on the sensor reading.
What is an H-bridge and how do they function?
An H-bridge is an electrical circuit that can direct voltage and current to flow either direction. It
can be either an integrated circuit chip or build from individual components. In this lab, you will
used the L293D which is a dual H-bridge chip, meaning that each side is its own H-bridge.
Some helpful links are provided below.
Similar circuit example using H-bridge
https://learn.adafruit.com/adafruit-arduino-lesson-15-dc-motor-reversing?
view=all
DIY H-bridge article and video example
http://hackaday.com/2014/04/25/a-h-bridge-motor-controller-tutorial-makes-it-simple-tounderstand/

Advantages of an H-bridge
Can easily use external power to run motors, (if you do this, you are not limited to the 5v
and 500 mAmp maximum output of the Arduino Uno)
Bi-directional DC motor control
One dual H-bridge-chip can run 2 DC motors at once
Can be used with steppers to run and hold position with an external load on the motor
Simple to code for simple applications
Arduino has libraries for different applications

Disadvantages of an H-bridge
For single direction control, H-bridges are more expensive than mosfets in lower power
applications
For a simple motor the setup might be more complicated than a mosfet
With continuous power, H-bridge chip can get hot (as with most electronics) and may
need a heatsink or active cooling to operate.
Examples of how circuits might be arranged
Dual H-bridge: (2 bi-directional motors; Texas instruments L293d)

Dual H-bridge used to turn 3 motors (2 single-direction, 1 bi-direction):


Ti L293d block diagram (from Texas instruments L293 PDF data sheet,
http://www.ti.com/lit/ds/symlink/l293.pdf)

In-Lab

Procedure
1. Build circuit from diagram provided, without distance sensor.

2. Read the following code and try to understand how it works. Ask any questions to a
mentor.
Code section for motor driver
const int en = 2;
//enabler pin
const int input1 = 10;
//input 1 to motor driver
const int input2 = 11;
//input 2 to motor driver
const int trig = 3;
//sound trigger to range finder
const int echo = 4;
//echo input from range finder
unsigned long d;
//distance variable
double s = sqrt(1.140*(16+273.15)*286.9); //Speed of sound
//sqrt(kRT)

void setup() {
pinMode(en,OUTPUT);
//Sets enabler pin as an output from arduino
pinMode(input1,OUTPUT);
//Sets input1 pin as an output from arduino
pinMode(input2,OUTPUT);
//Sets input2 pin as an output from arduino
pinMode(trig,OUTPUT);
//Sets trigger pin as an output from arduino
pinMode(echo,INPUT);
//Sets echo pin as in input to arduino
Serial.begin(9600);
//Starts Serial monitor with a 9600 baud rate
}
void loop() {
/*Code runs Motor in one direction increasing in speed to maximum, then decreasing back to
zero*/
/*For PWM starting at zero will increase by an increment of 1 until PWM is at 1023*/
for (int PWM = 0; PWM<255; PWM++){ //Increasing loop
digitalWrite(en,LOW);
//Turns off motor driver to change settings
analogWrite(input1,PWM); //analog writes to the input 1 on motor driver
analogWrite(input2,0);
//analog writes nothing to the motor driver on input 2 pin
digitalWrite(en,HIGH);
//Turns on motor driver to run changed settings
delay(5);
//process repeats every 5 milli seconds
}
for (int PWM = 255; PWM>=0; PWM--) { //Decreasing loop
digitalWrite(en,LOW);
//Turns off motor driver to change settings
analogWrite(input1,PWM); //analog writes PWM value to input 1
analogWrite(input2,0);
//analog writes 0 to input 2
digitalWrite(en,HIGH);
//Turns on motor driver to run changed settings
delay(5);
}
}
Code for range finder
const int en = 2;
//enabler pin
const int input1 = 10;
//input 1 to motor driver
const int input2 = 11;
//input 2 to motor driver
const int trig = 3;
//sound trigger to range finder
const int echo = 4;
//echo input from range finder
unsigned long d;
//distance variable
double s = sqrt(1.140*(16+273.15)*286.9); //Speed of sound
//sqrt(kRT)

void setup() {
pinMode(en,OUTPUT);
//Sets enabler pin as an output from arduino
pinMode(input1,OUTPUT);
//Sets input1 pin as an output from arduino
pinMode(input2,OUTPUT);
//Sets input2 pin as an output from arduino
pinMode(trig,OUTPUT);
//Sets trigger pin as an output from arduino
pinMode(echo,INPUT);
//Sets echo pin as in input to arduino
Serial.begin(9600);
//Starts Serail monitor with a 9600 baud rate
}
void loop() {
digitalWrite(trig,LOW); //ensures trig is off before sending signal
digitalWrite(trig,HIGH); //Writes Trig high
delayMicroseconds(2);
/*Delay 2 microseconds as is the required input to the HC-SR04*/
digitalWrite(trig,LOW); //Writs Trig pin low
d = pulseIn(echo,HIGH); /*times the time it take between low singal emmitted from the HCSR04*/
float dist = d*s*.0001/2; //Takes tme output and converts to distance
Serial.println(dist);
//Serial prints the distance in cm
}
3. Load code for motor driver into Arduino and run. After your motor is running, get
checked off by mentor -(See above for code).
4. Use your knowledge of arduino to modify the existing code to make the motor rotate in
both directions and turn off. After you have completed this, get checked off by mentor.

5.
6.
7.
8.

NOTE : Remember to take notes during circuit setup and modification for deliverables
section at the end of lab.
Add distance sensor to circuit -(See the figure under step 1).
Add the code section for distance sensor -(See above for code).
Check serial monitor for distance output-(make sure the distance sensor is working).
Write a code function that takes in distance and outputs variable motor speed and
direction- (suggested: map function) (get checked by mentors).

Deliverables
1. How might the H-bridge be useful in your final project?(3 applications )
2. In the void loop what command would you use to make the motor spin for 2 seconds?
3. Why are we using analogWrite for motor control as opposed to digitalWrite?
4. What are some pros and cons of using an H bridge as opposed to a relay?
5. Notes from lab.
Extra Credit
What will happen if both motor pins are on at the same time?
For procedure step 8 use a function other than map.

You might also like