#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL26V4fGv5q"
#define BLYNK_TEMPLATE_NAME "Test"
#define BLYNK_AUTH_TOKEN "XEHxNF_Ur1Nt2p7wB5B20dNI1ZUwj34P"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
#define AO 34
#define BUZZER_PIN 19
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, 4);
unsigned int output;
int Decibels;
char auth[] = "XEHxNF_Ur1Nt2p7wB5B20dNI1ZUwj34P"; // Update with your Blynk auth token
char ssid[] = "Wokwi-GUEST"; // Update with your WiFi SSID
char pass[] = ""; // Update with your WiFi password
BLYNK_READ(V0) {
Blynk.virtualWrite(V0, Decibels);
}
void setup() {
Serial.begin(115200);
pinMode(AO, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
//pinMode(micpin, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
float mic = analogRead(A0);
float PeakToPeak = voltageToDB(mic);
Decibels = map(PeakToPeak, 50, 500, 49.5, 90);
display.setTextSize(2);
display.setCursor(0, 10);
display.print(Decibels);
display.setTextSize(2);
display.setCursor(40, 10);
display.print("db");
display.display();
if (Decibels <= 50) {
display.setTextSize(2);
display.setCursor(0, 30);
display.print("LOW");
display.display();
} else if (Decibels > 50 && Decibels < 75) {
display.setTextSize(2);
display.setCursor(0, 30);
display.print("Moderate");
display.display();
} else if (Decibels >= 75) {
display.setTextSize(2);
display.setCursor(0, 30);
display.print("HIGH");
display.display();
tone(BUZZER_PIN, 1000); // 1000Hz tone
delay(1000); // Buzz for 1 second
noTone(BUZZER_PIN); // Stop buzzing
delay(1000); // Wait for 1 second
}
delay(1000);
display.clearDisplay();
}
// Example mapping function
float voltageToDB(float voltage) {
// You need to know the calibration factor and reference voltage for this conversion
float sensitivity = 30; // Sensitivity of the microphone (you should adjust this)
float referenceVoltage = 5.0; // Reference voltage used
// Calculate the voltage ratio
float voltageRatio = voltage / referenceVoltage;
// Convert to decibels (assuming a linear relationship between voltage and decibels)
float dB = 20 * log10(voltageRatio) + sensitivity;
return dB;
}
// Example usage: