PinoutSearch

Waterproof Temperature with DS18B20 (Arduino)

Read temperature from a DS18B20 1-Wire sensor — ideal for waterproof probes. Learn the OneWire bus.

Beginner25 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

ds18b20.ino
// Read a DS18B20 1-Wire temperature sensor.
// Install the "OneWire" and "DallasTemperature" libraries (Library Manager).
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(2);                 // DQ on pin 2
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(9600);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures();
  float c = sensors.getTempCByIndex(0);
  Serial.print(c); Serial.println(" C");
  delay(1000);
}

Steps

  1. Install "OneWire" and "DallasTemperature" via Tools → Manage Libraries.
  2. Wire the DS18B20: VDD to 5V, GND to GND, DQ (data) to D2.
  3. Add a 4.7kΩ resistor from DQ to VDD (1-Wire pull-up). Pre-wired probe modules often include it.
  4. Upload, open the Serial Monitor at 9600 baud.