Created
September 8, 2020 22:23
-
-
Save GeroBH/d7af68dc298b1dc26f77e570b8059e77 to your computer and use it in GitHub Desktop.
RPi, PWM over GPIO for spindle speed control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
GPIO.setwarnings(False) # needed because GPIO-cleanup can not be used. Setting pin to 1V | |
GPIO.setmode(GPIO.BOARD) # BCM system, replace BOARD with BCM | |
GPIO.setup(40,GPIO.OUT) # physical pin 11 will be an output | |
my_pwm=GPIO.PWM(40,100) # create a PWM object, physical pin, 100 Hz | |
my_pwm.start(0) # 50% duty cycle, 1.6V is about half of the 3.3V coming out of the Pi | |
stopit = False # varaible to exit the script | |
while(stopit != True): # while not True loop | |
dutycycle = input("Enter % Duty Cycle: 1 to QUIT ") # user input 0-100 = 0-~3.3V on the pin | |
if dutycycle == 1: | |
dutycycle = 0 # set PWM to 0% at exit | |
stopit = True #exit the loop | |
my_pwm.ChangeDutyCycle(int(dutycycle)) # change DuCy according to user input | |
print ("DuCy ") + (str(dutycycle)) # just for testing to get some feedback | |
my_pwm.stop() # not sure if this is needed. Without settin warning to False, warning ot pin beeing used still shows up. It can't harm though. | |
print ("Bye-bye") | |
# GPIO.output(40, 0) # attempt to set the pin to LOW. Did not work. GPIO-Cleanup makes it 1V | |
# GPIO.cleanup() # release your pins and clean up. Can't be used as it sets the pin to 1V | |
# exiting withou cleanup | |
# At boot the pin used for PWM will be at 1V, ~ 31% duty cicle and will trigger the PWM to volage module | |
# Solution is to "sudo nano /etc/rc.local" and add "pigs modes 21 w" before the "exit 0" | |
# note that the pin number it set in BCM, not BOARD, so my board pin 40 is addressed here as 21. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment