Created
March 31, 2017 20:23
-
-
Save actuino/d88061db0ec118d8de2b6f9d0d84d1d6 to your computer and use it in GitHub Desktop.
Raspberry Pi Zero Active Buzzer Test
This file contains hidden or 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
#!/usr/bin/env python | |
import RPi.GPIO as GPIO | |
import time | |
Buzzer = 40 # La pin Buzzer (N° de broche du Raspberry, pas le N° GPIO du proc) | |
def setup(pin): | |
global BuzzerPin | |
BuzzerPin = pin | |
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location | |
GPIO.setup(BuzzerPin, GPIO.OUT) | |
GPIO.output(BuzzerPin, GPIO.HIGH) | |
def on(): | |
GPIO.output(BuzzerPin, GPIO.HIGH) | |
def off(): | |
GPIO.output(BuzzerPin, GPIO.LOW) | |
def beep(x): | |
on() | |
time.sleep(x) | |
off() | |
def loop(): | |
while True: | |
beep(0.03) | |
time.sleep(0.05) | |
beep(0.03) | |
time.sleep(1) | |
def destroy(): | |
GPIO.output(BuzzerPin, GPIO.HIGH) | |
GPIO.cleanup() # Release resource | |
if __name__ == '__main__': # Program start from here | |
setup(Buzzer) | |
try: | |
loop() | |
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. | |
destroy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment