#include <Wire.h>
#include <Arduino.h>
#include <ledc.h> // Include for PWM control
#define PPM_PIN 4
// RGB LED pin definitions
#define RED_PIN 2
#define GREEN_PIN 12
#define BLUE_PIN 13
// Define PWM channels for the RGB pins
#define RED_CHANNEL 0
#define GREEN_CHANNEL 1
#define BLUE_CHANNEL 2
void setup() {
Wire.begin(23, 22); // Initialize I2C communication
Serial.begin(115200); // Start serial communication
Serial.println("Hello, ESP32!");
// Initialize LEDC PWM channels
ledcAttachChannel(RED_PIN, 5000, 8, RED_CHANNEL); // 5kHz frequency, 8-bit resolution for Red
ledcAttachChannel(GREEN_PIN, 5000, 8, GREEN_CHANNEL); // 5kHz frequency, 8-bit resolution for Green
ledcAttachChannel(BLUE_PIN, 5000, 8, BLUE_CHANNEL); // 5kHz frequency, 8-bit resolution for Blue
}
void loop() {
// Read value from the analog converter (PPM sensor)
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 to the Serial Monitor
Serial.print("PPM: ");
Serial.println(mappedppmValue);
// Control RGB LED based on the PPM value
if (mappedppmValue < 200) { // Low pollution
ledcWriteChannel(RED_CHANNEL, 0);
ledcWriteChannel(GREEN_CHANNEL, 255); // Green
ledcWriteChannel(BLUE_CHANNEL, 0);
} else if (mappedppmValue < 400) { // Moderate pollution
ledcWriteChannel(RED_CHANNEL, 255); // Yellow
ledcWriteChannel(GREEN_CHANNEL, 255);
ledcWriteChannel(BLUE_CHANNEL, 0);
} else { // High pollution
ledcWriteChannel(RED_CHANNEL, 255); // Red
ledcWriteChannel(GREEN_CHANNEL, 0);
ledcWriteChannel(BLUE_CHANNEL, 0);
}
delay(100); // Wait for 100 milliseconds
}