OK, well spending the $23 on
OK, well spending the $23 on the Pololu controller will definitely give you the most flexibility. It’s tiny, you can control two motors, forward or backward, in any of 127 speeds in either direction. And it only needs two I/O pins (one for the serial communication, one for the reset line). You could even just tie the reset line to +5V with a pull-up resistor if you wanted to get down to one pin, but they recommend using an I/O line on it if possible. One or two I/O pins for two motors in both directions is great. To change motor speeds, you just send 4-byte commands down the serial line to tell it things like “set motor 1 to speed 64 in reverse” and “set motor 2 to speed 127 forward”. It takes care of continuing to power the motors in the directions and speeds you’ve told it to while you can continue doing other things.
The main downside of the controller is just the expense. $23 isn’t all that expensive when it comes to robotics, but it’s almost the cost of a microcontroller board.
The next step down would be to use a motor driver IC like the L293D, or the pin-compatible SN754410 that I used on LDM (see the entry for 2008/04/13). This is basically just two H-bridges in an IC. This lets you use 3 I/O pins to drive one motor in both directions, or 6 I/O pins for two motors. This IC doesn’t give you any variable speed control, only on/off control in either direction, but you can vary the speed yourself with the Arduino if you use some of the PWM-capable pins on the Arduino to control it. However, if you do that, you can’t do anything else while you’re moving the motor. So it’s really best suited to situations where you don’t care about doing less than full speed in each direction. This chip also has a maximum current of 1A per motor. You control the motor by setting the I/O pins low or high – if you set one of the pins low and the other pin high, the motor turns in one direction. If you set the first pin high and the other pin low, then the motor turns in the other direction. If you’ve connected the Enable pin for the H-bridge to an I/O pin, you can use PWM on that pin to control the speed of the motor in the direction established by the other two pins.
The final option offering two-directional control, what I did on Harry The Discbot, is to build the H-bridges yourself out of 4 NPN transistors. This takes a lot of space on your breadboard (you need 4 transistors per motor), and you need to use 4 I/O pins per motor, and there’s no good way to control speed unless you make the circuit more complicated. But the upside is that you don’t need to wait for ICs to arrive in the mail – standard NPN transistors like the 2N3904 are one of the few electronics components you can still just walk into a Radio Shack and buy.
And finally, if you just want to be able to turn a motor on or off in one direction, you can do that with a single NPN transistor and a single I/O pin.
I’ll describe that last option, because it sounds like what you want to do is just get a motor turning. As you may know, a transistor can be used as an electronically controlled switch, to simply open or close a circuit. You can use that property to let your Arduino use its relatively small current to send a larger amount of current through the motor. A transistor has three pins, a Collector, Base, and Emitter. They’re not marked on the transistor itself, but the packaging usually has a drawing that shows you which pin is which. Note that often (including on the packaging for transistors at Radio Shack), the pins are actually shown looking at the transistor from the BOTTOM, with the pins pointing at you. That has caused misunderstanding for me several times The names of the pins do pretty much what they sound like – the Collector is where the (larger) current comes in, the Emitter is where that larger current exits when the transistor lets it. The Base pin is the one that controls the current through the other two.
I learned that it works best to put the transistor AFTER the motor, on the Ground side of the circuit. So you hook up +5V from the Arduino (or from a separate battery pack) to the positive terminal on your motor. You connect the negative terminal of the motor to the Collector pin on the transistor. You connect the Emitter pin of the transistor to Ground on the Arduino (or on the battery pack). You connect one of the Arduino’s I/O pins to the Base pin on the transistor. Note that, if you used a separate battery pack to power the motor, it’s important to connect the battery’s Ground to the Arduino’s Ground, or else it won’t work.
So, once you have all that connected, it’s simply a matter of setting that I/O pin to HIGH in order to tell the transistor to let the current flow through its Collector-Emitter pins, and thus turn on the motor. To turn the motor off, set the I/O pin to LOW. That’s all there is to it.
Once you’re comfortable with that, you should be able to extrapolate how to build the H-bridge out of 4 transistors. In an H-bridge, the transistors also function just like four switches, so the idea is the same. The Wikipedia article on H-bridges helped a lot when I was building my first one.
Hope that gets you started. Let me know if you have any other questions.
Dan