tutorialspinouttools
Sensors · ESP32-C3

DS18B20 on OLED: 1-Wire Temperature

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The DS18B20 is a precise digital temperature sensor (-55 to +125 °C, ±0.5 °C) that talks over Maxim's 1-Wire bus. It comes as a small TO-92 chip or a waterproof stainless probe. How it works: Each sensor has a unique 64-bit address, so many can share one data wire. The ESP32 sends a "convert" command, waits, then reads back the temperature over the same wire. Wiring: DS18B20 DATA (middle) -> GPIO10 DS18B20 VDD -> 3V3 DS18B20 GND -> GND IMPORTANT: add a 4.7k pull-up resistor between DATA and 3V3 — the bus does not work reliably without it. OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 Libraries: "OneWire" by Paul Stoffregen, "DallasTemperature" by Miles Burton, plus "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: temperature in °C with one decimal, updated about twice a second. A reading of -127 °C means the sensor wasn't found — check the pull-up and the DATA wire. Gotchas: The waterproof probe's wire colors vary; commonly red=VDD, black=GND, yellow=DATA, but verify with a multimeter. Don't skip the 4.7k pull-up.

Circuit Diagram

DS18B20 on OLED: 1-Wire Temperature wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>          // low-level 1-Wire bus
#include <DallasTemperature.h> // DS18B20 helper

Adafruit_SSD1306 display(128, 64, &Wire, -1);

#define ONE_WIRE 10           // DS18B20 DATA on GPIO10
OneWire oneWire(ONE_WIRE);
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();             // ask all sensors to measure
  float t = sensors.getTempCByIndex(0);      // read the first sensor, °C

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("DS18B20");

  if (t == DEVICE_DISCONNECTED_C) {          // -127 = not found
    display.setCursor(0, 26);
    display.println("No sensor");
    Serial.println("DS18B20 not found");
  } else {
    display.setTextSize(3);
    display.setCursor(0, 24);
    display.printf("%.1f", t);
    display.setTextSize(2);
    display.print(" C");
    Serial.printf("Temp: %.2f C\n", t);
  }
  display.display();
  delay(500);
}
Browse all tutorials ESP32-C3 pinout Calculators