#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
dht.begin();
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
oled.println("Failed");
} else {
oled.print("Temp: ");
oled.print(tempC);
oled.println(" *C");
oled.print("Humi: ");
oled.print(humi);
oled.println(" %");
}
oled.display();
delay(5000);
}