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
- ESP32 DevKit board
- DS18B20 sensor: TO-92 chip or a waterproof stainless probe (3 wires)
- 4.7 kΩ resistor (required, pulls the data line up to 3V3)
- 0.96" SSD1306 128×64 OLED with I2C interface (4 pins)
- Breadboard, jumper wires
Key specifications
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
| Device | Pin | ESP32 |
|---|---|---|
| OLED SSD1306 | VCC | 3V3 |
| GND | GND | |
| SCL | GPIO22 | |
| SDA | GPIO21 | |
| DS18B20 | VDD | 3V3 |
| DQ | GPIO4 (and through 4.7 kΩ to 3V3) | |
| GND | GND |
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)
OneWireby Paul StoffregenDallasTemperatureby Miles BurtonAdafruit SSD1306andAdafruit GFX Library(the IDE will offer to install Adafruit BusIO too; accept)
What you should see
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.
Points worth explaining
- −127 °C (
DEVICE_DISCONNECTED_C): the sensor is not responding, usually because of a missing pull-up, a broken wire or the wrong pin. - 85 °C: the default power-on value, returned if the conversion has not finished. The code filters it out; the side effect is that a real reading of exactly 85.0 °C is never shown, which is rare in typical use.
- Ring buffer (
hist,head,n): once full, the oldest point is overwritten, so no extra memory is used during long runs. - Auto-scaling Y axis of the chart using the current min/max, with a minimum span of 1 °C so the line does not jitter when the temperature barely changes.
Troubleshooting
| Symptom | What to do |
|---|---|
| Display stays dark, Serial says OLED not found | Check 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/noisy | 1.3" modules usually use an SH1106 controller rather than SSD1306; use the U8g2 or Adafruit SH110X library. |
| Always −127 °C | Missing or wrong pull-up, DQ on the wrong pin, loose connection. |
| Always 85 °C | A 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 high | The 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
- Connect several DS18B20 to the same GPIO4 and read them with
getTempCByIndex(i)or by ROM address (getAddress) to name each sensor. - Combine with the MQTT weather station to send temperatures to Home Assistant.
- Add an alarm threshold: drive a buzzer or relay when a limit is exceeded (fridge, aquarium).
- Use
oled.dim(true)or switch the display off at night to extend its life and avoid glare.