Created
November 29, 2017 15:41
-
-
Save iotguider/3567e77e23fec85c1a73dce39ff09288 to your computer and use it in GitHub Desktop.
Code for Small Microphone Sound Detection Module KY-038 in Raspberry Pi
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/python | |
import RPi.GPIO as GPIO | |
import time | |
#GPIO SETUP | |
sound = 17 | |
led = 27 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(sound, GPIO.IN) | |
GPIO.setup(led,GPIO.OUT) | |
def callback(sound): | |
if GPIO.input(sound): | |
print "Sound Detected!" | |
GPIO.output(led,HIGH) | |
else: | |
print "Sound Detected!" | |
GPIO.output(led,LOW) | |
GPIO.add_event_detect(sound, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW | |
GPIO.add_event_callback(sound, callback) # assign function to GPIO PIN, Run function on change | |
# infinite loop | |
while True: | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment