pwmOutput.py 524 B

1234567891011121314151617181920212223242526
  1. import RPi.GPIO as GPIO
  2. import time
  3. import sys
  4. class PWM:
  5. def __init__(self, pin):
  6. self.pin = pin
  7. GPIO.setup(self.pin, GPIO.OUT)
  8. GPIO.output(self.pin, 0)
  9. self.pwm = GPIO.PWM(self.pin, 100)
  10. self.pwm.start(0)
  11. def setDutyCycle(self, val):
  12. self.pwm.ChangeDutyCycle(val)
  13. if __name__ == '__main__':
  14. try:
  15. GPIO.setmode(GPIO.BCM)
  16. p = PWM(32)
  17. while True:
  18. p.setDutyCycle(int(time.time()*100 % 100))
  19. time.sleep(.03)
  20. except KeyboardInterrupt:
  21. GPIO.cleanup()
  22. sys.exit(0)