Software PWM eats ~2% CPU when accomplishing nothing
A Python module to control the GPIO on a Raspberry Pi
Brought to you by:
croston
When the duty cycle is set to 0 and probally 100 the software pwm contionues to process it as pwm, it should set the pin high/low and wait for the duty cycle to change
Watch the CPU usage of this script, it consumes around 2% of my CPU (B+ Pi @ 800Mhz) full time, it should drop to 0 every 10 seconds for 10 seconds
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm=GPIO.PWM(12, 440)
pwm.start(0)
while True:
pwm.ChangeDutyCycle(50)
time.sleep(10)
pwm.ChangeDutyCycle(0)
time.sleep(10)
except KeyboardInterrupt:
GPIO.cleanup()
an easy way to view the CPU usage of the script is this command
top -p $(pgrep -f PWMtestScript.py | tail -1)
*tail was used because the script is run with sudo so pgrep will return 2 pids
The workaround for this would be to start/stop the pwm process, however that seems to have a worse issue
https://sourceforge.net/p/raspberry-gpio-python/tickets/94/
Just to play devil's advocate: Why are you worrying about 2% CPU usage?