Advertisement
Guest User

sound script

a guest
Jul 16th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import time
  2. import grovepi
  3.  
  4. # Connect the Grove Sound Sensor to analog port A1
  5. # SIG,NC,VCC,GND
  6. sound_sensor = 1
  7.  
  8. # Connect the Grove LED to digital port D5
  9. # SIG,NC,VCC,GND
  10. led = 5
  11.  
  12. grovepi.pinMode(sound_sensor,"INPUT")
  13. grovepi.pinMode(led,"OUTPUT")
  14.  
  15. # The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v
  16. threshold_value = 400
  17.  
  18. while True:
  19. try:
  20. # Read the sound level
  21. sensor_value = grovepi.analogRead(sound_sensor)
  22.  
  23. # If loud, illuminate LED, otherwise dim
  24. if sensor_value > threshold_value:
  25. grovepi.digitalWrite(led,1)
  26. else:
  27. grovepi.digitalWrite(led,0)
  28.  
  29. print("sensor_value = %d" %sensor_value)
  30. time.sleep(1)
  31.  
  32. except IOError:
  33. print ("Error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement