FluxBench.AIShared project · read-only

DHT22 Weather Station

ESP32 DevKit (WROOM-32)·Arduino C++·Updated Jul 27, 2026·by FluxBench TeamIncludes Lab + Schematic

A DHT22 temperature/humidity sensor feeding a 128x64 OLED weather readout with min/max tracking and a comfort alert LED. Click the DHT22 on the bench and drag its two sliders while the sketch runs - the OLED, the serial monitor and the red alert LED on GPIO 4 all react live. Comfort window is 18-28 °C and 30-60 %RH; step outside it and the display calls out too hot / too cold / too humid / too dry. Plain DHT + Adafruit_SSD1306 calls, so the same sketch runs on real hardware unchanged.

sketch.ino
// DHT22 Weather Station - live temperature & humidity with a comfort alert.
// Click the DHT22 on the bench and drag its two sliders (temperature and
// humidity) while the sketch runs - the OLED, serial monitor and alert LED
// all react instantly.

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

#define DHT_PIN 14
#define ALERT_PIN 4

DHT dht(DHT_PIN, DHT22);
Adafruit_SSD1306 display(128, 64, &Wire, -1);

float minT = 999;
float maxT = -999;

void setup() {
  Serial.begin(115200);
  pinMode(ALERT_PIN, OUTPUT);
  dht.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  Serial.println("Weather station ready - drag the DHT22 sliders to change the readings");
}

void loop() {
  float t = dht.readTemperature();      // Celsius
  float h = dht.readHumidity();         // % relative humidity
  float f = t * 1.8 + 32;
  if (t < minT) minT = t;
  if (t > maxT) maxT = t;

  // comfort window: 18-28 C and 30-60 %RH
  int status = 0;                       // 0 comfy, 1 hot, 2 cold, 3 humid, 4 dry
  if (t > 28) status = 1;
  else if (t < 18) status = 2;
  else if (h > 60) status = 3;
  else if (h < 30) status = 4;
  digitalWrite(ALERT_PIN, status == 0 ? LOW : HIGH);

  Serial.print("Temp ");
  Serial.print(t, 1);
  Serial.print(" C / ");
  Serial.print(f, 1);
  Serial.print(" F   Hum ");
  Serial.print(h, 0);
  Serial.print(" %   ");
  if (status == 0) Serial.println("comfy");
  else if (status == 1) Serial.println("** TOO HOT **");
  else if (status == 2) Serial.println("** TOO COLD **");
  else if (status == 3) Serial.println("** TOO HUMID **");
  else Serial.println("** TOO DRY **");

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("WEATHER STATION");
  display.setTextSize(2);
  display.setCursor(0, 14);
  display.print(t, 1);
  display.print(" C");
  display.setTextSize(1);
  display.setCursor(0, 34);
  display.print("Humidity ");
  display.print(h, 0);
  display.print(" %");
  display.setCursor(0, 44);
  display.print("Min ");
  display.print(minT, 1);
  display.print("  Max ");
  display.print(maxT, 1);
  display.setCursor(0, 54);
  if (status == 0) display.print("Status: comfy");
  else if (status == 1) display.print("ALERT: too hot");
  else if (status == 2) display.print("ALERT: too cold");
  else if (status == 3) display.print("ALERT: too humid");
  else display.print("ALERT: too dry");
  display.display();

  delay(2000);
}

Built with FluxBench.AI — the AI workbench for embedded projects.