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! :)

 

 

I’m driving several

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 :slight_smile:

Dan

This is another reason why I

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: https://www.robotshop.com/letsmakerobots/node/359 - That is what I would go for in your saituation.

/ Fritsl

Yeah, I’m really happy with

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.

Dan

I need to know what …

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 :slight_smile: ~Matt

Pololu controllers

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

https://www.robotshop.com/letsmakerobots/node/359 (cost/control/ease of use)

~matt

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 :slight_smile: 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

(Now don’t say you did not

(Now don’t say you did not get an answer! :slight_smile:

/ Fritsl

Hehe, just keep in mind that

Hehe, just keep in mind that I’ve only been building robots and working with microcontrollers for like a month, so I am no seasoned voice of authority :slight_smile: Just sharing the things I’ve stumbled across as I flailed around wildly in the robotics universe :slight_smile:

Dan

Nice post, but one lie:

Nice post, but one lie:

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.

 

At Arduino website, here you can read:

After a call to analogWrite, the pin will generate a steady wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).

 

Aha, thanks for catching

Aha, thanks for catching that. I must have been confusing it with the PWMOUT command for the Basic Stamp, where you have to specify how long you want the pulse to be generated, and the code will wait until the pulse completes before continuing execution. See? Take everything I say with a grain of salt :slight_smile:

Dan

lols, ya… basic limits us a

lols, ya… basic limits us a lot in that way…

thats why i’m migrating to arduino now…

i’m thinking in buy some h-bridges from sparkfun:

http://www.sparkfun.com/datasheets/IC/SN754410.pdf

when i do, i will post some info.

other thing… you know any equivalent of this TI ic from maxim? ty

**wow **

thank you that was exactly what I needed to know

~Matt

 

P.S how long did that take you to type? it makes me tired just reading it :slight_smile:

Awesome, I’m glad that

Awesome, I’m glad that helped. And yeah, it made me tired to type it too :slight_smile:

Dan

**pololu **

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

~Matt

The chip you linked to is a

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.

Dan

oohhh.

I see :slight_smile:

Thanks ~Matt

Just in case you not already know.

Adafruit recently had motor driver shield out. This link has the info. It has 2 servo ports, 4 DC bi-diretional motors and 2 step motors.

http://www.adafruit.com/index.php?main_page=product_info&cPath=17_21&products_id=81

-Pandit

Paranoid or dense…

Hello,

This is my first post here. Maybe I’m paranoid or just dense or overthinking this, but I can not find this info anywhere. I have a GWS Mini servo with r/br/o wires and “J” connector like shown here:


Signal would come from whichever PVM pin I assign in my code and Red + / Brown - would be from the board (or aux power) and be on at all times? No resistors/diodes required? Just want to make sure I don’t blow up anything… This will move my IR sensor back and forth.

I just got my Arduino and don’t want to let the magic smoke out of the chips! :slight_smile: Once I get this going, I will try out my Polou Micro Dual Motor Controller with the motors/wheels Fritsl had listed here.

 

Thanks for any help!

Hi,I used an Arduino for

Hi,

I used an Arduino for this robot: https://www.robotshop.com/letsmakerobots/node/556. I connected the signals directly to the output pins and I haven’t released the magic smoke yet. I have researched the resistor or no resistor subject on the net and some people add a resistor and some don’t. If you want to be on the safe side, you can add a 330ohm resistor like the Picaxe crowd does.

The servos in my solution are powered by a separate power source. That is, I have a 9V battery connected to the Arduino and 4 1.2V cells for the servos. The GND signal of the two power sources are connected to create a common ground.

Hope this helps.

/Jesper