# Use Arduino's PWM to control motor direction?

I'm looking for a way to allow Arduino to control a motor using PWM.

So far what I have gathered is that using Arduino's PWM sets the digital value of 255 to 5v and 0 to 0v.

However what I am looking to do is to set the motors to stop at 2.5v, move forward as it approaches 5v and move backwards as it approaches 0v.

Any suggestion on how I would go about accomplishing this will be highly appreciated.

• Just use two pins. One for ground and the other for 5v (with PWM). To reverse, have the first do the 5v (pwm) and the second be ground. Just note that the arduino pins can only provide enough current to power a small motor. Alternatively use an H-bridge Sep 22, 2014 at 18:16

You seem to have misunderstood PWM here. It's actually a digital signal which is constantly pulsing on and off very quickly (several hundred times per second). You don't actually change the output voltage at all -- it's only ever HIGH or LOW (+5v or +0v). The thing you change is how long the signal is HIGH for on each pulse.

When you're controlling a DC motor, this approach is actually better than varying the voltage. The constant pulses still make the motor turn at an adjustable speed, but without losing torque. PWM is also useful for controlling the brightness of LEDs, which don't respond so well to changes in current/voltage.

To address your original motor control question, there's no sensible way to change the motor's direction using the PWM value. You will need some additional circuitry which is able to reverse the polarity of the connections to the motor (meaning your PWM value just controls speed, and doesn't care about direction).

An H-bridge is the usual approach for this. Here's a fairly good tutorial which covers the basic principles: http://www.instructables.com/id/H-Bridge-on-a-Breadboard

Obviously you'd need to modify it for use with your Arduino project. The push switches could easily be replaced by digital signals from a couple of Arduino pins. The motor's power supply would be controlled by the PWM signal. You'll need an extra transistor for that, because you can't safely power the motor directly from an Arduino pin.

You can use a MOSFET to control a DC motor directly from an Arduino. Using a H-Bridge is another way to control DC motors. Probably the L298N is the most used one but I prefer TB6612FNG. It's a bit expensive, but uses much less current and is something with a better response.

You will find tons of tutorial with both modules.

Good luck!

continuous rotation servo demonstrates a different approach of using PWM.

A continuous rotation servo is a standard servo modified in 2 ways. First the "position feedback" in the servo is disconnected and set to always read as if the servo was in the center position. Second the "stop" which physically prevents the motor from turning 360 degrees is removed.

Then if you set the servo to 0 degrees it rotates fast in one direction if you set it 180 degrees it move in the opposite direction.