PWM maped speed-change not happen on my DC motor, it's just ON or OFF

Hi.

I read about PWM and how it should work nice with DC motors, I upload the code to the Arduino Uno, using pin 3,5,6,9 or 10 - and mappes the speed from 0 - 256. But it do not work. 0 = off, and any number between 1 and 256 = on, but no change in speed at all.

Do PWM only work with some type of DC motors?

Thanks for any answers.

This is the DC motor I'm testing with:

http://www.ebay.com/itm/DC-3V-5V-6V-100RPM-N20-Mini-Full-Metal-Gearbox-Gear-Motor-Speed-Reduction-Motor-/112176903094?hash=item1a1e43b3b6:g:zGcAAOSwe7BWyyRo

But it do not work. 0 = off, and any number between 1 and 256 = on, but no change in speed at all.

It's hard to believe that a "1" runs the motor at all!

  • Try simplifying your code to a single analogWrite() (probably followed by a delay while you wait to see what happens). What you're describing will happen with digitalWrite().

  • Or connect an LED (and resistor) in parallel with the motor to see if it dims with PWM. If it doesn't dim, you're not getting PWM. If it doesn't dim at the motor connection (driver output) try directly connecting the LED & resistor to the Arduino I/O pin.

I'd guess you've got a bug in your code, but there could be something screwy with your driver circuit. (You are using a motor driver circuit, right?)

How do you have it wired up? What are you mapping?

Hi. This is my code, real simpel. Yes the engine run on 1 or any number, I thought is runs like a boolean operation, 0 = "false", while any number is "true".

No motor driving circuit, small engine connected directly to the Arduino board to ground and pin 3.

     const int DC_MOTOR_PIN  =  3;

      void setup() {
          pinMode( DC_MOTOR_PIN, OUTPUT ); //0 - 256
      }

      void loop() {
          digitalWrite( DC_MOTOR_PIN, 1 );
          delay( 2000 );
          digitalWrite( DC_MOTOR_PIN, LOW );
          delay( 200 );
          digitalWrite( DC_MOTOR_PIN, 200 );
          delay( 2000 );
          digitalWrite( DC_MOTOR_PIN, LOW );
          while(1); // 
      }

Look into providing alternative power to motors.

As for the code, you can get rid of the map, because you have nothing TO map.

Change the digitalWrite(s) to

analogWrite( DC_MOTOR_PIN, 0); //OFF
analogWrite(DC_MOTOR_PIN, 255); //fully ON
analogWrite( DC_MOTOR_PIN, 100); somewhere in the middle

a delay of only 200 is pretty short, 1/5th of a second.

I testet with analogWrite and the motor PWM, thanks! Now I feel like a real amateur, and hm, that's actually not so far from the truth. Thanks again.

Don't feel bad, analogWrite is a poor name for writing PWM anyway!
And digitalWrite should really take a boolean so that your mistake would be spotted by the compiler!

No motor driving circuit, small engine connected directly to the Arduino board to ground and pin 3.

You really need to change that. The I/O pins are only good for 40mA MAX, and I wouldn't recommend anything over 30mA. The motor also needs a diode across it, and it's own supply and controlled by either a mosfet or bjt. What's about to happen is you are going to fry your arduino.

MarkT:
Don't feel bad, analogWrite is a poor name for writing PWM anyway!
And digitalWrite should really take a boolean so that your mistake would be spotted by the compiler!

C++ does not know the concept of boolean AFAIK. Did I miss something? I agree that it would be nice to have at least a compiler warning for "strange" values.

Thanks Tinman13kup, the small motors use 35 mA. I did not know about mosfet before you mention it, but I will buy them from Ebay. What kind of diode would you recommend?

Hi Olf 2012, have to say I don't know what an AFAIK is?

Fillock:
Hi Olf 2012, have to say I don't know what an AFAIK is?

Sorry, it is just an abbrevation for "as far as I know"

IMHO everyone should understand AFAIK, but then IANAL
YMMV


Oh yes, back to boolean and C++ type checking, you're right, its that awful!

If you only ever plan on running the motors in one direction, then the mosfet is all fine and dandy, and any common 1N400x diode will be fine. If you decide you want the motor to spin both directions, then a motor controller (h-bridge) is what you need. The controllers also make it easy to attach that critical second power source.

Your motor might only pull 35mA freewheeling, but it will pull much more current when under load or stalled. Start up also sees a very brief current surge.

Most H-brides I find on Ebay are for stepper motors, can they be used for DC motors also?

Generally yes, but given your limited experience in electronics, I would recommend sticking to reputable sellers with good tutorials, libraries, and even customer support. Here is a DC/stepper motor driver from Adafruit. They have lots of interesting items for the arduino, backed by the aforementioned support. SparkFun is another site to visit.

Most of the stuff of ebay is cheap knockoffs using inferior parts. They tend to leave you on your own after the sale, and documentation is normally non-existent. When you buy something, you need to know what the pinout is, what voltage/current specs are, what input parameters are needed, any libraries, and what the output specs are.

Take a simple buck/boost module. It doesn't help that it's real cheap if you don't know what the current limits are, or how much ripple is on the output.

Yes I notis that when I buyed from Ebay, I get the electronics but no code or tutorial. I will buy the H-bridge from Adafruit :slight_smile: