BlynkTimer timer;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
const byte soundpin = 35;
const byte led1 = 2;
const byte led2 = 4;
const byte led3 = 17;
const byte led4 = 19;
void setup() {
pinMode (soundpin, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
Serial.begin (115200);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sensorDataSend);
}
void sensorDataSend() {
int sample;
unsigned long startMillis = millis();
float peakTopeak = 0;
int db;
unsigned int signalMax = 0; // minvalue
unsigned int signalMin = 1024; //maxvalue previously 1024
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(soundpin);
if (sample < 1024) //previously i use 1024
{
if (sample > signalMax)
{
signalMax = sample; //save just max level
}
else if (sample < signalMin)
{
signalMin = sample; //save just min level
}
}
}
Serial.print("\nmin=");
Serial.print(signalMin);
Serial.print("\tmax=");
Serial.print(signalMax);
peakTopeak = signalMax - signalMin;
Serial.print("\tp2p=");
Serial.print(peakTopeak);
int store = map(peakTopeak, 0, 1023, 25, 90 ); //calibrate the db 20,900,49,90
db = store;
Serial.print("\tLoudness: ");
Serial.print (db);
Serial.print ("Db(A)\t");
Blynk.virtualWrite(V0, db);
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
digitalWrite (led4, LOW);
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
Blynk.virtualWrite(V4, 0);
if (db <= 40)
{
Serial.print("Levell: Quiet");
digitalWrite(led1, HIGH);
Blynk.virtualWrite(V1, 1);
}
else if (db > 40 && db < 55)
{
Serial.print("Level2: Moderate");
digitalWrite(led2, HIGH);
Blynk.virtualWrite(V2, 1);
}
else if (db > 55 && db < 70)
{
Serial.print("Level3: HIGH");
digitalWrite(led3, HIGH);
Blynk.virtualWrite(V3, 1);
}
else if (db >= 70)
{
Serial.print("Level4: Ultimately HIGH");
digitalWrite(led4, HIGH);
Blynk.virtualWrite(V4, 1);
}
}
void loop() {
Blynk.run();
timer.run();
}
could you help me solve this because the peak to peak= min -max. but min = 1024. and max = 0. also anyone have other idea to do a project on decibel meter?
below is the 4 leds connects to esp32 through breadboard to notify the user for certain limit of decibel level. and 3 out of 4 pin of grove sound sensor connect to measure analog signal before get the reading in db.
I once made an SPL Meter with an Arduino Uno and an analog SparkFun sound sensor.
You'd have to adapt it to the ESP32, calibrate for particular sound sensor, and possibly make other changes for your sensor. And, you'd need to add the code for the LEDs. (I send the dB values to the Serial Monitor.)
I have an issue with the sensitivity since I compare the reading with decibelX(software from phone). The above coding I shared with you is less sensitivity, when I clap, theres no big changes in db value. So i taught because of the bit is 12, is there reason I should reduce to 10bit to increase the sens of sensor? or any idea on this thing?
You should forget about the dB calculation until you get the raw values working properly. Use serial.Print() of the raw values and see how they change with the sound level. When they are satisfactory, then begin the dB calculations.
Throughout the test i increase my speaker volume from 0 to 80% and I saw a big difference happened on 80% but the signal value is not up to 4096 (12 bit is default in esp32 right). so why the value is not close to 4000 after increasing the sound. I use vcc 5v.