#include // Using DHT Library #include // Using U8g2 Library #include #define DHTPIN 26 // DHT11 I/O pin connected #define DHTTYPE DHT11 // Sensor type (DHT11, DHT21, DHT22) DHT dht(DHTPIN, DHTTYPE); // define DHT Object named 'dht' with previous define pin and type // OLED display constructor U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 3, /* data=*/ 4); void setup() { Serial.begin(9600); // start serial communication with baud rate dht.begin(); // start dht sensor read u8g2.begin(); // Initialize the OLED display } void loop() { delay(1000); // wait 1 sec between each sensor read u8g2.clearBuffer(); // Clear the screen // Set font and position u8g2.setFont(u8g2_font_ncenB14_tr); u8g2.setCursor(0, 20); // Set position (x, y) // Reading DHT11 data float humidity = dht.readHumidity(); // Read humidity in % float temperature = dht.readTemperature(); // Read temperature in °C // Checking if reading data succeed if (isnan(humidity) || isnan(temperature)) { // if data read "is not a number" Serial.println("Failed to read DHT11 Sensor !"); //print on serial monitor error message u8g2.println("Failed to read DHT11 Sensor !"); return; } // Displaying data on serial monitor : u8g2.print("Humidity: "); u8g2.print(humidity); u8g2.print(" %\t"); u8g2.print("Temperature: "); u8g2.print(temperature); u8g2.println(" °C"); u8g2.sendBuffer(); // Displaying data on serial monitor : Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C"); }