Skillnad mellan versioner av "Grove - Sound Sensor"
Rad 33: | Rad 33: | ||
=== Raspberry Pi === | === Raspberry Pi === | ||
<syntaxhighlight lang="Python" line> | <syntaxhighlight lang="Python" line> | ||
+ | import time | ||
+ | import grovepi | ||
+ | # Connect the Grove Sound Sensor to analog port A0 | ||
+ | # SIG,NC,VCC,GND | ||
+ | sound_sensor = 0 | ||
+ | |||
+ | # Connect the Grove LED to digital port D5 | ||
+ | # SIG,NC,VCC,GND | ||
+ | led = 5 | ||
+ | |||
+ | grovepi.pinMode(sound_sensor,"INPUT") | ||
+ | grovepi.pinMode(led,"OUTPUT") | ||
+ | |||
+ | # The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v | ||
+ | threshold_value = 400 | ||
+ | |||
+ | while True: | ||
+ | try: | ||
+ | # Read the sound level | ||
+ | sensor_value = grovepi.analogRead(sound_sensor) | ||
+ | |||
+ | # If loud, illuminate LED, otherwise dim | ||
+ | if sensor_value > threshold_value: | ||
+ | grovepi.digitalWrite(led,1) | ||
+ | else: | ||
+ | grovepi.digitalWrite(led,0) | ||
+ | |||
+ | print("sensor_value = %d" %sensor_value) | ||
+ | time.sleep(.5) | ||
+ | |||
+ | except IOError: | ||
+ | print ("Error") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | == Mer information == | ||
+ | http://wiki.seeedstudio.com/Grove-Sound_Sensor/ | ||
+ | |||
+ | |||
+ | [[Category: Grove]] |
Versionen från 26 oktober 2018 kl. 21.24
Ljudsensorn kan upptäcka miljöens ljudintensitet. Modulens huvudkomponent är en enkel mikrofon. Output är ett analogt värde.
Kompatibilitet
- Arduino
- Raspberry Pi
Port
Analog
Exempelkod
Arduino
const int pinSound = A0; // pin of Sound Sensor
const int pinLed = 7; // pin of LED
int thresholdValue = 50; // the threshold to turn on or off the LED
void setup()
{
pinMode(pinLed, OUTPUT); //set the LED on Digital 12 as an OUTPUT
}
void loop()
{
int sensorValue = analogRead(pinSound); //read the sensorValue on Analog 0
if(sensorValue>thresholdValue)
digitalWrite(pinLed,HIGH);
delay(200);
digitalWrite(pinLed,LOW);
}
Raspberry Pi
import time
import grovepi
# Connect the Grove Sound Sensor to analog port A0
# SIG,NC,VCC,GND
sound_sensor = 0
# Connect the Grove LED to digital port D5
# SIG,NC,VCC,GND
led = 5
grovepi.pinMode(sound_sensor,"INPUT")
grovepi.pinMode(led,"OUTPUT")
# The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v
threshold_value = 400
while True:
try:
# Read the sound level
sensor_value = grovepi.analogRead(sound_sensor)
# If loud, illuminate LED, otherwise dim
if sensor_value > threshold_value:
grovepi.digitalWrite(led,1)
else:
grovepi.digitalWrite(led,0)
print("sensor_value = %d" %sensor_value)
time.sleep(.5)
except IOError:
print ("Error")