Skillnad mellan versioner av "Grove - Sound Sensor"
Rad 74: | Rad 74: | ||
[[Category: Grove]] | [[Category: Grove]] | ||
+ | [[Category:Grove-Sensor]] | ||
+ | |||
[[Category:Kna-Wiki]] | [[Category:Kna-Wiki]] |
Nuvarande version från 4 februari 2021 kl. 07.30
Ljudsensorn kan mäta miljöns 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")