I have attached a fan to pin #17 (through a transistor of course) and am writing a script to turn the fan on for a specified duration with a specified PWM duty.
#!/usr/bin/env python import os import sys import thread from time import sleep import RPi.GPIO as GPIO fan_pin = 17 def run_fan(pin, freq, duty) : global myPWM GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(fan_pin, GPIO.OUT) myPWM=GPIO.PWM(pin,freq) myPWM.start(duty) if len(sys.argv) < 4: print "Usage %s <PWM Frequency> <Duty> <Duration Percent>" % sys.argv else: print "Starting fan on Pin: %s, Freq: %s, Duty: %s" % (fan_pin, sys.argv, sys.argv) run_fan(fan_pin, float(sys.argv), float(sys.argv)) sleep(float(sys.argv)) print "Done." # myPWM.ChangeDutyCycle(0) # GPIO.cleanup()
When the two last lines are commented, and I run it with a frequency less than 100Hz, like this:
./fanOn.py 50 40 1
It generates a PWM signal of 40% duty for 1 second (which is not enough duty to turn my fan on), and then the pin goes high and remains constantly high, which makes the fan start spinning!
This does not happen if I run it with a frequency of 100Hz or higher:
./fanOn.py 100 40 1
Which makes it output a 40% PWM for 1 second and then the pin goes low and stays low.
Why is that? What happens at those lower frequencies?
I am running these in DietPi on a Raspberry Pi 3 and Python version 2.7.16
This does not happen with "gpiozero" library. That correctly shuts down the PWM on program exit.