1

I am attempting to setup a PWM signal on Pin 12 (GPIO #18) using a Raspberry Pi 3. I am capable of sending a PWM signal, however it is quite off from the target frequency. I have an o-scope that I have been using to measure the signals in order to ensure that it is operating correctly. Here is my code:

import spidev
import time
import RPi.GPIO as GPIO

# Choose pins here (in reference to BOARD) and frequencies FROM DATASHEET
FCLK_pin = 12
EN_pin = 29
FCLK_freq = 500 # Recommended LPF Cutoff
duty = 50.0

def GPIO_setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(EN_pin, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(FCLK_pin, GPIO.OUT, initial=GPIO.LOW)
    pwm = GPIO.PWM(FCLK_pin, FCLK_freq*60)
    return pwm;

fclk_pwm = GPIO_setup()
fclk_pwm.start(duty)
time.sleep(1)
GPIO.output(EN_pin, GPIO.HIGH)
time.sleep(5)
fclk_pwm.close()
GPIO.cleanup()

The desired frequency is 500*60Hz, or 30kHz. Here is what I get back on the o-scope when I replace FCLK_freq*60 to a solid 1000 (so expecting 1000Hz): O-scope readings at PWM set to 50% duty and 1000 Hz.

Here is the readout when set to 30000 (so expecting 30kHz): O-scope readings at PWM set to 50% duty and 30 kHz.

Am I going about this the wrong way? Any advice / help is appreciated!

2

1 Answer 1

2

Using software PWM for a frequency of 30 kHz is ambitious on the Pi.

I would use hardware PWM (available from GPIO 12, 13, 18, 19).

See http://abyz.me.uk/rpi/pigpio/python.html#hardware_PWM

1
  • This was definitely the solution. I updated the script and included the pigpio hardware PWM and verified it via scope. Thanks for the answer!
    – Ryanator13
    Apr 17, 2020 at 16:36

Your Answer

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

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