|
Research & Learning Class pages
Shop Admin
ITP Help Pages |
DC Motor ControlDC Motor Control Using an H-BridgeIn this tutorial, you'll learn how to control a DC motor's direction using an H-bridge. To reverse a DC motor, you need to be able to reverse the direction of the current in the motor. The easiest way to do this is using an H-bridge circuit. There are many different models and brands of H-Bridge. This tutorial uses one of the most basic, an L293E from Texas Instruments (the alternate name for this chip is the SN754410). If you simply want to turn a motor on and off, and don't need to reverse it, for example if you're controlling a fan, try the tutorial on controlling high current loads with transistors. For this lab you'll need:
Prepare the breadboardConect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections: ![]() If you're using an Arduino breadboard shield, there is a row of sockets connected to 5V on the analog in side of the breadboard, and a row connected to ground on the digital in side of the board: ![]() Add a Digital Input (a switch)Connect a switch to digital input 2 on the Arduino.
Get a motorGet a DC motor that runs on low voltage DC, in the 5-15V range. Connect leads to its terminals, and run if from a benchtop power supply. Try changing the voltage on it, and seeing what effect it has. Don't go over the motor's rated voltage. IF you do, the motor will ruin very hot, and probably stop within a minute and burn out. Get an H-bridgeThis example uses an H-bridge integrated circuit, the Texas Instruments L293 (also sold as the SN754410). Acroname and Digikey carry these chips, as do many other manufacturers. You can find its datasheet [[ This example uses an H-bridge integrated circuit, the Texas Instruments L293 (also sold as the SN754410). Acroname and Digikey carry these chips, as do many other manufacturers. This particular chip has 4 half-H bridges, and can therefore control 2 motors. It can drive up to 1 amp of current, and between 4.5 and 36V. If your motor needs more than that, use a different H-bridge. |here]]. This particular chip has 2 H-bridges, and can therefore control 2 motors. It can drive up to 1 amp of current, and between 4.5 and 36V. If your motor needs more than that, use a different H-bridge. The L293 is a very basic H-bridge. There are many different models of H-bridge from other manufacturers. This one actually has two bridges, one on the left side of the chip and one on the right. Though they may have some extra features, every H-bridge will have the same basic interface:
The logic voltage pins connect to the same voltage source as your microcontroller. The motor supply voltage connect to the voltage source for the motor. Since the motor may need higher voltage an almost certainly needs higher current, this is often a separate supply. If you're using a motor that draw less than about 750 milliamps at 15 volts or less, you could use the 9-15V power jack on the Arduino module, and switch the module to draw power from this source as well. The logic inputs on the H-bridge connect to the pins on your microcontroller that you use to output control signals to the H-bridge and the supply output pins go to your motor. The configuration of these pins might vary slightly on other models of H-bridge. They might also use slightly different names, but the functions are roughly the same. Connect the motor to the H-bridgeConnect the motor to the H-bridge as follows:
Most motors consume more current than a microprocessor, and need their own supply. The motor and the microcontroller need a common ground, however. The example above uses 12V, run in parallel with the 5V voltage regulator that supplies the Arduino board. Whatever motor you use, make sure the power source is compatible (i.e. don't use a 9V battery for a 3V motor!). Note the capacitor connecting the motor supply to ground. It smooths out the voltage spikes and dips that occur as the motor turns on and off. program the MicrocontrollerProgram the microcontroller to run the motor through the H-bridge:
int switchPin = 2; // 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
void setup() {
// set the switch as an input:
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 brownout
// or a short.
blink(ledPin, 3, 100);
}
void loop() {
// if the switch is high, motor will turn on one direction:
if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// 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
}
}
/*
blinks an LED
*/
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);
}
}
Once you've seen this code working, try modifying the speed of the motor using the analogWrite() function, as explained in the analog lab. Use analogWrite() on pin 9, the enable pin of the motor, and see what happens as you change the value of the analogWrite(). Get creativeUse your motor to make something move, vibrate, rise, fall, roll, creep, or whatever you can think of, in response to user input from a digital input device (switch, floor sensor, tripwire, etc). Look inside moving toys, you'll find a number of excellent motors and gears you can re-purpose. See the innards of a cymbal monkey below as an example. Perhaps you can re-design the user interface to a toy, using the microcontroller to meciate between new sensors on the toy and the motors of the toy. Whatever you build, make sure it reacts in some way to human action. |
||||||||