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!

Ryanator13
  • 13
  • 2
  • Or you can instead use a hardware sig gen, such as the 1Hz~150kHz, XY-LPWM: https://raspberrypi.stackexchange.com/questions/104779/how-can-rpi4b-python-uart-talk-to-xy-pwm-signal-generators. – tlfong01 Apr 17 '20 at 02:19
  • Or ICL8038/AD9850 for higher frequencies: https://raspberrypi.stackexchange.com/questions/96423/pi-hat-waveform-generator-for-raspberry-pi-3b. – tlfong01 Apr 17 '20 at 02:25

1 Answers1

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

joan
  • 71,024
  • 5
  • 73
  • 106
  • 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 '20 at 16:36