#include <DHT.h>
// #define DHTPIN 14 //ESP32
#define DHTTYPE DHT22 //ESP32
// DHT11 setup
#define DHTPIN 14 //WOKWI
// #define DHTTYPE DHT11 //Arduino
DHT dht(DHTPIN, DHTTYPE);
// RGB LED setup
#define RED_PIN 4 //WOKWI
#define BLUE_PIN 17 //WOKWI
#define GREEN_PIN 16 //WOKWI
// #define RED_PIN A5 //ESP32
// #define GREEN_PIN A1 //ESP32
// #define BLUE_PIN A0 //ESP32
// Button setup
#define BUTTON_PIN 5 //WOKWI
// #define BUTTON_PIN 14 //ESP32
void setup() {
// Initialize DHT sensor
dht.begin();
// Set up RGB LED pins as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
// Set up button pin as input with internal pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize serial communication for debugging purposes
Serial.begin(9600);
}
void loop() {
// Read the button state
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW; // LOW means pressed
if (buttonPressed) {
// If the button is pressed, show humidity and set RGB to red
float humidity = dht.readHumidity();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
setRGB(255, 0, 0); // Red color
} else {
// If the button is not pressed, show temperature and set RGB to green
float temperature = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
setRGB(0, 255, 0); // Green color
}
// Delay between each loop for data stability
delay(2000);
}
// Helper function to set RGB LED color
void setRGB(int redValue, int greenValue, int blueValue) {
analogWrite(RED_PIN, redValue);
analogWrite(GREEN_PIN, greenValue);
analogWrite(BLUE_PIN, blueValue);
}