import
math
import
sys
import
time
from
grove.adc
import
ADC
class
GroveLoudnessSensor:
def
__init__(self, channel):
self.channel = channel
self.adc = ADC()
@property
def
value(self):
return
self.adc.read(self.channel)
Grove = GroveLoudnessSensor
def
main():
if
len(sys.argv) < 2:
print('Usage:
{}
adc_channel'.format(sys.argv[0]))
sys.exit(1)
sensor = GroveLoudnessSensor(int(sys.argv[1]))
print('Detecting
loud...')
while
True:
value = sensor.value
if
value > 10:
print("Loud value {}, Loud
Detected.".format(value))
time.sleep(.5)
if
__name__ == '__main__':
main()
done
Success
If everything goes well, you will be able to see the following result:
pi@raspberrypi:~/grove.py/grove
Detecting loud...
Loud value 15, Loud Detected.
Loud value 11, Loud Detected.
Loud value 250, Loud Detected.
Loud value 429, Loud Detected.
Loud value 203, Loud Detected.
Loud value 16, Loud Detected.
Loud value 11, Loud Detected.
^CTraceback (most recent call last):
File "grove_loudness_sensor.py", line 68, in <module>
main()
File "grove_loudness_sensor.py", line 65, in main
time.sleep(.5)
KeyboardInterrupt
You can use this sensor to detect the loudness. Press
edit
Notice
You may have noticed that for the analog port, the silkscreen pin number is something like A1, A0,
however in the command we use parameter 0 and 1, just the same as digital port. So please make sure
you plug the module into the correct port, otherwise there may be pin conflicts.
Play With Raspberry Pi(with GrovePi_Plus)
Hardware
Step 1. Prepare the below stuffs:
Raspberry pi
GrovePi_Plus
Get One Now
Get One Now
Step 2. Plug the GrovePi_Plus into Raspberry.
Step 3. Connect Grove-Loudness Sensor to A0 port of GrovePi_Plus.
Step 4. Connect the Raspberry to PC through USB cable.
$
python grove_loudness_sensor.py
0
Ctrl
+
C
to quit.
Grove-Loudness Sensor
Get One Now
Need help?
Do you have a question about the Grove Loudness Sensor and is the answer not in the manual?