Projects / Blue Pill one-wire thermometer

Blue Pill one-wire thermometer

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. STM32DS18B2026.4°C

    Wire the probe

    Red to 3.3, black to GND, yellow to PB5.

  3. STM32

    Add the pull-up

    4.7 kilo-ohm from PB5 to 3.3 V.

  4. STM32

    Wire the display

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

  5. STM3215.3 mASSD1306

    Install the libraries

    OneWire, DallasTemperature, SSD1306 and GFX.

  6. STM32

    Upload the sketch

    A temperature appears.

  7. STM32DS18B2026.5°C

    Put the probe in cold and then warm water

    Watch the value and the minimum and maximum.

In short

How do you build a blue Pill one-wire thermometer?

A Blue Pill reads a DS18B20 probe over one wire and shows the temperature with the lowest and highest values on an OLED. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DS18B20 waterproof probe, Resistor 4.7 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)
DS18B20 redBlue Pill 3.3 pinPower at 3.3 V
DS18B20 blackBlue Pill GNDGround
DS18B20 yellowBlue Pill PB5One-wire data
4.7 kilo-ohm resistorBetween PB5 and 3.3 VPull-up for the data line
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 DS18B20 is a digital temperature probe. It sends its reading as a number over a single data wire, and many probes can share that wire because each has its own address. The data line must be pulled up to 3.3 V with a 4.7 kilo-ohm resistor, because the probe only pulls the line low and lets the resistor raise it again. At 12-bit resolution a reading takes up to 750 milliseconds, so the sketch asks for a measurement, goes on running, and reads the result 800 milliseconds later rather than waiting. A value of minus 127 means the probe did not answer, and the sketch shows dashes in that case. The display keeps the lowest and highest values since power-up, which is handy for a freezer, a room overnight or a cup of tea going cold. Readings are indicative only: a probe in air follows the temperature slowly, and the metal tip must touch whatever you are measuring. Do not use it for food safety or medical decisions.

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 <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

OneWire bus(PB5);
DallasTemperature probe(&bus);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

float minT = 1000, maxT = -1000, nowT = NAN;
unsigned long requestedAt = 0;
bool waiting = false;

void setup() {
  Wire.begin();                                  // PB7 SDA, PB6 SCL
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  probe.begin();
  probe.setWaitForConversion(false);             // do not block while the probe measures
  probe.setResolution(12);                       // 12 bits takes up to 750 ms
}

void loop() {
  if (!waiting) {
    probe.requestTemperatures();
    requestedAt = millis();
    waiting = true;
  } else if (millis() - requestedAt >= 800) {
    float t = probe.getTempCByIndex(0);
    waiting = false;
    if (t > -100) {                              // -127 means the probe was not found
      nowT = t;
      if (t < minT) minT = t;
      if (t > maxT) maxT = t;
    } else {
      nowT = NAN;
    }
    oled.clearDisplay();
    oled.setTextColor(SSD1306_WHITE);
    oled.setTextSize(1);
    oled.setCursor(0, 0); oled.print("DS18B20 thermometer");
    oled.setTextSize(3);
    oled.setCursor(0, 14);
    if (isnan(nowT)) oled.print("---"); else { oled.print(nowT, 1); }
    oled.setTextSize(1);
    oled.setCursor(0, 46); oled.print("Min "); oled.print(minT < 999 ? minT : 0.0, 1);
    oled.setCursor(0, 56); oled.print("Max "); oled.print(maxT > -999 ? maxT : 0.0, 1);
    oled.display();
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Probe in airRoom temperature that settles slowly
Probe in cold waterValue falls
Probe in warm waterValue rises
Min and maxTrack the lowest and highest values
Unplug the probeDashes appear

If it does not work

Dashes only

Check the yellow wire on PB5 and the 4.7 kilo-ohm pull-up.

Reads 85 degrees

The probe has just powered up, wait for the next reading.

Reads minus 127

The probe is not found, check wiring and power.

Blank screen

Check the OLED address and I2C wires.

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 one-wire thermometer?

STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), DS18B20 waterproof probe, Resistor 4.7 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points.

How long does the blue Pill one-wire thermometer take to build?

About 140 minutes for a intermediate maker, in 7 steps.

How do I know the blue Pill one-wire thermometer works?

Probe in air: Room temperature that settles slowly. Probe in cold water: Value falls. Probe in warm water: Value rises.

Dashes only: what should I check?

Check the yellow wire on PB5 and the 4.7 kilo-ohm pull-up.

Reads 85 degrees: what should I check?

The probe has just powered up, wait for the next reading.

Reads minus 127: what should I check?

The probe is not found, check wiring and power.

Blank screen: what should I check?

Check the OLED address and I2C wires.