Projects / Blue Pill humidity gauge

Blue Pill humidity gauge

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V.

  2. STM32

    Wire the sensor

    VCC to 3.3, GND, data to PB5 with a 10 kilo-ohm pull-up to 3.3 V.

  3. STM32

    Wire the display

    SDA to PB7, SCL to PB6, power at 3.3 V.

  4. STM3214.5 mASSD1306

    Install the libraries

    DHT, Unified Sensor, SSD1306 and GFX.

  5. STM32

    Upload the sketch

    Humidity and temperature appear.

  6. STM32

    Breathe gently near the sensor

    The bar rises.

  7. STM32

    Wait a minute

    The bar settles back.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBoard GNDGround
ST-Link SWDIOBoard DIODebug data
ST-Link SWCLKBoard DCLKDebug clock
ST-Link 3.3VBoard 3.3 pinPower from the programmer (do not also plug in USB)
DHT22 VCCBlue Pill 3.3 pinPower at 3.3 V
DHT22 GNDBlue Pill GNDGround
DHT22 dataBlue Pill PB5One-wire style data
10 kilo-ohm resistorBetween DHT22 data and 3.3 VPull-up
OLED VCC and GNDBlue Pill 3.3 pin and GNDPower at 3.3 V
OLED SDA and SCLBlue Pill PB7 and PB6I2C 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.

CheckExpected
Room airPlausible humidity and temperature
Breath near the sensorHumidity rises briefly
Unplug the data wireSensor read failed message
Bar and marksBar passes the 40 and 60 marks as humidity changes
Update rateEvery 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 →

Ask Lark AI

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.