DHT11 Sensor 0.91 inch OLED Arduino TFT LCD | ESP32 IoT Project | All in Hindi

DHT11 Sensor 0.91 inch OLED Arduino TFT LCD | ESP32 IoT Project | All in Hindi





Code & Connection

We can change Data Pin of DHT11 according to our convenient.



#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

#define OLED_RESET    -1
Adafruit_SSD1306 display(OLED_RESET);

#define DHTPIN 4     // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);

  // Initialize the OLED display with I2C address 0x3C (for the 128x64)
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  // Initialize DHT sensor
  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature(); // Celsius

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Clear the display
  display.clearDisplay();

  // Display temperature and humidity on OLED
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.println(temperature);
  display.print("Humidity: ");
  display.println(humidity);

  display.display();
  delay(2000); // Delay to avoid flooding the display with updates
}

Comments

Popular Posts