Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. ########################################################################
  3. # Filename : Doorbell.py
  4. # Description : Controlling an buzzer by button.
  5. # Author : freenove
  6. # modification: 2018/08/02
  7. ########################################################################
  8. import RPi.GPIO as GPIO
  9.  
  10. buzzerPin = 11 # define the buzzerPin
  11. buttonPin = 12 # define the buttonPin
  12.  
  13. def setup():
  14. print ('Program is starting...')
  15. GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
  16. GPIO.setup(buzzerPin, GPIO.OUT) # Set buzzerPin's mode is output
  17. GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set buttonPin's mode is input, and pull up to high level(3.3V)
  18.  
  19. def loop():
  20. while True:
  21. if GPIO.input(buttonPin)==GPIO.LOW:
  22. GPIO.output(buzzerPin,GPIO.HIGH)
  23. print ('buzzer on ...')
  24. else :
  25. GPIO.output(buzzerPin,GPIO.LOW)
  26. print ('buzzer off ...')
  27.  
  28. def destroy():
  29. GPIO.output(buzzerPin, GPIO.LOW) # buzzer off
  30. GPIO.cleanup() # Release resource
  31.  
  32. if __name__ == '__main__': # Program start from here
  33. setup()
  34. try:
  35. loop()
  36. except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
  37. destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement