PinoutSearch

Temperature on an OLED (DHT22 + SSD1306)

Show live temperature and humidity on an OLED screen — combine a sensor and a display into a real gadget.

Intermediate30 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

temp_oled.ino
// Show temperature + humidity from a DHT22 on an SSD1306 OLED.
// Install: Adafruit SSD1306, Adafruit GFX, DHT sensor library.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

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

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

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0, 0);   display.print(t, 1); display.println(" C");
  display.setCursor(0, 34);  display.print(h, 1); display.println(" %");
  display.display();
  delay(2000);
}

Steps

  1. Wire the DHT22 (VDD/DATA/GND, with a 10kΩ pull-up on DATA) — DATA to D2.
  2. Wire the OLED over I²C: SDA to A4, SCL to A5, plus power.
  3. Install the Adafruit SSD1306 + GFX and DHT libraries.
  4. Upload — the screen shows live temperature and humidity.