IntermediateIoT about 75 min

ESP32 Weather Display

Read temperature and humidity with a DHT22 and show it on an OLED. Ready to extend to Wi-Fi dashboards.

Steps

  1. Wire the DHT22

    VCC to 3.3V, GND to GND, DATA to GPIO 4.

  2. Wire the OLED

    VCC to 3.3V, GND to GND, SDA to GPIO 21, SCL to GPIO 22.

  3. Install libraries

    Install DHT sensor library, Adafruit GFX and Adafruit SSD1306 from the Library Manager and add ESP32 board support.

  4. Upload

    Select 'ESP32 Dev Module' and upload the sketch.

Code

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

DHT dht(4, DHT22);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

void setup() {
  dht.begin();
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
}

void loop() {
  float t = dht.readTemperature(), h = dht.readHumidity();
  oled.clearDisplay(); oled.setTextSize(2);
  oled.setCursor(0, 8);  oled.print(t, 1); oled.print(" C");
  oled.setCursor(0, 40); oled.print(h, 0); oled.print(" %");
  oled.display();
  delay(2000);
}