3

I'm new to PWM. This is my motor and controller. When I have the jumper in (ENA pins: I assume this gives the PWM pin the full 5V, as that is what the controller board runs on). My 6V 47 RPM motor goes at full speed. When I use PWM from my Pi, the speed reduces significantly, even on 100% duty.

Could this be because my GPIO PWM pin only allows 3V out from the Pi 3? Or am I possibly not understanding PWM? I am running a for loop to run at 100% duty cycle and increase the frequency gradually from 0 to 100Hz.

Also, before anyone asks, it is a 6V motor on a controller board that takes 5V. It doesn't need an external PSU unless you run a motor over 12V.

1

3 Answers 3

2

You have misunderstood how PWM is used to control DC motor speed.

To control the speed you vary the duty cycle, not the frequency. The frequency should be fixed at a reasonable value for your motor driver board. Say anything over 100 Hz.

A duty cycle of 0% stops the motor. A duty cycle of 100% sets the motor full on.

The voltage applied to the motor will be roughly supply voltage multiplied by duty cycle percentage divided by 100.

The motor will start spinning at some value of duty cycle above 0%. From that value to 100% duty cycle the speed increase will be fairly linear.

An example using PWM with a L298N based driver board.

2
  • Thank you, so I tried setting duty to 100 and tried a freq to 100,1000,10000 and didn't see any change. I'd there a frequency "sweet spot"?
    – Paul Toone
    Nov 22, 2017 at 14:28
  • Not really for the L298N (as you have). I'd use 800 Hz simply because it is a fairly default value. The L298N does drop about 1.5 volts from the supply voltage (from memory).
    – joan
    Nov 22, 2017 at 16:31
2

You wrote that you're not using an external power supply for the motor?

This motor probably requires a separate supply. I could not find the stall current for the motor, but under load the motor will probably drop the power enough that the Raspberry Pi no longer functions.

However, that isn't the worst problem. Motors are very noisy, electrically speaking. This means that they put voltage spikes into power wires. And when you stop a motor, while it's stopping, the motor turns into a generator and will put power back into the system. You will need capacitors to deal with the voltage spikes and a clamping diode to deal with the reverse current flow.

You can put motors on the same power supply as the electronics, but you have to be very careful.

I am very careful about my power supplies when I build robots. I usually use two different batteries, but that's my choice. Sometimes I will use a single power supply, and then I will take precautions.

1

You might find it easier to use gpiozero to control the motor. It simplifies the PWM control down to a 0 to 1 value scale, and the Motor class provides forward(), backward(), stop() and value setting from -1 to 1.

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.