SensorsDisplay

ESP32 digital thermometer with a DS18B20 and OLED display

Difficulty: easy · Time: ~1 hour · Cost: ~$5–8

This thermometer shows the current temperature, the minimum/maximum since power-up and a chart of roughly the last 10 minutes on a 0.96" OLED. The DS18B20 is a great choice: it is accurate (±0.5°C), comes in waterproof probe form and several of them can share a single data wire.

Parts

Key specifications

−55…+125 °Crange
±0.5 °Cfrom −10 to +85 °C
9–12 bit0.5 → 0.0625 °C
3.0–5.5 Vsupply
≈750 ms12-bit conversion

The DS18B20 uses the 1-Wire protocol: a single data line (DQ) plus power and ground. Every chip has a unique 64-bit ID, so several sensors on one GPIO can still be told apart. The data line is open-drain, so it needs a pull-up resistor to return to the high level; without it the library will always report −127 °C.

Wiring

ESP32 DevKit3V3GNDGPIO22 (SCL)GPIO21 (SDA)GPIO4OLED SSD1306 128×64 (I2C)VCCGNDSCLSDAI2C address 0x3CDS18B20 (TO-92 / probe)VDDDQGND4.7 kΩ4.7 kΩ pull-up
Figure 1: Circuit diagram. A solid dot is a junction; crossing lines without a dot are not connected. The 4.7 kΩ resistor sits between 3V3 and the DQ pin.
DevicePinESP32
OLED SSD1306VCC3V3
GNDGND
SCLGPIO22
SDAGPIO21
DS18B20VDD3V3
DQGPIO4 (and through 4.7 kΩ to 3V3)
GNDGND
Check the pin order: the pin order on OLED modules is not standardised (GND-VCC-SCL-SDA or VCC-GND-SCL-SDA), so read the silkscreen. Swapping VCC and GND can destroy the display. On waterproof probes red = VDD, black = GND, yellow/white = DQ is typical, but verify against the seller's documentation.

DS18B20 probes are often powered from 5V, but here we use 3.3V to match the ESP32 and OLED logic levels; the chip works fine from 3.0V.

Libraries (Arduino IDE)

What you should see

DS18B20 (C)27.6min 26.3 max 28.1128×64 pxSSD1306
Figure 2: Simulated OLED output when running the code below: title line, large temperature, min/max and a ~10-minute chart at the bottom.

Source code

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int     PIN_SDA   = 21;
const int     PIN_SCL   = 22;
const int     PIN_DQ    = 4;      // DS18B20 data pin (with a 4.7k pull-up)
const uint8_t OLED_ADDR = 0x3C;   // some modules use 0x3D

Adafruit_SSD1306  oled(128, 64, &Wire, -1);
OneWire           oneWire(PIN_DQ);
DallasTemperature ds(&oneWire);

const int H = 128;                // history points = display width
float hist[H];
int   head = 0, n = 0;
float tNow = NAN, tMin = 1000, tMax = -1000;
uint8_t  tick = 0;
uint32_t lastMs = 0;

void pushHist(float v) {
  hist[head] = v;
  head = (head + 1) % H;
  if (n < H) n++;
}

// Draws a 128×16 px line chart at the bottom of the screen
void drawGraph() {
  if (n < 2) return;
  float lo = 1e9, hi = -1e9;
  for (int i = 0; i < n; i++) {
    float v = hist[(head - n + i + H) % H];
    if (v < lo) lo = v;
    if (v > hi) hi = v;
  }
  if (hi - lo < 1.0) { float mid = (hi + lo) / 2; lo = mid - 0.5; hi = mid + 0.5; }
  int px = -1, py = 0;
  for (int i = 0; i < n; i++) {
    float v = hist[(head - n + i + H) % H];
    int x = (H - n) + i;                                  // right-aligned: newest point at the right edge
    int y = 63 - (int)((v - lo) / (hi - lo) * 15);
    if (px >= 0) oled.drawLine(px, py, x, y, SSD1306_WHITE);
    px = x; py = y;
  }
}

void draw() {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print("DS18B20  (C)");
  oled.setTextSize(3);
  oled.setCursor(0, 14);
  if (isnan(tNow)) oled.print("--.-"); else oled.printf("%.1f", tNow);
  oled.setTextSize(1);
  oled.setCursor(0, 40);
  if (tMin <= tMax) oled.printf("min %.1f  max %.1f", tMin, tMax);
  drawGraph();
  oled.display();
}

void setup() {
  Serial.begin(115200);
  Wire.begin(PIN_SDA, PIN_SCL);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
    Serial.println("OLED not found, check wiring and I2C address!");
    for (;;) delay(1000);
  }
  ds.begin();
  ds.setResolution(12);              // 0.0625 °C, takes ~750 ms
  ds.setWaitForConversion(false);    // do not block the CPU while converting
  ds.requestTemperatures();
  draw();
}

void loop() {
  if (millis() - lastMs < 1000) return;
  lastMs = millis();

  float t = ds.getTempCByIndex(0);   // result of the previous request (finished within 1 s)
  ds.requestTemperatures();          // request the next conversion

  if (t == DEVICE_DISCONNECTED_C || t == 85.0) {   // -127 = disconnected, 85 = power-on reset value
    tNow = NAN;
    Serial.println("DS18B20 sensor error");
  } else {
    tNow = t;
    if (t < tMin) tMin = t;
    if (t > tMax) tMax = t;
    if (tick++ % 5 == 0) pushHist(t);   // 1 point / 5 s: 128 points ≈ 10 min
    Serial.printf("%.2f C\n", t);
  }
  draw();
}

Why non-blocking reads?

By default the DallasTemperature library calls requestTemperatures() and then waits out the conversion time (750 ms at 12-bit) with delay. During that time the CPU can do nothing, so the display and buttons freeze. With setWaitForConversion(false) the request returns immediately; the conversion runs inside the sensor and we collect the result on the next iteration, one second later, which is plenty of time.

Blocking: delay(750) while convertingCPU waitsNon-blocking: setWaitForConversion(false)CPU does other work (WiFi, OLED, buttons...)0750 ms1000 ms12-bit conversion ≈ 750 msrequestread
Figure 3: Blocking versus non-blocking reads. Both take ~750 ms to convert, but the non-blocking version does not make the CPU wait.

Points worth explaining

Troubleshooting

SymptomWhat to do
Display stays dark, Serial says OLED not foundCheck SDA/SCL and power. Run an I2C scanner sketch to find the real address (0x3C or 0x3D) and update OLED_ADDR.
Display lights up but the image is shifted/noisy1.3" modules usually use an SH1106 controller rather than SSD1306; use the U8g2 or Adafruit SH110X library.
Always −127 °CMissing or wrong pull-up, DQ on the wrong pin, loose connection.
Always 85 °CA weak or noisy supply stops the chip finishing the conversion, or it is read too early. Add a 100 nF capacitor next to the chip and check the supply.
Reads a few degrees too highThe sensor is next to the ESP32 or OLED, which give off heat. Move it away or use a probe on a long lead.

Next steps