Hi.
I bought this sound sensor from SparkFun Sound Detector - SEN-12642 - SparkFun Electronics
I hooked up like this:
Sensor Arduino
GND -> GND
VCC -> 5V
GATE -> Digital Pin 7
Gate returns 0 or 1, just like simple push button - 0 when sound does not reach treshold and 1 if it does. Everything worked as espected, but I wanted to add LED on my board. So I attached it on digital pin 10 and GND. And i programmed of course without delay function to escape from additional problems.
Problem: Now everytime when LED turns off, sound detector somehow detects sound:
Here is my code:
// Sound sensor
const int sensorPin = 7;
int clapCount = 0;
int sensorState = 0;
int lastSensorState = 0;
unsigned long prevClap = 0;
unsigned long currentClap;
unsigned long timeBetweenClaps = 0;
// LED
const int ledPin = 10;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// LED
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
// Sound sensor
sensorState = digitalRead(sensorPin);
currentClap = millis();
if (sensorState != lastSensorState) {
if (sensorState == HIGH) { // If hands are clapped
timeBetweenClaps = currentClap - prevClap;
clapCount++;
Serial.print("CLAP: ");
Serial.println(clapCount);
Serial.println(timeBetweenClaps);
Serial.println();
prevClap = currentClap;
}
}
lastSensorState = sensorState;
}
Here is what i get in serial monitor even if there is a silence in room:
CLAP: 1
2000
CLAP: 2
2000
CLAP: 3
2000
CLAP: 4
2000
Can anyone please help? What could be the problem? Thanks.
UPDATE:
I changed voltage from 5V to 3.3 (Specs say: 3.5 and 5.5 Volts. 5 Volts is ideal) and everything works.
Could this mean there is some noise or what after the LED turns off?
Please explain this to me. I am just curious.