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

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,852
  • 5
  • 76
  • 108