Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the sensor
VCC to 3.3, GND, data to PB5 with a 10 kilo-ohm pull-up to 3.3 V.
Wire the display
SDA to PB7, SCL to PB6, power at 3.3 V.
Install the libraries
DHT, Unified Sensor, SSD1306 and GFX.
Upload the sketch
Humidity and temperature appear.
Breathe gently near the sensor
The bar rises.
Wait a minute
The bar settles back.
How do you build a blue Pill humidity gauge?
A Blue Pill reads a DHT22 and shows humidity and temperature on an OLED, with a bar and a mark for the 40 to 60 percent comfort band. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DHT22 Sensor, Resistor 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.
- Level
- Intermediate
- Time
- About 140 min
- Parts
- 6
Parts list
- Development BoardsBuy
- InterfacesBuy
- SensorsBuyDHT22 Sensor × 1
- PassivesBuyResistor 10 kΩ × 1
- DisplaysBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| DHT22 VCC | Blue Pill 3.3 pin | Power at 3.3 V |
| DHT22 GND | Blue Pill GND | Ground |
| DHT22 data | Blue Pill PB5 | One-wire style data |
| 10 kilo-ohm resistor | Between DHT22 data and 3.3 V | Pull-up |
| OLED VCC and GND | Blue Pill 3.3 pin and GND | Power at 3.3 V |
| OLED SDA and SCL | Blue Pill PB7 and PB6 | I2C data and clock |
Code
The DHT22 measures humidity with a small capacitor whose value changes with moisture, and temperature with a thermistor, then sends both as a number over one data wire. The data line is held high by a 10 kilo-ohm pull-up and the sensor pulls it low in short and long pulses that the library times to decode the bits. The sensor is slow, so it can be read only about every two seconds, and a read can fail, which the sketch checks for and reports. The bar is one pixel per percent, with two small marks at 40 and 60 percent, the range most people find comfortable indoors. Readings are indicative only: the sensor reads a few percent off, a breath or a kettle moves it quickly, and it takes a while to settle after a big change. The sensor runs happily at 3.3 V here.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
DHT dht(PB5, DHT22);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
unsigned long lastRead = 0;
void setup() {
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
dht.begin();
}
void loop() {
if (millis() - lastRead < 2000) return; // the sensor needs about 2 s between reads
lastRead = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("Humidity gauge");
if (isnan(h) || isnan(t)) {
oled.setCursor(0, 20); oled.print("Sensor read failed");
oled.display();
return;
}
oled.setTextSize(3);
oled.setCursor(0, 12); oled.print(h, 0); oled.print("%");
oled.setTextSize(1);
oled.setCursor(80, 20); oled.print(t, 1); oled.print(" C");
oled.drawRect(0, 46, 102, 12, SSD1306_WHITE); // bar frame, 1 pixel per percent
oled.fillRect(1, 47, (int)constrain(h, 0, 100), 10, SSD1306_WHITE);
oled.drawLine(41, 42, 41, 44, SSD1306_WHITE); // 40 percent mark
oled.drawLine(61, 42, 61, 44, SSD1306_WHITE); // 60 percent mark
oled.setCursor(0, 58); oled.print("Comfort band 40-60");
oled.display();
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Room air | Plausible humidity and temperature |
| Breath near the sensor | Humidity rises briefly |
| Unplug the data wire | Sensor read failed message |
| Bar and marks | Bar passes the 40 and 60 marks as humidity changes |
| Update rate | Every two seconds |
If it does not work
Sensor read failed
Check the data pin, the pull-up and 3.3 V power.
No display
Check the OLED address and I2C wires.
Values jump
Keep the sensor away from heat and air flow.
Compile error on DHT
Install the Adafruit Unified Sensor library as well.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill humidity gauge?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DHT22 Sensor, Resistor 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the blue Pill humidity gauge take to build?
About 140 minutes for a intermediate maker, in 7 steps.
How do I know the blue Pill humidity gauge works?
Room air: Plausible humidity and temperature. Breath near the sensor: Humidity rises briefly. Unplug the data wire: Sensor read failed message.
Sensor read failed: what should I check?
Check the data pin, the pull-up and 3.3 V power.
No display: what should I check?
Check the OLED address and I2C wires.
Values jump: what should I check?
Keep the sensor away from heat and air flow.
Compile error on DHT: what should I check?
Install the Adafruit Unified Sensor library as well.
