Im attempting to make a sound meter with 10 Leds that light up from green [ok] to red [bad] ive got it all wired and all 10 leds light up but instead of going up slowly as the noise increases they all 10 light at once. Can any of you see anything i messed up?
void setup() {
//here is an input for sound sensor
pinMode(A0, INPUT);
//here we are setting up all pins as an outputs for LEDs
for(int z = 0; z < 10; z++){
pinMode(z, OUTPUT);
}
}
void loop() {
//here we are storing the volume value
int volume = analogRead(A0);
//max value for analog read is 1023 but it must be very very loud to reach this value
//so I lower it down in map function to 700
//mapping volume value to make it easier to turn LEDs on
volume = map(volume, 0, 700, 0, 10);
//for loop to turn on or off all LEDs
//thanks to this loop code for this project is very short
//we are going through all pins where we have LEDs and checking if the volume is
//bigger then pin number (that's why we are maping the volume)
for(int a = 0; a < 10; a++){
if(volume >= a){
//if it is bigger we can turn on the LED
digitalWrite(a, HIGH);
}else{
//if it is smaller we can turn the LED off
digitalWrite(a, LOW);
}
}
}
Im very new to the sound measuring world. I followed a tutorial for this and have ran into this issue. He set it to 700 because he said reaching 1023 required to much noise. Ive now moved them from pins 0 and 1 but i still have the same result where instead of gradually lighting up as noise increases all 10 leds light at once at any sound.
It's extremely helpful to auto format, and for the forum you should also post using code tags, like:
void setup() {
//here is an input for sound sensor
pinMode(A0, INPUT);
//here we are setting up all pins as an outputs for LEDs
for (int z = 0; z < 10; z++) {
pinMode(z, OUTPUT);
}
}
void loop() {
//here we are storing the volume value
int volume = analogRead(A0);
//max value for analog read is 1023 but it must be very very loud to reach this value
//so I lower it down in map function to 700
//mapping volume value to make it easier to turn LEDs on
volume = map(volume, 0, 700, 0, 10);
//for loop to turn on or off all LEDs
//thanks to this loop code for this project is very short
//we are going through all pins where we have LEDs and checking if the volume is
//bigger then pin number (that's why we are maping the volume)
for (int a = 0; a < 10; a++) {
if (volume >= a) {
//if it is bigger we can turn on the LED
digitalWrite(a, HIGH);
} else {
//if it is smaller we can turn the LED off
digitalWrite(a, LOW);
}
}
}
There are 3 or 4 different kinds of sound sensors. Which one do you have?
And you do need to know what kinds of raw numbers you're getting. Some sound sensors (including the one I have) are no good for quiet sounds,
Note that sound is a wave with a positive & negative peak, plus two zero-crossings. every cycle. If you have a sound sensor that reads the actual wave you'll get "random looking readings" and without some "processing" your LEDs might flicker a lot. But if you look at the readings you should be able to see the difference between loud sounds and quiet sounds.
Most analog sound sensor boards are biased at 2.5V which means silence reads about 512. The wave is centered around 512 with the positive half of the waveform going higher and the negative half going lower.
This isn't exactly what you're trying to do but I made an SPL meter and the code is here here. It finds peaks & averages which you may want to use. It also calculates dB SPL which you may not want,
So i found that my particular sound sensor only detects the presence of sound. It cannot determine the level of sound. I believe that is my issue. Thanks for the help.