DC motor noob here

Hello all,

I'm working on a project that uses a DC motor, and I know next to nothing about using them in tandem with Arduino.

Here are some basic questions:

  • How can I control the motor's speed?
  • How can I control the direction in which the motor moves?
  • I want to write a program in which the user can input a value using push buttons and an LCD screen, and then I want to be able to calibrate the motor's speed using those values. How can I do that?

Thanks!

To control the direction and speed of a DC motor, you need an H-bridge controlled by PWM from the Arduino. The L298n dual H-bridge chip is a popular choice. With adequate heatsinking, it can control 2 motors that need up to 2A current each, or one larger motor if you parallel the channels. You can buy a shield based on the L298n if you don't want to make the hardware yourself.

and then I want to be able to calibrate the motor's speed using those values. How can I do that?

What do you mean by 'calibrate'? If you just want to increase or decrease the motor speed as the user presses a up or down switch that is pretty simple software function in your sketch and using a motor driver as alread suggested. If however you wish the user to input a specific RPM value and make the motor change to that speed then in addition to the motor driver you will need some form of physical feedback sensor attached to the motor so your sketch can measure the motor's speed at any instant and adjust it as required. That requires more hardware and more software functions in your sketch.

Lefty

Another new Arduino / Electronics person here. Thought I'd jump on this thread instead of starting a new one.

I'd like to know if there are any tutorials available on using DC motors with the Arduino that go into great detail on not just setting up an H-bridge which seems to be what most are, but also how to supply external power correctly? I've looked at what must have been a dozen or more tutorials that basically show you how to set up an h-bridge and then that is all.

For example, I'd like to build a bot that uses two DC 9V high-speed motors you can get from Radio Shack for about $4 each. Both run to go forward, to turn left one runs, to turn right, the other runs.

The one thing I get out of all these tutorials is that Arduino cannot power these by itself. You need a battery of some sort. What would be the ideal solution to power this set up? I'd like it to run for more than a minute at a time. This is where I get lost and there isn't much out there that I've found that explains all of this. The most I have seen are demos showing a tiny motor being run with a tiny square 9V battery. What about a larger battery that a lot of the RC cars use that will run it for a long time?

If someone knows of a project or a tutorial that shows something long this line for Arduino that would be a great help!

Try:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html
and
http://www.thebox.myzen.co.uk/Workshop/Motors_2.html

This is where I get lost and there isn't much out there that I've found that explains all of this.

You need to know the current the motor takes. Say it takes half an amp (500mA) then you would expect it to run a maximum of 3 hours on a 1500mA / Hour battery (actually it will run a bit less). So look at the Milly ampes per hour battery capacity the clue is in the name of the units.

demos showing a tiny motor being run with a tiny square 9V battery.

These are useless forget them.

Thanks Mike, while I gleaned some info out of those links, I'm still a bit lost.

Maybe I'll rephrase the question a bit, so someone can maybe explain this in better detail.

Object is to get two DC motors working as a mobile platform. To be able to go forward, left, right, reverse.

Arduino UNO as the MC.

I am looking at buying two of these motors: Micro Metal Gearmotor 30:1 - ROB-08911 - SparkFun Electronics

  • 30:1 Gear ratio
  • 430rpm @ 6V
  • 32mA @ 6V
  • 500mA stall current @ 6V
  • 2.6 oz inches torque @ 6V

For a motor controller, I am looking at this: SparkFun Ardumoto Shield Kit - KIT-14180 - SparkFun Electronics
Arduino Motor Driver shield retail. It says it can run two motors at 2A maximum. It also takes external power.

For a battery for just the motors, I was looking at: Lithium Ion Battery - 2Ah - PRT-13855 - SparkFun Electronics

  • 3.7V at 2000mAh

Now.. Can I just hook the motors up to this shield, then hook the battery to the ext power on the shield with this battery? If not, why not? This is where I get lost in the entire power flow aspect. Do I match motors to the voltage on the battery? Does the motor controller just handle it by itself? So lost..

Philthy:
Now.. Can I just hook the motors up to this shield, then hook the battery to the ext power on the shield with this battery? If not, why not? This is where I get lost in the entire power flow aspect. Do I match motors to the voltage on the battery? Does the motor controller just handle it by itself? So lost..

Not quite. That board based on the L298 is quite capable of driving those motors, but there are two contraints on the voltage applied to Vin on jumper JP1 (the supply that is used for powering the motors):

  1. It must be at least 2.5v above the Vcc supply, which in this case means at least 7.5v

  2. The total voltage lost in the L298 is between 1.8v and 3.2v at 1A current (it will be a little less at 500mA). So if you want to drive the motors at up to their specified voltage (6v), you may need to provide as much as 9.2v.

So a 3.7v battery is too low a voltage on both counts.

Not sure what you mean by 2.5v more than the vcc? Where are you getting vcc from? Why do we know it has to be 2.5v more? (Sorry if this is common electrical knowledge)

There appears to be very few options of a battery to run these tiny motors? The only thing I found was Polymer Lithium Ion Battery - 1500mAh 11.1v - PRT-10470 - SparkFun Electronics
which is 11.1v.

I often see people powering their motors with a 4xAA .. which only comes out to 4.8v if you use 1.2v rechargeables.

I guess I am not understanding how people are able to use motors with such a small selection of battery types? Is it the 6V motors that are making my choices so limited? What do people do with 12V motors?

Some sites like Sparkfun might to do well to sell packages that come with two motors, controller, and the correct battery to power it I think. I wish I could make heads or tails of any of this. I can program my way out of anything, but trying to learn how to supply correct battery power to a motor melts my mind.

Philthy:
So there are very very few options of a battery to run these tiny motors? The only thing I found was Polymer Lithium Ion Battery - 1500mAh 11.1v - PRT-10470 - SparkFun Electronics
which is 11.1v.

I often see people powering their motors with a 4xAA .. which only comes out to 4.8v if you use 1.2v rechargeables.

I guess I am not understanding how people are able to use motors with such a small selection of battery types? Is it the 6V motors that are making my choices so limited? What do people do with 12V motors?

Wish there was a chart that I could reference this out. I just want to make a tiny robot zoom around.

You could probably just use an L293 or L293D (or shield based on such) - see the datasheet:

http://www.datasheetcatalog.org/datasheet/texasinstruments/l293.pdf

You still wouldn't be able to use a 3.7 volt battery; but you could use 4.8 volts - though I would seriously think about using at least 6 volts for a 6 volt motor (get all the torque you can); that would just need a 5-cell battery (if using rechargeables). Though you'd still need something larger to run the Arduino (due to the regulator, even 6 volts won't be enough - you need at least 7-7.5 volts). Unless you wanted to run 4.8 volts into the Arduino's Vin pin (I think that's right - the pin for the power that bypasses the on-board regulator - check the schematic). You can't run anything more than 5 volts into it, though...

Another option might be to build your own set of h-bridges from some small transistors...

Okay. So, it is really dependent on the voltage of the motors being used. If I have two 3V motors, I would want around 3V (or higher) of whatever battery type I want. If I choose 6V motors, I would want around 6V (or higher) of battery type, 12V motors around 12V (or higher), etc etc? So long as the motor controller allows the selected voltage to be run off of it. That 293D chip says 4.5v-36v, so I would NOT want to use 3V motors with it reliably correct? Also, if I went with 4 motors (dual 293D board), nothing would change, the battery would just get drained quicker? That is where the mAh comes in to play when choosing.

That 3.7v battery would be good for 3V motors and a controller that also supported that low of voltage, yeah?

(Hope I am getting this)

So then, if all that above is correct. Where does amperage come into play? I am guessing this is why a 9V square battery wont run jack, because it can't pump out enough juice at once? I don't see amps listed by batteries, so this looks like another stumbling block.

Philthy:
Okay. So, it is really dependent on the voltage of the motors being used. If I have two 3V motors, I would want around 3V (or higher) of whatever battery type I want. If I choose 6V motors, I would want around 6V (or higher) of battery type, 12V motors around 12V (or higher), etc etc?

It depends.

Some motors rated at one voltage can be run at a higher voltage, although life of the motor may be shortened (bearing and commutator, mainly). Some can be run at a lower voltage than their rating; depending on what kind of "cogging" effect on the rotor there is, you can sometimes run a motor on a fraction of its voltage rating (note, though that torque will be affected). For instance, I have a 90 VDC motor that I can run fine on 12 volts (and it still has quite a bit of torque - it was a motor for a treadmill).

Generally, you don't want to push a motor beyond 10-25% of its voltage rating; for the longest life, though, don't run a motor at a higher voltage than it is rated for.

Philthy:
So long as the motor controller allows the selected voltage to be run off of it.

Correct.

Philthy:
That 293D chip says 4.5v-36v, so I would NOT want to use 3V motors with it reliably correct?

Well - there is always going to be an internal voltage loss on the h-bridge; you might find that you could supply it 4.5 volts, but the output hovering around 3 volts, in which case you -could- use it with a 3 volt motor. Read the datasheet; it should have some info on this.

Philthy:
Also, if I went with 4 motors (dual 293D board), nothing would change, the battery would just get drained quicker? That is where the mAh comes in to play when choosing.

Depending on the mAh rating of the battery, and the mA ratings of the motors.

I'll try to explain more in a bit...

Philthy:
That 3.7v battery would be good for 3V motors and a controller that also supported that low of voltage, yeah?

Yeah; likely with motors that small and a small battery, you would use a discrete transistor/mosfet h-bridge (and depending on your needs, those transistors/mosfets might all be SMT components).

Philthy:
(Hope I am getting this)

I think you're on your way... :slight_smile:

Philthy:
So then, if all that above is correct. Where does amperage come into play? I am guessing this is why a 9V square battery wont run jack, because it can't pump out enough juice at once? I don't see amps listed by batteries, so this looks like another stumbling block.

Most batteries (or cells) don't have that information listed on them (many rechargebles do, though); you generally have to look up the datasheets for the batteries at the manufacturer's website - so when in doubt, check there. You can also get a good idea what certain cells/batteries can supply (on average) from wikipedia (can't find the article right now, but there is one out there - somewhere).

Now - to explain amp-hour ratings and how long something will run...

Let's say you have a motor that draws 1000 mA (1 Amp), and you have a battery that has a rating of 1000 mAh (1 Amp-Hour) of capacity. That means that motor can run for one hour (this isn't completely true - but accept this as "true" for now). If the motor was rated for 500 mA (.5 Amp) - then it could run for two hours off that same battery. Now - what if the motor drawed 2000 mA (2 Amps) when running? Could you still use the battery?

Sure you can! How long would the battery supply current? It would supply the current for .5 hours (half-hour). See how the math works? Simple division/multiplication.

Now - as noted - this is called an "ideal" situation. The truth is worse. You won't get these numbers and times, even if both are rated the same way. This is where the datasheet of the battery comes into play.

You see, when you draw current from a battery with a load, the battery heats up (caused by the amount of current being drawed and its internal resistance - the resistance is noted on the datasheet, btw); this changes things drastically. The change is called a "de-rating". Other things can cause further de-rating of a battery's capacity (mainly external temperature; humidity can also come into play - but the main factor is temperature). On the datasheet, they note all of this - you'll find a "de-rating" curve graph (or maybe more than one) that will show you how these factors work against you.

So - in reality - you'll never get 1 hour of run-time with a 1 amp motor on a 1 Amp-Hour battery; more than likely, it will be around only 30-40 minutes of run-time, and that at only a certain temperature (some kind of ambient room temperature - 70 degrees F, for instance); with higher or lower temperatures (both external, and internal to the battery as it is used) this will change. If you want to know your "real" run time, you'll have to take that into account as well.

Hope this helps to explain things...

:slight_smile:

It most definitely helps! Thanks for putting in the effort to help hammer some of this home.

It honestly feels like I took a step forward. I've been pouring over everything I can but somehow that just wasn't sticking.

Philthy:
Not sure what you mean by 2.5v more than the vcc? Where are you getting vcc from? Why do we know it has to be 2.5v more? (Sorry if this is common electrical knowledge)

The motor driver board you are looking at is based on the L298 chip, and I'm getting those figures from the L298 data sheet, see http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00000240.pdf. On page 3 it says that in the operative condition, Vs must be at least Vih+2.5 and no more than 46v. Vih is the logic high level applied to the input pins, which is 5v in that design.

For the battery, you could use the lithium version of the standard 9v battery (e.g. Energizer 522, capacity ~600mAH at 1A), but they are quite expensive. Or you could also use 8 rechargeable NiMH AA cells. I don't recommend alkaline cells for motors, they may work but their capacity drops off rapidly at high currents.

Another quick question:

What type of IC would someone use for a lower voltage motor?

Say 3V or lower that some of the smaller toys might use? The popular ones seem to only start from 5V upwards.

Asking this because I have not bought a mobile platform(s) yet from a thrift store. Never know what you end up with and if I will keep the motors or replace them, etc.

Doing a search, I found the 17C72, which looks to handle 2.7 V ~ 5.5 V .. http://search.digikey.com/scripts/DkSearch/dksus.dll?Cat=2556626&k=%2017C72

Though I have no idea how large that is, or if it's a tiny tiny chip that only machines can solder.

Though I have no idea how large that is,

Look at page 10 of the data sheet. It is 3mm square and the solder contacts are underneath the chip so you can't use a soldering iron you need to use re-flow soldering techniques. Not something for a beginner.

Thanks Mike, starting to get the hang of all this.

Thanks everyone for the helping hand. I managed to get my first H bridge built using some parts from an old RC toy I bought for a few bucks. Success!

Now to find a treaded platform that I like. :smiley: