#include <DHT.h>
#include <Adafruit_SSD1306.h>
#define DHT_PIN 5 // Pin connected to the DHT sensor
#define OLED_SDA 2 // Pin connected to the OLED's SDA
#define OLED_SCL 4 // Pin connected to the OLED's SCL
DHT dht(DHT_PIN, DHT22); // Change to DHT22 if using that sensor
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000); // Delay to allow OLED to initialize
display.clearDisplay();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Clear the OLED display
display.clearDisplay();
// Print temperature and humidity values on the OLED display
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" *C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.display();
// Print temperature and humidity values to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(5000); // Delay before the next reading
}