0

I want to make a 120kHz PWM signal from my raspberry pi 3. Since it uses a 1,2gHz CPU. I tried using RPi.GPIO.PWM(Chanel, frequency), the max frequency I got was about 8kHz. How can I come up to 120kHz?

4
  • How are you confirming that? Although it's in C and uses libbcm2835 you could try this quick example of a 40 kHz wave and see what your test methodology gives you for that: raspberrypi.stackexchange.com/a/53583/5538 Also, people may be inclined to assume you are just doing it wrong if you do not post the actual code you've used which is supposed to generate 120 kHz pulses. No one wants to bother posting an answer only to hear you say, "Oh that's how I was already doing it, sorry for wasting your time."
    – goldilocks
    Sep 4, 2016 at 12:23
  • @goldilocks That example is using hardware PWM.
    – joan
    Sep 4, 2016 at 12:57
  • @joan I don't use RPi.GPIO so I don't know exactly what the OP is doing here but "Chanel" (sic) implied hardware to me. If that's not the case then yeah, you probably couldn't do this with much consistency or precision on that scale.
    – goldilocks
    Sep 4, 2016 at 12:59
  • 1
    @goldilocks RPi.GPIO only supports software PWM at the moment. Channel is just the term used to identify a GPIO (both pin numbering and Broadcom numbering are supported).
    – joan
    Sep 4, 2016 at 13:06

2 Answers 2

2

I'm not sure that you can reach 120kHz with software PWM. That would require a wakeup every 8µs or so.

Use hardware PWM, e.g. my pigpio provides http://abyz.me.uk/rpi/pigpio/python.html#hardware_PWM and http://abyz.me.uk/rpi/pigpio/python.html#hardware_clock

1

The RPi module has only software PWM, and you get 8 kHz with default priority. With something like this:

sudo chrt -f 99 python3 test_pwm.py

I got a bit more then 60 kHz. Using RPi.GPIO.output, rather then PWM, I got 264 kHz.

Notes

  • at max frequency, the pulse width is stuck at 50%
  • with 2 PWM signals, the max frequency is around 40 kHz and phase varies from -145° to 100°
  • don't use sleep/wait for timing (it's increasing the uncertainty), make a loop with a simple instruction (e.g. var=0)
  • hardware PWM is said to have the base frequency at 19.2 MHz...

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.