#include "DHT.h"
// Define the pin numbers
const int buttonPin1 = 27; // Green Pushbutton connected to GPIO 27
const int buttonPin2 = 25; // Blue Pushbutton connected to GPIO 25
const int ledPin1 = 23; // Red LED connected to GPIO 23
const int ledPin2 = 19; // Yellow LED connected to GPIO 19
const int DHTPin = 15; // DHT22 data pin connected to GPIO 15
// DHT sensor type
#define DHTTYPE DHT22
// Create a DHT object
DHT dht(DHTPin, DHTTYPE);
// Variables to hold the state of the buttons
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {
// Initialize the LED pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize the pushbutton pins as inputs
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// Initialize the DHT sensor
dht.begin();
// Start the serial communication for debugging
Serial.begin(115200);
}
void loop() {
// Read the state of the pushbuttons
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// Check if the green pushbutton is pressed
if (buttonState1 == LOW) {
// Turn the red LED on
digitalWrite(ledPin1, LOW);
Serial.println("Green button pressed, Red LED ON");
} else {
// Turn the red LED off
digitalWrite(ledPin1, HIGH);
Serial.println("Green button not pressed, Red LED OFF");
}
// Check if the blue pushbutton is pressed
if (buttonState2 == LOW) {
// Turn the yellow LED on
digitalWrite(ledPin2, LOW);
Serial.println("Blue button pressed, Yellow LED ON");
} else {
// Turn the yellow LED off
digitalWrite(ledPin2, HIGH);
Serial.println("Blue button not pressed, Yellow LED OFF");
}
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity values to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Add a small delay to debounce the buttons and give time between sensor readings
delay(1000); // Increase delay to 5 seconds
}