# Using a pulse-width modulated signal to possibly reverse polarity

I'm trying to help an artist control a simple DC motor using a single signal. The signal is a digital output, 0/5V, and the motor runs with max 36V. The motor needs to run forwards and backwards. There is only 1 digital output available per motor.

I know how to generate a PWM signal, and wire it up to a transistor (with resistor at the base) so that the motor spins slowly or quickly, that much is working. We have a simple circuit for motor speed in one direction.

Also thanks to this site I just learned about H-bridges and found a few good pages on using 4 transistors to switch a motor's polarity.

My thought was to have the output signal be pulse width modulated 0% = full reverse, to 35% = slow reverse, and 65% = slow forward, to 100% = full forward.

To make that control the motor, I think about splitting the signal into two isolated parts. I "smooth" one part and use it as a "direction control" signal, low=reverse, high=forward. The other original PWM signal just modulates the power, for speed control.

I can't get it working in my circuit simulator. First problem is that my "smoother" is a filtered rectifier, and it always produces the same output voltage regardless of the duty cycle, which makes sense, because the diode bridge just converts everything to the same voltage. The second problem I have is how to make the "low" parts of the duty cycle make the motor run "fast" when it is in reverse- pretty sure I can fix that with a NOT gate and another transistor, but it feels like there must be a more elegant way. The fewer parts in this design, the better.

(Main problem is that I'm in over my head. Haven't built any circuits in 20+ years.)

The question is- how would you make this work, using a 36V power supply and some components to convert a 0-100% 5V PWM signal into a -36V to +36V PWM power source?

FYI, We do have the option of just buying a second microcontroller, which would give us more output pins, which would let us use one pin for a "forward" signal, and another for "reverse." It's just frustrating me that I can't make the single-signal idea work.

If you have an H-bridge controller that can switch cleanly at a PWM frequency which is sufficiently fast relative to the motor's inductance (the lower the inductance, the faster the PWM must be), driving it with a waveform that's 60% forward and 40% reverse will be a good way to drive it forward at 20% speed; 40% forward 60% reverse will be a good way to drive it backward at 20% speed. If both conditions above are met, driving a motor in this fashion will give a speed response which is much more linear than PWM'ing between driven and "open-circuit", and will also be more energy-efficient. Additionally, trying to drive the motor at a speed which is somewhat slower than it's presently turning will provide regenerative braking [i.e. allow motor energy to be fed reasonably nicely into the supply].

The important thing to note is that running the PWM too fast for the H-bridge controller may waste energy in the H-bridge controller; running it too slow for the motor inductance will increase the amount of energy wasted in the motor. If the PWM is much too slow, driving the motor at half speed may use many times more energy than trying to run it at full speed. If, however, the motor is driven with a fast PWM and the H-bridge can handle it, efficiency may be very good; a stalled motor driven at 75% forward 25% reverse will have about half the torque as would one driven at 100% forward, but will only take about a quarter of the power [about 75% of the time, it will draw about half as much current from the supply as it would if on 100%, and the other 25% of the time it return that same amount of current].

• Maybe combine @supercat and Colin's answers with a window comparator on the analog voltage (say 2.4 to 2.6V) to shut the motor off if it's necessary to have it sit completely still when the PWM ratio is near 50%. Jan 21, 2014 at 0:31
• @SpehroPefhany: Indeed, some sort of shut-down logic would be a good idea. As an alternative to filtering the PWM, it might be possible to wire things so that pin high=run CW; pin low=run CCW; pin floating=do nothing. Jan 21, 2014 at 0:34
• I should have added to the question that the motor will never be "stopped" other than instantaneously, it will be oscillating. The speed will be "roughly sinusoidal" with some dynamically generated and random variations. So we should not need a shutoff for the "near 50%" case. I'll measure the motor's inductance and figure out the components I need to make this work. Thanks for the quick replies.
– Yary
Jan 21, 2014 at 0:41

You can take the PWM signal, run it through a low-pass filter (e.g. RC filter), and then use a comparator to compare it with 2.5 V. Let the output of the comparator be the direction bit for the H-bridge. If the PWM value higher than 50%, then the output of the low-pass filter is higher than 2.5 V.

Now you still need to figure out how to get the PWM signal to the motor properly ... I haven't thought that through enough.