A desk weather station is the first OLED project that feels useful every day, and it isn't another scrolling hello. Temperature and humidity on a 128×64 SSD1306, with a humidity bar and a tiny sun or cloud icon, are enough to make your display earn its keep.
In this tutorial I use a DHT22 (AM2302) for the readings, Adafruit SSD1306 + GFX for the UI, and the Adafruit DHT library for sampling. You get Uno and ESP32 pin maps, vector icons drawn with circles and lines (you don't need bitmaps), a NaN strategy that keeps the last good values on screen, and a complete sketch you can paste into the Arduino IDE.
What you'll build
- A large temperature readout (°C) plus humidity percent on one 128×64 I2C OLED, so you can read it from across the desk
- A horizontal humidity bar that you scale 0-100%
- Sun and cloud icons that you draw with Adafruit GFX primitives, and that you can pick by simple thresholds
- Non-blocking DHT reads every ~2 s, which is the DHT22's minimum interval
- NaN and failed-read handling that keeps your last good temp and humidity
- A "sensor error" flag line, so you know when the bus or sensor isn't happy
The parts you need and how you wire them
- An Arduino Uno or Nano, or an ESP32 DevKit-style board if that's what you've got
- An I2C SSD1306 128×64 OLED
- A DHT22 module, or a bare AM2302 if you're happy to add a 4.7k-10k pull-up on data
- Jumper wires, and a breadboard if you'd like one
| Part | Arduino Uno / Nano | ESP32 (common) |
|---|---|---|
| OLED VCC / GND | 5V / GND | 3.3V / GND |
| OLED SDA / SCL | A4 / A5 | GPIO21 / GPIO22 |
| DHT22 VCC / GND | 5V / GND (or 3.3V if module allows) | 3.3V / GND |
| DHT22 DATA | D2 (changeable) | GPIO4 (changeable) |
| DHT data pull-up | Often on-module; else 4.7k to VCC | Same |
Confirm your OLED on 0x3C (or 0x3D) with an I2C scanner before you blame the DHT. It's an easy check.
For a DHT22, leave at least 2 seconds between reads. If you sample faster, you'll get garbage or NaN. If your
screen stays black, use the blank-screen checklist first.
Libraries you'll install
Install these from Library Manager:
- Adafruit GFX Library
- Adafruit SSD1306
- DHT sensor library by Adafruit, which pulls in Adafruit Unified Sensor for you
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
#define DHTPIN 2 // GPIO4 on ESP32 in the full sketch variants
#define DHTTYPE DHT22
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE);
On an ESP32, change DHTPIN to 4, or whatever GPIO you wired. Don't use
strapping pins for the sensor if you can avoid them, since they'll cause boot trouble.
How you lay it out on a 128×64 panel
Divide the screen in your head:
- Top-left ~80×40 holds your big temperature digits
- Top-right holds the weather icon (sun or cloud)
- Mid band holds the "H: 45%" text
- Bottom holds the humidity bar with its outline
Fixed pixel positions beat relative "niceness" on an OLED. You want numbers that don't jump when humidity ticks from 9% to 10%. Use monospace-ish placement, and right-pad or clear a fixed region before you redraw digits. You'll get a steadier display.
void drawTempLarge(float t) {
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 4);
display.print(t, 1);
display.setTextSize(1);
display.setCursor(90, 8);
display.print("C");
}
Your humidity bar
A filled rectangle scaled to 0-100% is easier to glance at than raw digits. Clamp your inputs, so a bad reading can't draw a bar wider than the outline.
void drawHumidityBar(float h) {
const int x = 0, y = 52, w = 128, hBar = 10;
display.drawRect(x, y, w, hBar, SSD1306_WHITE);
int fill = (int)constrain(h, 0.0f, 100.0f) * (w - 2) / 100;
if (fill > 0) {
display.fillRect(x + 1, y + 1, fill, hBar - 2, SSD1306_WHITE);
}
}
Here's an optional refinement. You can invert the fill (a black hole growing left-to-right on a white bar) for a denser look, or draw a vertical tick every 25% so "50%" is obvious without reading text.
Draw sun and cloud icons with GFX
I skip bitmaps for workshop stations. A sun's a filled circle plus eight short rays, and a cloud is three overlapping circles with a flat rectangle base. Choose the icon from humidity (or later, temperature trend) so your screen feels alive:
- If humidity is under 55%, you show the sun
- If it's 55% or more, you show the cloud
void drawSun(int cx, int cy) {
display.fillCircle(cx, cy, 6, SSD1306_WHITE);
for (int a = 0; a < 360; a += 45) {
float rad = a * 0.0174533f;
int x0 = cx + (int)(8 * cos(rad));
int y0 = cy + (int)(8 * sin(rad));
int x1 = cx + (int)(11 * cos(rad));
int y1 = cy + (int)(11 * sin(rad));
display.drawLine(x0, y0, x1, y1, SSD1306_WHITE);
}
}
void drawCloud(int cx, int cy) {
display.fillCircle(cx - 6, cy, 5, SSD1306_WHITE);
display.fillCircle(cx + 2, cy - 2, 6, SSD1306_WHITE);
display.fillCircle(cx + 8, cy + 1, 5, SSD1306_WHITE);
display.fillRect(cx - 10, cy, 20, 6, SSD1306_WHITE);
}
Place the icons around (112, 16) so they clear the big temperature. If you hate floating-point
in the ray loop, hardcode eight drawLine pairs. That's clearer for beginners, and you won't need
cmath on some AVR builds.
Want custom season icons later? You can draw them in
OLED Animation Maker, export PROGMEM bitmaps, and
swap drawBitmap in for these helpers while you leave the layout and DHT logic alone.
How you handle NaN and keep the last good values
A DHT22 fails now and then. Short wires pick up noise, people touch the data line, or you've polled too soon
after boot. Adafruit's readTemperature() and readHumidity() return
NaN on failure. Don't print those raw, or you'll get a blank or broken UI.
Keep last-good floats, update them only when both readings are valid, and flag the UI when a sample fails. That way you aren't silently stuck on stale numbers forever.
float lastTemp = 0.0f;
float lastHum = 0.0f;
bool haveGood = false;
bool lastReadOk = false;
bool sampleDht() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // Celsius
if (isnan(h) || isnan(t)) {
lastReadOk = false;
return false;
}
lastHum = h;
lastTemp = t;
haveGood = true;
lastReadOk = true;
return true;
}
On the first boot before any good sample, show -- and leave the bar empty. After the first
success, redraw with last-good even when a later poll fails, but show a tiny "!" or "ERR" so you'll
notice a disconnected sensor instead of believing yesterday's air forever.
Timing: when you read vs when you redraw
I poll the DHT every 2000-2500 ms. Redraw the OLED at the same cadence, or slightly faster if you want a soft blink
of status text only. Don't call display() at 30 Hz for a weather UI. It burns power and
buys you nothing, so I wouldn't. If your station will run from a USB bank or LiPo for days, see the companion article
on stretching battery life.
unsigned long nextSampleAt = 0;
const unsigned long SAMPLE_MS = 2000;
void loop() {
unsigned long now = millis();
if (now >= nextSampleAt) {
nextSampleAt = now + SAMPLE_MS;
sampleDht();
redrawUi();
}
}
Full sketch you can copy and paste
On an Uno or Nano the default is DHT on D2 and OLED at 0x3C. On an ESP32, set DHTPIN to
4 and power the OLED from 3.3V. You'll need Adafruit GFX, Adafruit SSD1306 and Adafruit DHT.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <math.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
// Uno/Nano: 2 · ESP32: use 4 (and 3.3V for OLED)
#define DHTPIN 2
#define DHTTYPE DHT22
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE);
float lastTemp = 0.0f;
float lastHum = 0.0f;
bool haveGood = false;
bool lastReadOk = false;
unsigned long nextSampleAt = 0;
const unsigned long SAMPLE_MS = 2200UL;
void drawSun(int cx, int cy) {
display.fillCircle(cx, cy, 6, SSD1306_WHITE);
// 8 rays without float/cos — beginner-friendly
display.drawLine(cx, cy - 11, cx, cy - 8, SSD1306_WHITE);
display.drawLine(cx, cy + 8, cx, cy + 11, SSD1306_WHITE);
display.drawLine(cx - 11, cy, cx - 8, cy, SSD1306_WHITE);
display.drawLine(cx + 8, cy, cx + 11, cy, SSD1306_WHITE);
display.drawLine(cx - 8, cy - 8, cx - 6, cy - 6, SSD1306_WHITE);
display.drawLine(cx + 6, cy - 6, cx + 8, cy - 8, SSD1306_WHITE);
display.drawLine(cx - 8, cy + 8, cx - 6, cy + 6, SSD1306_WHITE);
display.drawLine(cx + 6, cy + 6, cx + 8, cy + 8, SSD1306_WHITE);
}
void drawCloud(int cx, int cy) {
display.fillCircle(cx - 6, cy, 5, SSD1306_WHITE);
display.fillCircle(cx + 2, cy - 2, 6, SSD1306_WHITE);
display.fillCircle(cx + 8, cy + 1, 5, SSD1306_WHITE);
display.fillRect(cx - 10, cy, 20, 6, SSD1306_WHITE);
}
void drawHumidityBar(float h) {
const int x = 0, y = 52, w = 128, barH = 10;
display.drawRect(x, y, w, barH, SSD1306_WHITE);
int fill = (int)(constrain(h, 0.0f, 100.0f) * (w - 2) / 100.0f);
if (fill > 0) {
display.fillRect(x + 1, y + 1, fill, barH - 2, SSD1306_WHITE);
}
}
bool sampleDht() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
lastReadOk = false;
return false;
}
lastHum = h;
lastTemp = t;
haveGood = true;
lastReadOk = true;
return true;
}
void redrawUi() {
display.clearDisplay();
if (!haveGood) {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(8, 20);
display.print("--.- C");
display.setTextSize(1);
display.setCursor(8, 44);
display.print("Waiting for DHT22...");
display.display();
return;
}
// large temperature
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 4);
display.print(lastTemp, 1);
display.setTextSize(1);
display.setCursor(92, 6);
display.print((char)247); // degree-ish; fallback below if font lacks it
display.setCursor(100, 8);
display.print("C");
// humidity text
display.setCursor(0, 36);
display.print("H: ");
display.print(lastHum, 0);
display.print("%");
// status
if (!lastReadOk) {
display.setCursor(70, 36);
display.print("ERR");
}
// icon from humidity
if (lastHum < 55.0f) drawSun(112, 18);
else drawCloud(110, 18);
drawHumidityBar(lastHum);
display.display();
}
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
for (;;);
}
dht.begin();
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 24);
display.print("Weather boot...");
display.display();
delay(1500); // DHT22 needs warm-up after power
nextSampleAt = millis();
}
void loop() {
unsigned long now = millis();
if (now >= nextSampleAt) {
nextSampleAt = now + SAMPLE_MS;
sampleDht();
redrawUi();
}
}
The degree character depends on the font. If you see garbage beside °C, drop the (char)247
line and print a plain "C" only. It's just as clear on a monochrome panel.
The ESP32 pin variant
It's the same sketch with two defines:
#define DHTPIN 4
// Power OLED from 3.3V. Wire DHT VCC to 3.3V on most ESP32 modules.
If you enable Wi-Fi later for a "live weather" dashboard, keep DHT sampling on its timer and never
block loop() with long delay() chains. That's the same discipline you'd use in animation sketches.
Calibration and placement tips for your sensor
- Don't bury the DHT under the OLED ribbon heat from a sealed box. Vent it, or leave a window.
- Self-heating is real, so keep the sensor a few centimeters from regulators and ESP32 chips. It'll read warm otherwise.
- Outdoor boxes need dew protection. Condensation kills exposed PCBs, and you'll see your humidity hanging near 99%.
- Compare it against a known thermometer once. If your module runs 1-2°C high, you can offset it with a constant.
Extending your UI
You can add a second "page" toggled by a button: min and max today, a heat-index estimate, or a tiny sparkline of the last 60 humidity samples in a ring buffer. For multi-page OLED patterns, the menu and navigation article shows non-blocking button handling that pairs well with this station, and you'll reuse it.
If your station runs on a battery, dim the contrast, slow the sample interval to 10-30 s, and use
DISPLAYOFF between updates. I cover that step by step in the power-consumption guide linked
below, and you'll save real battery.
Troubleshooting FAQ for your build
I always get NaN or ERR after boot. Why?
Check your DATA pin number, your shared GND and your pull-up. Wait at least 2 s between reads. Swap to another GPIO, since some
Nano clones mark pins oddly. Confirm you've got a DHT22, not a DHT11, because the wrong DHTTYPE
gives you nonsense.
My OLED is black but Serial prints the temp fine
Your address may be wrong (0x3C vs 0x3D), or you've got a 5V-only module on ESP32 3.3V logic with weak
pull-ups, or there isn't enough power. Run the blank-screen checklist.
My humidity is stuck at 99% or 0%
You've got a wet or sealed enclosure, or a dying sensor. Power-cycle it after drying. A bare DHT22 without a pull-up will also sit in limbo, so add 4.7k.
My numbers flicker or tear
Clear, then draw the whole frame once, then call a single display(). Don't call
clearDisplay between temperature and humidity. Slow SAMPLE_MS down if your I2C wiring's long.
My Uno runs out of SRAM with extra widgets
Avoid String, keep your icons as vectors, and skip dual full-screen bitmaps. Read the Uno
memory limits article before you stack Wi-Fi and OLED buffers.
My SH1106 panel looks shifted
That's a controller mismatch. Use an SH1106 init or X offset. I cover the details in SH1106 vs SSD1306.
My OLED is fine until I extend the DHT wires
A metre of loose data wire adds capacitance. The DHT edge slows down and reads become NaN, while I2C on A4/A5 isn't to blame. Shorten the run, twist data with ground, and keep the 4.7k pull-up at the sensor end. Don't bundle the DHT data line with SDA and SCL.
Can I show heat index from a DHT22 alone?
You can approximate the US heat-index polynomial from temperature and humidity, but it's only meaningful in warm air. Below about 26°C the number isn't a comfort tool. Label it "HI" and hide it when it's cooler than that, or your station will look broken on a mild day.
I've got two DHT22 modules and one Uno. Which pins do I use?
You can use any free digital pin for each, with its own DHT object and its own 2-second timer. Don't
call both readTemperature() back to back with no gap. Stagger them by a few hundred
milliseconds, so your OLED redraw still happens once per completed pair.
A second page: your comfort band and an hour of samples
The main layout already shows the latest temperature, humidity, a bar and a sun or cloud. A second page answers a different question: is your room in a band people usually call comfortable, and is the last hour drifting? Comfort here's a display label, not a medical claim. A common indoor band used by building guides is roughly 20-24°C with relative humidity around 40-60%. Outside that rectangle your station can say "dry," "muggy" or "cool" without pretending to be a forecast.
| Reading | Label on the OLED | Why this bucket |
|---|---|---|
| 20-24°C and 40-60% RH | OK | Usual indoor comfort band |
| RH under 40%, any mild temperature | Dry | Static, sore throat complaints, wood movement |
| RH over 60% and at least 22°C | Muggy | The cloud icon alone doesn't say the air feels heavy |
| Under 18°C | Cool | Humidity bar stays; the word stops people misreading a low number as a sensor fault |
| NaN this cycle, last sample good | STALE | Keep last good digits; don't swap the comfort word to OK |
I'd put the word on a second page instead of crowding the 128×64 home screen. One button, wired like the
menu article, toggles page. Page 0 is the live readout, and page 1 is the hour. Sample timing
stays at 2 seconds minimum for a DHT22, but your history doesn't have to store every sample. Keep a
uint8_t humHist[30] and a matching temperature array scaled by ten
(int16_t t10[30] holds 23.4°C as 234). Every fourth successful read, about every 8
seconds, write one slot and advance a ring index. Thirty slots then cover about four minutes if you
store every read, or about an hour if you store one sample a minute. That's your choice. Pick one interval and print it in
the corner ("60 min") so your sparkline has a unit.
// call only after a good sample, and only when (goodCount % 30 == 0) for ~1 min spacing
void pushHour(float t, float h) {
t10[ring] = (int16_t)(t * 10.0f);
humHist[ring] = (uint8_t)constrain(h, 0.0f, 100.0f);
ring = (ring + 1) % 30;
if (filled < 30) filled++;
}
Draw the sparkline as 30 columns starting at x = 4. Map humidity 0-100 onto y = 10..40, or map
temperature across the min and max currently in the ring, so you can see a 0.5°C drift. Skip columns
beyond filled during the first hour. Otherwise the missing slots look like a crash to zero.
You get min and max for the page from a single pass over the ring at redraw time. On an Uno that loop's cheap
next to the 1024-byte framebuffer you already allocated.
Moving your probe to a longer cable
Desk wiring in the pin table (DHT data on D2, OLED on A4/A5, address 0x3C) fails in a
specific way once you move the sensor to a window. The DHT22 uses a one-wire-style protocol with tight edges.
A metre of ribbon adds capacitance, the pull-up can't rise fast enough, and you get runs of NaN while
the OLED keeps answering an I2C scanner. Twist the data line with ground, place the 4.7k at the sensor,
and keep SDA and SCL on their own short pair. Power the sensor from the same 5V (Uno) or 3.3V (ESP32,
data on GPIO4, OLED on GPIO21 and GPIO22) as the module you tested on the bench. A second regulator
for "cleaner" 3.3V, with a different ground path, creates an offset that you then try to calibrate out.
Calibration stays a single constant once the sensor's been powered for several minutes away from the regulator's heat. If a trusted thermometer reads 23.4°C and your DHT sits at 25.1°C in the same shade, subtract 1.7 from the displayed temperature and write that offset at the top of your sketch. Don't "fix" it by averaging in the NaN cycles. Those should keep the last good sample, and the STALE flag from the table above tells you the cable's unhappy. Humidity near 99% after the move is still condensation or a sealed box, which the placement notes already cover. The new clue is NaN that shows up only after you've lengthened the wire.
Related guides you might like
- OLED power consumption & battery life
- SSD1306 I2C getting started
- Blank screen checklist
- Gauges and progress bars
- Menus and button navigation
- GFX shapes: circle, rect, line
- Battery voltage on OLED
- Uno memory limits
Design your own weather icons
Draw sun, rain and moon as tiny sprites in the free tool, export the PROGMEM arrays, and drop them into your station layout.
Open oledanimationmaker.com →Free tools: OLED Animation Maker for your SSD1306 and SH1106 animations, Assemble Studio for your name and logo animations, the Shadow Box Lamp Generator for your 3D-printed shadow lamps, Text to STL for your 3D-printed names and keychains, and 3MF to Web to show your 3D prints on any website. New: the Image to Byte Array converter, the OLED Robot Eyes generator and the Lithophane Maker. See all tools.