// Define the analog pins for the sensors
#define MQ135_PIN 34 // Analog pin for MQ135 sensor (potentiometer simulating the gas sensor)
#define KY038_PIN 35 // Analog pin for KY-038 sensor (potentiometer simulating the sound sensor)
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
// Configure the analog pins (ESP32 pins are configured by default as input)
pinMode(MQ135_PIN, INPUT);
pinMode(KY038_PIN, INPUT);
Serial.println("ESP32 with MQ135 and KY-038 (Simulated using Potentiometers)");
}
void loop() {
// Read analog values from both sensors (simulated potentiometers)
int gasValue = analogRead(MQ135_PIN); // Read raw gas sensor value (ADC from potentiometer)
int soundValue = analogRead(KY038_PIN); // Read raw sound sensor value (ADC from potentiometer)
// Map the gas sensor value to a gas concentration (this requires calibration)
float gasConcentration = mapGasReading(gasValue);
// Convert sound sensor value to decibels (you may need to calibrate)
float soundLevel = mapSoundReading(soundValue);
// Print the sensor values for debugging
Serial.print("Gas Concentration (PPM): ");
Serial.println(gasConcentration);
Serial.print("Sound Level (dB): ");
Serial.println(soundLevel);
// Add a delay for stability (adjust as necessary)
delay(1000); // 1 second delay
}
// Function to convert gas sensor value to concentration (this is a placeholder)
float mapGasReading(int gasValue) {
// Example: Simple linear conversion (you'll need to replace with actual formula or calibration)
float concentration = gasValue * (500.0 / 4095.0); // Example for a linear scale
return concentration;
}
// Function to convert sound sensor value to decibels (adjust calibration as needed)
float mapSoundReading(int soundValue) {
// Example: Linear mapping (calibrate for accurate conversion)
float soundLevel = soundValue * (100.0 / 4095.0); // Convert to decibels (dB)
return soundLevel;
}