piveloper
Posts: 11
Joined: Wed May 03, 2017 3:59 pm

Glitches on hardware pwm

Tue Jun 06, 2017 8:18 am

Hi,
I have connected the Rapsberry Pi to a Stepper driver which controls a stepper motor. Recently I observed step losses on the motor every second or two. This is only noticable at higher RPM (>500 Hz PWM). With a scope I can see that the raspberry pi skips rectangle pulses (see screenshot). Are these glitches normal or is my raspbbery pi broken? Any Ideas if a misconfiguration of the pi can lead to this phenomena?
Image

I used following code to produce the pwm in the screenshot:

Code: Select all

import RPi.GPIO as GPIO
import sys  # exit program
import time
import signal

GPIO.setmode(GPIO.BCM)
pin_step = 18
GPIO.setup(pin_step, GPIO.OUT)
pwm = GPIO.PWM(pin_step, 1)
pwm.start(10.0)
freq = 1000
pwm.ChangeFrequency(freq)


def signalHandlerSIGINT(signal, frame):
    pwm.stop()
    GPIO.cleanup()
    sys.exit(0)


signal.signal(signal.SIGINT, signalHandlerSIGINT)

while True:
    time.sleep(10)
Best regards

MaxK1
Posts: 1043
Joined: Sun Aug 26, 2012 11:34 pm

Re: Glitches on hardware pwm

Tue Jun 06, 2017 8:48 am

Which O/S and is it fully up-to-date? What Pi model? Overclocked or not? What else is connected? What else is running?
You are in a maze of twisty little passages, all alike.
When General Failure and Major Disaster get together, Private Parts usually suffers.

mattmiller
Posts: 2243
Joined: Thu Feb 05, 2015 11:25 pm

Re: Glitches on hardware pwm

Tue Jun 06, 2017 8:52 am

AFAIK - although pin 12 (BCM18) is capable of using hardware PCM - the RPi.GPIO library treats it like all the others and uses software PWM on it

Try using pigpiod if you need hardware pwm on pins

piveloper
Posts: 11
Joined: Wed May 03, 2017 3:59 pm

Re: Glitches on hardware pwm

Thu Jun 08, 2017 2:31 pm

mattmiller wrote:AFAIK - although pin 12 (BCM18) is capable of using hardware PCM - the RPi.GPIO library treats it like all the others and uses software PWM on it

Try using pigpiod if you need hardware pwm on pins
Thanks for this information! Using pigpiod I was able to get a much more stable pwm signal. Unfortunately pigpiod needs a daemon already running in the background which consumes 5-10% of the cpu time.

I resolved this issue using wiringpi+hardwarepwm:
https://raspberrypi.stackexchange.com/q ... /9725#9725

Code: Select all

import wiringpi

pin = 18
freq = 50
wiringpi.wiringPiSetupGpio()

wiringpi.pinMode(pin, 2)
wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS)

piPwmClock = 19.2e6
range_value=200
clock_value=int(piPwmClock / ( range_value*freq))

wiringpi.pwmSetClock(clock_value)
wiringpi.pwmSetRange(range_value)
dutyCycle = 0.5
value = int(dutyCycle*range_value)
wiringpi.pwmWrite(pin,value)
The code above results in a stable hardware pwm with 50 Hz.
Image

I was only able to get the hardware pwm on channel 13 and 18 simultaneously with same frequency. Is there a way to have two hardware pwms with different frequencies on my rpi3 model B?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Glitches on hardware pwm

Thu Jun 08, 2017 3:06 pm

There are two hardware PWM channels: channel 0 and channel 1. Each channel can have its own frequency. Channel 0 can be fed to GPIO 12/18, channel 1 can be fed to GPIO 13/19.

pigpio will let you set the frequency and duty cycle (e.g. http://abyz.co.uk/rpi/pigpio/pigs.html#HP ).

piveloper
Posts: 11
Joined: Wed May 03, 2017 3:59 pm

Re: Glitches on hardware pwm

Thu Jun 08, 2017 3:30 pm

joan wrote:There are two hardware PWM channels: channel 0 and channel 1. Each channel can have its own frequency. Channel 0 can be fed to GPIO 12/18, channel 1 can be fed to GPIO 13/19.

pigpio will let you set the frequency and duty cycle (e.g. http://abyz.co.uk/rpi/pigpio/pigs.html#HP ).
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?

I searched for it a lot, but I could not find any python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Glitches on hardware pwm

Thu Jun 08, 2017 4:00 pm

piveloper wrote: ...
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?

I searched for it a lot, but I could not find any python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).
You pays your money and you takes your choice. :D

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

piveloper
Posts: 11
Joined: Wed May 03, 2017 3:59 pm

Re: Glitches on hardware pwm

Thu Jun 08, 2017 5:31 pm

joan wrote:
piveloper wrote: ...
I want to avoid running pigpio running in the background as is consumes 5-10% CPU time permanently. But if pigpio can use both hardware pwms there must be a way to use them without pigpio daemon, right?

I searched for it a lot, but I could not find any python code snippet on the internet which enables two hardware pwms (one on BCM18 and one on BCM13).
You pays your money and you takes your choice. :D

http://abyz.co.uk/rpi/pigpio/python.html#hardware_PWM
:roll: well, for that I still need pigpio daemon running. Would be great if there is a solution without a CPU consuming background task.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Glitches on hardware pwm

Thu Jun 08, 2017 5:50 pm

It would be quite easy to do. You could take the appropriate parts of gpioHardwarePWM from pigpio.c and incorporate it into http://abyz.co.uk/rpi/pigpio/examples.h ... nimal_gpio

piveloper
Posts: 11
Joined: Wed May 03, 2017 3:59 pm

Re: Glitches on hardware pwm

Thu Jun 08, 2017 6:42 pm

joan wrote:It would be quite easy to do. You could take the appropriate parts of gpioHardwarePWM from pigpio.c and incorporate it into http://abyz.co.uk/rpi/pigpio/examples.h ... nimal_gpio
That may work. But it will take some time for me to figure out how to do it, since I never wrote a c package for python.

Thank you!

Return to “Troubleshooting”