Let's Make Robots!

Driving a motor with arduino...

I need some help with driving dc motors with arduino. I have already looked at the arduino site and all I found was this http://www.arduino.cc/en/Main/ArduinoShields and no guide or parts list.

I have gone thru all of the lessons here http://www.ladyada.net/learn/arduino/  and ithas been really fun so far but I WANT TO BUILD ROBOTS! NOT BLINKIE LIGHTS! :)



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I'm driving several different motors in several different ways on my Arduino-powered Little Drum Machine. What is it you want to know, exactly? The Arduino doesn't provide enough current through its I/O pins to drive a motor directly, so you'll need some amount of additional circuitry either way, but what you need depends on exactly what you're trying to do -- what size motor you want to control, how much control you want to have over it (simply on/off in one direction, or on/off in both directions, or variable speed control in both directions, etc). But I'd be happy to share with you what I've learned -- I'm no seasoned expert, I just got my Arduino a few weeks ago, but this Little Drum Machine project has presented lots of interesting challenges and forced me to learn a lot, usually the hard way :)


I need to know what I will need to drive small dc motors (on off in both directions or variable speed control in both directions if it isnt too hard to do) 3V and up. Do I need to make an H-bridge If so how is that done?

 Thanks :) ~Matt

This is another reason why I recomend people to start wit Picaxe; On the 28-board you can just attach 2 motors, one line of code, and they drive.

I think you may have interest in CowGod's postin here as well: http://letsmakerobots.com/node/359 - That is what I would go for in your saituation.

/ Fritsl

Yeah, I'm really happy with that Pololu controller. If you want to have full forward/backward/variable speed control of two motors, and the motors aren't too big (1 amp max per motor, or 2 amps if you only use one motor), that's the way to go. If you don't need that much control, you can do it with less, cheaper hardware.


so how do these rate compared to an h-bridge ? (cost/control/ease of use)

http://letsmakerobots.com/node/359  (cost/control/ease of use)


what is the difference between this http://www.solarbotics.com/products/l298/  and a pololu controller? 


The chip you linked to is a motor driver, not a motor controller like the Pololu unit. That means it is basically just two H-bridges in IC form, so it doesn't have any onboard intelligence and you need to do more of the work yourself. Pololu has a good description of the difference between motor drivers and motor controllers on this page, I'll copy and paste that:

  • Motor drivers are the simplest modules in the sense that all they do is provide power amplification for low-level control signals (e.g. PWM and direction) supplied by the user; on the other hand, that means that the master device to which the motor driver is connected must take care of the low-level, resource-consuming signal generation.
  • Motor controllers are motor drivers with additional intelligence: an on-board microcontroller generates the low-level signals and presents the user with higher-level interfaces and commands. For example, our dual serial motor controllers allow two DC motors to be controlled by a single serial line, and the master controller simply issues commands only when the speeds of the motors should be changed. Other motor controllers are even more complex, incorporating advanced acceleration commands, current sensing, feedback-based control, and more.

 You can see my longer post below for another explanation of the practical difference between H-bridges and motor controllers. Basically, you'd need to use more of your I/O pins and generate the PWM signals yourself.


 I see :)

 Thanks ~Matt

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.


(Now don't say you did not get an answer! :) 

/ Fritsl