#include <Wire.h>
#include <Arduino.h>
#define PPM_PIN 4
// RGB LED pin definitions
#define RED_PIN 2
#define GREEN_PIN 12
#define BLUE_PIN 13
void setup() {
Wire.begin(23, 22); // Initialize I2C
Serial.begin(115200); // Start serial communication
Serial.println("Hello, ESP32!");
// Initialize RGB LED pins
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
// Read value from the analog converter
int16_t ppmValue = analogRead(PPM_PIN);
// Map the correct value for ADC (max voltage to be 3.3V)
int mappedppmValue = (ppmValue / 4.095);
// Print the mapped PPM value
Serial.print("PPM: ");
Serial.println(mappedppmValue);
// Control RGB LED based on the PPM value
if (mappedppmValue < 200) { // Low pollution
analogWrite(RED_PIN, 0);
analogWrite(GREEN_PIN, 255); // Green
analogWrite(BLUE_PIN, 0);
} else if (mappedppmValue < 400) { // Moderate pollution
analogWrite(RED_PIN, 255); // Yellow
analogWrite(GREEN_PIN, 255);
analogWrite(BLUE_PIN, 0);
} else { // High pollution
analogWrite(RED_PIN, 255); // Red
analogWrite(GREEN_PIN, 0);
analogWrite(BLUE_PIN, 0);
}
delay(100); // Wait for 100 milliseconds
}