Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iotguider/3567e77e23fec85c1a73dce39ff09288 to your computer and use it in GitHub Desktop.
Save iotguider/3567e77e23fec85c1a73dce39ff09288 to your computer and use it in GitHub Desktop.
Code for Small Microphone Sound Detection Module KY-038 in Raspberry Pi
#!/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