I'm trying to control a DC motor's speed with my Arduino Mega analogWrite(), but it doesn't seem to function properly. The motor never completely slows down, just slows down a bit (which is still pretty fast) and then stops spinning completely.

I'm trying to create a robot with different speeds, and it just has to have a slow speed. I know it isn't the code, since the relay and motor works just fine when I use the digitalWrite().

Is it better to write directly into the timer registers to implement the PWM, OR what else could be causing the problem?

  • To answer that we would have to see the code and how you wired things up. At the end of the day analogWrite writes to the registers, so there is no direct reason not to use it. A possible issue is the amount of power available for the motor. A schematic would help sort things out.
    – Nick Gammon
    Oct 5, 2016 at 22:14
  • Could it be that the power is in excess or in shortage, because i used a 12V relay to interface the motor to the arduino Oct 5, 2016 at 23:14
  • 2
    A relay will have a physical time it can take to operate. You need a motor driver board (or MOSFET wired suitably). Unless you are referring to a solid-state relay and not a mechanical one.
    – Nick Gammon
    Oct 5, 2016 at 23:40
  • Motors will stall if there isn't enough current. Motors have a minimal required speed, before they stall. It also depends on the load on the motor. Not that a 50% duty cycle doesn't mean the motor will run at half the speed. There is no one-to-one correlation.
    – Gerben
    Oct 6, 2016 at 8:47

1 Answer 1

  1. analogWrite() is already PWM, Arduino doesn't produce a true analog signal

  2. Motors tend to draw a lot of current so you might want to control the motor with a MOSFET then connect your power supply directly to the motor. PWM will still work in that case, you just need to connect the output pin to the gate pin of the MOSFET

  3. If you want more of an assurance when working with motors, try buying a motor shield. You current problem, speed problem and many will be resolved. It also makes it easy to control multiple motors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.