How to Program a DC Motor?

I have seen tons of google results regarding this topic, but none seems to be working for me.

I am really new to this, so sorry if it's a really newb question.

I am trying to program a DC Motor. But I'm not sure how to do so.

I am using an arduino Uno and a DC Motor. I don't have any motor shields or anything.

I want the motor to go from speed of 0 to max. I don't want any inputs, just want to spin the motor.

I wrote something like this:

void setup() {

pinMode(13, OUTPUT);

Serial.begin(9600); }

void loop() {

digitalWrite(13, 255); delay(15);

}

That was just to see if motor spins, but it doesn't. I tried a servo example and saw if it worked if i treated it as a servo, and it worked. but I don't want that, I need it to spin. Can somebody please tell me what am I doing wrong?

Unless your motor is VERY tiny it will draw too much current for an Arduino pin to power it. You should use a transistor to handle the current. See:

http://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/transistors

You can drive small hobby motors directly with the analogWrite() - which will PWM (turn on and off really fast) to effectively provide 0 - 5v (depending on arduino) to drive the motor and adjust its speed - sort of. Its fine on motors with little load. For demonstrations etc.. As speed will never be constant without feedback, temps, humidity, motor wear, will affect 'speed'. So it depends on if speed control is to be relative (who cares how fast just as fast as possible or 1/2 that) vs calibrated (2200 RPM +/- X%).

But anything more than a hobby motor drawing ~40mA loaded, you're gonna need driver hardware, that would allow higher current and possibly higher voltage motors to be speed controlled this way.

Hrendor66: You can drive small hobby motors directly with the analogWrite() - which will PWM (turn on and off really fast) to effectively provide 0 - 5v (depending on arduino) to drive the motor and adjust its speed - sort of.

I consider that extremely poor advice. You should never plan to power a DC motor directly from an Arduino I/O pin. The motor is an inductive load which will damage the pin's driver unless you protect it with a suitable external circuit, and even the tiniest most microscopic motor you're likely to encounter will have a stall current greatly exceeding the 20mA which is the maximum you should pass through an Arduino's I/O pin.

While it's conceivable that somebody somewhere finds a nano motor which has a stall current below 20mA at 5V, if you know enough to be working with something like that you will already know how to drive it. But aside from exotic situations like that, as a general guide you cannot and should not power a DC motor directly from an Arduino I/O pin. It won't work, and is very likely to destroy the pin.

While it's conceivable that somebody somewhere finds a nano motor which has a stall current below 20mA at 5V

Its not only conceivable, there are tons of them - I bet you have one in your cell phone now that provides haptic feedback. How many old cell phones are sitting in your drawer?

So while your not wrong is saying its not good practice, you are wrong is saying IT WONT work or 'likely' will destroy anything.

I did warn of loading and current capacities - and sue me for presuming some knowledge of ohms law on others part. Someone diving into an arduino and NOT understanding simple ohms law deserved to learn the 'oops I blew it up' lesson the more expensive way - a way, which btw were always the lessons I've never forgotten. So...

Also - my Uno is rated at 40ma, same with my Mega2560... which I did not realize varied from arduino to arduino - I see some are only good to 20mA.

Frankly; telling someone they cant and not give reason why - or ignoring their posts - is worse than advise that could be argued as 'dangerous'. Ignorance is far more dangerous than smoking an arduino. ;)

johnwasser: Unless your motor is VERY tiny it will draw too much current for an Arduino pin to power it. You should use a transistor to handle the current. See:

http://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/transistors

..... never forgetting the diode as shown in that link. (Even if it was run directly from an Arduino IO pin.)

This part of the OP's post confuses me:

I tried a servo example and saw if it worked if i treated it as a servo, and it worked.

Are you saying you connected and ran a 2-wire DC motor as a 3-wire servo and ran it using servo code like myServo.write? Or did I misunderstand something?

Hrendor66:

While it's conceivable that somebody somewhere finds a nano motor which has a stall current below 20mA at 5V

Its not only conceivable, there are tons of them - I bet you have one in your cell phone now that provides haptic feedback. How many old cell phones are sitting in your drawer?

Quite a few - but none with haptic motors with a peak current below 20mA, and also none that could be driven without flyback protection. Even the smallest haptic motors are likely to be well over 50 mA sustained current and with peaks even higher. Certainly there do exist motors that can be run from a 20mA supply but they're pretty exotic. In any case, the original question and your response were not about pico motors. I was responding to your comment that "You can drive small hobby motors directly" which is simply not true in my opinion for any reasonable interpretation of "small hobby motor" and is liable to cause somebody following your advice to damage their Arduino.