- â Understanding the DS18B20 sensor (features, accuracy, 1-Wire protocol)
- â Wiring DS18B20 to ESP8266, ESP32, and Pico W
- â Installing OneWire and DallasTemperature libraries
- â Reading temperature data
- â Using multiple DS18B20 sensors on one pin
- â Calibrating DS18B20 readings with offset
- â Troubleshooting common DS18B20 issues
Introduction to DS18B20
The DS18B20 is a popular digital temperature sensor from Maxim Integrated (now Analog Devices). Unlike the DHT22, it measures temperature only (no humidity), but offers excellent accuracy, a wide temperature range, and a unique feature: multiple sensors can share the same data pin!
| Parameter | Value |
|---|---|
| Temperature Range | -55°C to +125°C (-67°F to +257°F) |
| Temperature Accuracy | ¹0.5°C (from -10°C to +85°C) |
| Resolution | 9-bit to 12-bit (configurable, 0.0625°C at 12-bit) |
| Supply Voltage | 3.0V to 5.5V |
| Current Draw | 1.5mA (active), 1ΞA (standby) |
| Interface | 1-Wire (digital, single pin) |
| Unique 64-bit ROM Code | Yes (allows multiple sensors on one pin) |
| Price | $2-4 (waterproof version $5-8) |
| Feature | DS18B20 | DHT22 |
|---|---|---|
| Temperature Only? | â Yes (temperature only) | â Temperature + Humidity |
| Temperature Range | -55°C to +125°C | -40°C to +80°C |
| Accuracy | ¹0.5°C | ¹0.5°C |
| Multiple Sensors | â Yes (unlimited on one pin) | â No (one per pin) |
| Waterproof Version | â Available (stainless steel probe) | â ïļ Limited (not common) |
| Pull-up Resistor | â Required (4.7kÎĐ) | â ïļ Optional (some modules have it) |
| Price | $2-4 | $4-6 |
DS18B20 is better for: extreme temperatures (-55°C to +125°C), waterproof applications (aquariums, outdoors, liquids), multiple temperature points with one pin.
DHT22 is better for: projects needing humidity data, room temperature monitoring, simpler wiring (no external pull-up on some modules).
Pinout and Wiring
The DS18B20 comes in two common form factors: TO-92 transistor package (3 pins) and waterproof probe (3 wires).
TO-92 Package (Through-Hole)
| Pin | Name | Connection |
|---|---|---|
| Pin 1 (left, flat side facing you) | GND | Ground |
| Pin 2 (center) | DATA | GPIO pin + 4.7kÎĐ pull-up to VCC |
| Pin 3 (right) | VDD | 3.3V or 5V power |
DS18B20 TO-92 Pinout (flat side facing you, pins down):
âââââââââââââââââââ
â âââââââââââ â
â â DS18B20 â â
â âââââââââââ â
â â
â âââââ âââââ â
â â 1 â â 2 â â â Pin 1: GND, Pin 2: DATA
â âââââ âââââ â
â âââââ â
â â 3 â â â Pin 3: VDD
â âââââ â
âââââââââââââââââââ
Waterproof Probe Version
| Wire Color (Typical) | Name | Connection |
|---|---|---|
| Red | VDD | 3.3V or 5V power |
| Yellow or White | DATA | GPIO pin + 4.7kÎĐ pull-up to VCC |
| Black | GND | Ground |
The DS18B20 requires a 4.7kÎĐ pull-up resistor between DATA and VCC (3.3V or 5V). Without it, the sensor will not work or will give erratic readings (-127°C). This is NOT optional!
DS18B20 Wiring with 4.7kÎĐ Pull-up Resistor:
3.3V âââââââŽââââââââââââââââââââââ VDD (Pin 3 / Red)
â
4.7kÎĐ
â
GPIO âââââââžââââââââââââââââââââââ DATA (Pin 2 / Yellow)
â
â
GND ââââââââīââââââââââââââââââââââ GND (Pin 1 / Black)
Wiring to ESP8266 (D1 Mini)
| DS18B20 Pin | ESP8266 Pin | GPIO |
|---|---|---|
| VDD | 3.3V | - |
| DATA | D2 (with 4.7kÎĐ pull-up to 3.3V) | GPIO4 |
| GND | GND | - |
ESP8266 D1 Mini + DS18B20 Wiring:
âââââââââââ ââââââââââââ
â D1 Mini â â DS18B20 â
âââââââââââĪ ââââââââââââĪ
â 3.3V ââââââŽââââââââââââââââĪ VDD (3) â
â â â â â
â D2 ââââââžââââââââââââââââĪ DATA (2) â
â â â 4.7kÎĐ â â
â GND ââââââžââââ[====]ââââââĪ GND (1) â
â â â â â
âââââââââââ â ââââââââââââ
ââââââââââââââââââ
Wiring to ESP32
| DS18B20 Pin | ESP32 Pin | GPIO |
|---|---|---|
| VDD | 3.3V | - |
| DATA | GPIO15 (with 4.7kÎĐ pull-up to 3.3V) | 15 |
| GND | GND | - |
Wiring to Raspberry Pi Pico W
| DS18B20 Pin | Pico W Pin | GPIO |
|---|---|---|
| VDD | 3.3V | - |
| DATA | GP15 (with 4.7kÎĐ pull-up to 3.3V) | 15 |
| GND | GND | - |
Installing Required Libraries
DS18B20 requires two libraries: OneWire (communication protocol) and DallasTemperature (high-level functions).
- Open Arduino IDE
- Go to Sketch â Include Library â Manage Libraries
- Search for "OneWire"
- Find "OneWire by Jim Studt" (or Paul Stoffregen version) and click Install
- Search for "DallasTemperature"
- Find "DallasTemperature by Miles Burton" and click Install
If you followed Tutorial 08, you already have these libraries installed. You can skip this step.
Testing Your DS18B20
Before flashing the full OceanRemote firmware, test your DS18B20 with this simple sketch:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // Change to your GPIO pin (ESP8266 D2 = GPIO4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
Serial.println("DS18B20 Test Started");
}
void loop() {
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
if (temp == -127.0) {
Serial.println("DS18B20 read failed! Check wiring and pull-up resistor.");
} else {
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println("°C");
}
delay(2000);
}
- If you see temperature values (e.g., 22.5°C), your sensor is working!
- If you see -127.0°C, check your wiring and pull-up resistor
- If you see 85.0°C, the sensor is powered but not communicating (check DATA connection)
Multiple DS18B20 Sensors on One Pin
The DS18B20's superpower is that you can connect multiple sensors to the same GPIO pin. Each sensor has a unique 64-bit ROM address, and the library automatically detects all connected sensors.
Wiring Multiple DS18B20 Sensors (Parasite Mode Not Recommended):
3.3V âââââââŽâââââââââââââââââââââââââââââââââââââââ
â â
4.7kÎĐ 4.7kÎĐ
â â
GPIO âââââââžâââââââââââââââââââââââââââââââââââââââžâââââââ
â â â
â ââââââââââââ â â
â â DS18B20 â â â
â â #1 â â â
â ââââââââââââ â â
â â â â
â ââââââââââââ â â
â â DS18B20 â â â
â â #2 â â â
â ââââââââââââ â â
â â â â
GND ââââââââīââââââââââââââââââââââââââīââââââââââââīâââââââ
Detecting Multiple Sensors
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
int deviceCount = sensors.getDeviceCount();
Serial.print("Found ");
Serial.print(deviceCount);
Serial.println(" DS18B20 sensors");
for (int i = 0; i < deviceCount; i++) {
DeviceAddress address;
sensors.getAddress(address, i);
Serial.print("Sensor ");
Serial.print(i);
Serial.print(" Address: ");
printAddress(address);
}
}
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void loop() {
sensors.requestTemperatures();
int deviceCount = sensors.getDeviceCount();
for (int i = 0; i < deviceCount; i++) {
float temp = sensors.getTempCByIndex(i);
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
Serial.print(temp);
Serial.println("°C");
}
delay(2000);
}
OceanRemote firmware currently supports one DS18B20 sensor per device. To monitor multiple temperature points, create separate devices or modify the firmware. This feature may be expanded in future versions.
How OceanRemote Uses DS18B20
When you select DS18B20 in the device configuration, OceanRemote firmware automatically:
- Initializes the OneWire bus on the specified pin
- Searches for the first DS18B20 sensor (index 0)
- Reads temperature every 5 seconds
- Sends the temperature value to the server with each poll
- Displays temperature on your dashboard
- Allows temperature calibration via offset (dashboard setting)
// How OceanRemote reads DS18B20 (firmware snippet)
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // GPIO4 on ESP8266 (D2)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setupSensor() {
sensors.begin();
}
void readSensor() {
static unsigned long lastRead = 0;
unsigned long now = millis();
if (now - lastRead >= 5000) {
lastRead = now;
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
if (temp == -127.0 || temp == 85.0) {
temperature = -999; // Invalid reading
} else {
temperature = temp;
}
}
}
Calibrating DS18B20 Readings
OceanRemote allows you to calibrate temperature readings using an offset. The DS18B20 is quite accurate (¹0.5°C), but calibration can correct for installation-specific errors.
How to Calibrate:
- Place a known-accurate thermometer next to your DS18B20
- Note the difference between the two readings
- In your OceanRemote dashboard, find your device's temperature card
- Enter the offset value in the "Calibration Offset (¹°C)" field
- The formula is: Calibrated = Raw Reading + Offset
Example: Raw reading: 23.5°C Actual temperature: 22.8°C Difference: -0.7°C Enter offset: -0.7 Result: 23.5 + (-0.7) = 22.8°C â
Parasite Mode (Advanced)
The DS18B20 can operate in "parasite mode" where it draws power from the DATA line, eliminating the need for a separate VDD connection. This is useful for 2-wire installations.
Parasite mode requires a strong pull-up (typically 2.2kÎĐ instead of 4.7kÎĐ) and may be unreliable with long cables. OceanRemote firmware uses external power mode (VDD connected to 3.3V/5V) for maximum reliability.
Troubleshooting DS18B20
Problem: Temperature reads -127°C
- Most likely: Missing or incorrect pull-up resistor! Add 4.7kÎĐ between DATA and 3.3V.
- Check wiring - DATA must be connected to the correct GPIO pin
- Verify VDD is connected to 3.3V or 5V
- Try a different GPIO pin
Problem: Temperature reads 85°C
- This is the power-on reset value of the DS18B20
- Usually means the sensor is powered but communication is failing
- Check DATA connection and pull-up resistor value
- Try a shorter cable (long cables can cause signal issues)
Problem: Erratic or jumping readings
- Check pull-up resistor (4.7kÎĐ is recommended)
- Check for loose connections
- Avoid running DATA wire near power lines (60Hz interference)
- For long cables (>10m), use a lower pull-up value (2.2kÎĐ) and shielded cable
Problem: No sensors found
- Run the address detection sketch to verify sensor is responding
- Check that VDD is getting power (3.3V or 5V)
- Verify pull-up resistor is installed
- Try a different DS18B20 (sensor may be damaged)
The DS18B20 is NOT analog - don't connect DATA to A0! It's a digital 1-Wire sensor and MUST be connected to a digital GPIO pin.
DS18B20 Best Practices
- Pull-up Resistor: Always use a 4.7kÎĐ resistor between DATA and VCC. This is not optional!
- Waterproof Version: Great for aquariums, outdoor sensors, refrigerators, and soil temperature monitoring.
- Cable Length: Keep wires under 10-20 meters for reliable communication. Use shielded cable for longer runs.
- Multiple Sensors: You can connect dozens of DS18B20 sensors to one pin! Each has a unique address.
- Power: Use 3.3V for consistency with ESP boards, or 5V if needed (both work).
- Resolution: Default 12-bit resolution gives 0.0625°C precision. OceanRemote uses default settings.
- Aquariums: Monitor fish tank temperature remotely
- Refrigerators/Freezers: Alert if temperature goes out of safe range
- Compost/Garden: Monitor soil temperature for planting
- Beverage Fermentation: Track beer/wine fermentation temperature
- HVAC Ducts: Monitor air handler temperatures
- Outdoor Weather: Place sensor in a radiation shield
Next Steps
Now that you understand the DS18B20 sensor, continue with:
- Tutorial 18: NTC 10kÎĐ Thermistor Guide
- Tutorial 19: Understanding the NTC Voltage Divider Circuit
- Tutorial 20: Calibrating Temperature Sensors with Offset
- Return to Tutorial 02: Generate firmware with DS18B20 support!
Your DS18B20 is now properly wired and tested. Generate new firmware with DS18B20 selected, and your dashboard will show temperature readings!
Pro tip: For multiple temperature points, consider using multiple DS18B20 sensors on one pin, or create separate devices for each location.