DS18B20 Waterproof Temperature Probe
The 1-Wire digital thermometer that survives water, soil and long cables, with a unique address so many probes can share one pin.
- Temperature range
- −55 °C to +125 °C
Wiring & code for
What is the DS18B20?
If a project needs to know how hot a liquid, a patch of soil or a freezer is, the DS18B20 is usually the first sensor people reach for. It looks like a small black transistor (the TO-92 package) or, more often, like a short stainless-steel tube on the end of a cable. Inside is a complete digital thermometer: a temperature sensor, an analog-to-digital converter, a bit of memory and a tiny communication engine. You never read a voltage, you never convert a resistance and you never calibrate anything. You ask for a temperature and you get a number in degrees Celsius.
What makes the DS18B20 special is how it connects. It uses Dallas/Maxim's 1-Wire bus, which needs only one data wire (plus ground, and power if you do not use parasite mode). Every chip is laser-engraved with a unique 64-bit address at the factory, so you can wire five, ten or twenty probes onto the same pin and still ask each one separately for its own reading. That single feature is why it is the standard choice for multi-point monitoring in aquariums, greenhouses, brewing, heating systems and server rooms.
The DS18B20 works from 3.0 V to 5.5 V, so it runs directly from an Arduino Uno (5 V) or an ESP32 (3.3 V) with no level shifting at all. The rest of this page covers the pinout, the one resistor you must not forget, working code for both boards, how to read several probes at once, and what the strange values 85 °C and −127 °C really mean.
Warning
The classic DS18B20 mistakes are a missing 4.7 kΩ pull-up resistor and a reversed VDD/GND on a bare TO-92 chip. The first gives you −127 °C or "no devices found". The second makes the chip hot enough to burn skin within seconds. Check the pinout and add the resistor before you apply power.
Specifications
- Sensing element
- Digital temperature sensor with built-in 9–12 bit ADC, factory calibrated
- Operating voltage
- 3.0 V – 5.5 V DC (works on both 3.3 V and 5 V boards)
- Temperature range
- −55 °C to +125 °C
- Accuracy
- ±0.5 °C from −10 °C to +85 °C; wider error outside that window (see datasheet)
- Resolution
- Selectable 9, 10, 11 or 12 bit (0.5, 0.25, 0.125 or 0.0625 °C steps)
- Conversion time
- 93.75 ms at 9-bit up to 750 ms at 12-bit (maximum)
- Interface
- Dallas/Maxim 1-Wire: one data line plus ground, optional parasite power
- Device identity
- Unique 64-bit ROM code (family code 0x28 plus 48-bit serial and CRC)
- Pull-up resistor
- 4.7 kΩ between DQ and VDD (not built in on bare parts or most probes)
- Standby current
- About 1 µA when idle; roughly 1–1.5 mA while converting
- Alarm function
- Programmable high and low alarm thresholds stored in on-chip EEPROM
- Probe cable
- Typically 1 m, three wires; length and sleeve vary by seller (often 6 mm stainless steel)
Pinout
| Pin | Name | Description |
|---|---|---|
| 1 | GND | Ground. On the common TO-92 package this is the left pin with the flat face toward you |
| 2 | DQ | Data line. Open-drain, needs a 4.7 kΩ pull-up to VDD |
| 3 | VDD | Power, 3.0–5.5 V. Tie to GND only in parasite-power mode (not recommended) |
Swipe sideways to see every column →
On the bare TO-92 package, hold the sensor with the flat face toward you and the legs pointing down: the pins run GND, DQ, VDD from left to right. The waterproof probe has three wires, and the usual colours are red for VDD, black for GND and yellow (sometimes white or blue) for DQ. Colours are not standardised, so check the listing or test with a multimeter before applying power. Reversing VDD and GND makes the chip get very hot within seconds, so a wrong guess can destroy it and burn your fingers. On a 3.3 V board such as the ESP32, power the sensor from 3.3 V and pull the data line up to 3.3 V, never to 5 V.
Variants
| Variant | Form | Temp range | Accuracy | Interface | Price |
|---|---|---|---|---|---|
| DS18B20 probe (stainless, 1 m cable) | Sealed probe | −55–125°C | ±0.5°C | 1-Wire | ~$1–3 |
| DS18B20 TO-92 | Bare chip | −55–125°C | ±0.5°C | 1-Wire | ~$1–2 |
| DS18B20 breakout module | PCB with pull-up | −55–125°C | ±0.5°C | 1-Wire | ~$1–2 |
| DS18S20 | TO-92 | −55–125°C | ±0.5°C | 1-Wire, 9-bit only | ~$2–3 |
| MAX31820 | TO-92 / small | −55–125°C | ±0.5°C | 1-Wire, 3.0–3.7 V | ~$3–5 |
Swipe sideways to see every column →
For most projects the waterproof probe is the right DS18B20: it is sealed, it comes on a cable and it costs about a dollar or two. Choose the bare TO-92 when you are building your own enclosure or PCB. If you want humidity as well as temperature, look at the DHT22 or SHT31, and if you need air pressure too, the BME280.
How the DS18B20 measures temperature
Inside the chip, a temperature sensor built from silicon junctions produces a signal that changes predictably with temperature. A small converter turns that signal into a digital number, and the chip stores the result in a 9-byte memory called the scratchpad. When you request a reading, the chip does the conversion on its own, then you read the scratchpad back over the 1-Wire line. The temperature itself occupies the first two bytes as a signed 16-bit number, and the last byte of the scratchpad is a CRC checksum. The library checks that checksum for you, which is why a noisy cable gives you an error value instead of a believable but wrong temperature.
You can choose how finely the chip measures. At 9-bit resolution the temperature changes in 0.5 °C steps and the conversion is finished in about 94 ms. At 12-bit resolution, the chip's default, the step is 0.0625 °C and the conversion takes up to 750 ms. The trade-off is simple: more bits means more precision in the number but more waiting time per reading.
| Resolution | Temperature step | Maximum conversion time |
|---|---|---|
| 9-bit | 0.5 °C | 93.75 ms |
| 10-bit | 0.25 °C | 187.5 ms |
| 11-bit | 0.125 °C | 375 ms |
| 12-bit | 0.0625 °C | 750 ms |
It is important not to confuse resolution with accuracy. Resolution is the size of the step in the number. Accuracy is how close the number is to the real temperature. A 12-bit reading changes in 0.0625 °C steps, but the datasheet accuracy is ±0.5 °C between −10 °C and +85 °C. Two genuine sensors sitting in the same glass of water can easily differ by a few tenths of a degree. If you need absolute accuracy, calibrate each probe against a trusted reference, as described in the accuracy section below.
Wiring: the 4.7 kΩ pull-up resistor
The 1-Wire data line is open-drain, which means the sensor and your board can only pull it down to ground. Something else has to pull it back up to a high level when nobody is driving it. That something is a 4.7 kΩ pull-up resistor between DQ and VDD. Without it, the line floats and your board sees either nothing or garbage, which is by far the most common reason a DS18B20 "does not work".
The wiring is the same for every board:
- VDD to 5 V on an Arduino Uno, or to 3.3 V on an ESP32
- GND to GND
- DQ to a digital pin (D2 on the Uno, GPIO4 on the ESP32 in our examples)
- 4.7 kΩ resistor between DQ and the same VDD rail you used for power
Some breakout modules and a few probes with a small PCB at the end already include the resistor. If yours has one, do not add a second one unless the readings are unstable. Two 4.7 kΩ resistors in parallel make about 2.35 kΩ, which usually still works but loads the line more than necessary.
On the ESP32, the pull-up must go to 3.3 V. The ESP32's pins are not 5 V tolerant, so never pull the data line up to 5 V. Also avoid GPIO34 to GPIO39, which are input-only and cannot drive the line, and avoid the boot-sensitive pins GPIO0, GPIO2, GPIO12 and GPIO15 if you can.
- 1Open the Arduino IDE and go to Tools → Manage Libraries.
- 2Search for "OneWire" and install the one by Paul Stoffregen.
- 3Search for "DallasTemperature" and install the one by Miles Burton, accepting any dependency prompt.
- 4Wire VDD, GND and DQ as shown, with the 4.7 kΩ resistor between DQ and VDD.
- 5Set ONE_WIRE_BUS to 2 for an Uno or 4 for an ESP32, select your board and port, and upload the sketch.
- 6Open the Serial Monitor at 115200 baud. You should see "DS18B20 devices found: 1" and a temperature every two seconds.
Reading the sensor, line by line
The shortest working sketch needs two libraries. OneWire handles the timing of the bus: it creates the reset pulse, sends and receives individual bits and does the CRC. DallasTemperature sits on top of it and understands the DS18B20 itself: how to start a conversion, how to read the scratchpad and how to convert the raw number into degrees.
OneWire oneWire(ONE_WIRE_BUS); tells the library which pin the data line is on. DallasTemperature sensors(&oneWire); creates the sensor manager that uses that bus. In setup(), sensors.begin() scans the bus and counts the devices, and sensors.getDeviceCount() tells you how many it found. If that number is 0, nothing is answering and you should check the wiring before anything else.
In loop(), sensors.requestTemperatures() tells every sensor on the bus to start a conversion. By default the library waits for the conversion to finish, which takes up to 750 ms at 12-bit. Then sensors.getTempCByIndex(0) reads the first sensor found. If the read fails, the library returns the constant DEVICE_DISCONNECTED_C, which equals −127. The sketch checks for that value, so a loose wire is reported as an error and never recorded as a real temperature.
Using several DS18B20 probes on one pin
This is where the DS18B20 beats almost every other temperature sensor. Because each chip has its own 64-bit address, you can connect all the data wires to the same pin, share one 4.7 kΩ resistor, and read each probe individually. That means three probes on an Arduino Uno still use just one digital pin.
The address has three parts: an 8-bit family code (0x28 for the DS18B20), a 48-bit unique serial number and an 8-bit CRC. The library asks every device to announce itself during a bus search and then stores the addresses in the order it finds them.
That order is where beginners get caught. getTempCByIndex(0) means "the first device the search found", and the search order depends on the addresses and the physical bus, not on where you placed the probes. If you swap a probe or add a new one, the index numbers can change. The reliable method is to run a scanner sketch once, write down each address and label the probe with tape, then read by address in your real program.
The first sketch below prints the address of every probe. The second reads two probes by their stored addresses.
Reading without freezing your program
requestTemperatures() blocks for up to 750 ms at 12-bit. For a data logger that is fine. For a project that also scrolls an LCD, checks a button or serves a web page, three-quarters of a second of dead time per reading is a problem.
The fix is to start the conversion, carry on with other work, and collect the result later. Call sensors.setWaitForConversion(false) once, then request a conversion, remember the time and read the value after the conversion time has passed. At 12-bit that means waiting at least 750 ms, and it is sensible to add a small margin.
This also matters on the ESP32. A blocking delay() inside a long loop can starve Wi-Fi and MQTT tasks. Non-blocking reads keep the connection healthy and still give you a fresh temperature every few seconds.
Parasite power: what it is and why to skip it
The DS18B20 can run without a separate supply wire. In parasite power mode you connect VDD to GND and the chip charges a tiny capacitor from the data line while the line is high. This lets you use just two wires, which sounds attractive for a very long or awkward cable.
The catch is that during a conversion the sensor draws more current than a weak pull-up resistor can deliver. The datasheet requires a strong pull-up, such as a MOSFET driven by a spare pin, to be switched on for the whole conversion. Without it you get random errors and the famous 85 °C value, and the problem gets worse at 12-bit and at higher temperatures. Libraries handle parasite mode, but the extra hardware is rarely worth it. If you can run three wires, do so. Three-wire mode with a normal 4.7 kΩ resistor is simpler, faster and far more reliable.
Cable length and bus topology
One of the DS18B20's selling points is that it tolerates long cables, and it genuinely does. Many people run tens of metres of ordinary cable to outdoor and tank probes. But how far you can go depends on cable capacitance, wiring layout, the number of sensors and the pull-up value, so treat any single number you read online as a rough guide.
A few habits make long runs far more reliable:
- Use a linear (daisy-chain) bus with short stubs, not a star with long branches. Reflections and capacitance add up fast in a star.
- Use twisted-pair or shielded cable for long runs, and put the data wire in a pair with ground.
- Put the pull-up resistor near the microcontroller. If readings get flaky on a very long run, try lowering it to around 3.3 kΩ or 2.2 kΩ and test carefully.
- Add a small series resistor (about 100 Ω) between the pin and the line if you see ringing, and keep the bus away from motors, relays and mains wiring.
- Power the sensors from a stable supply and keep VDD and GND together in the same cable as the data wire.
Getting accurate readings
A DS18B20 is accurate for a one-dollar sensor, but where and how you use it still changes what it tells you.
Let it reach equilibrium. The chip measures its own temperature, not the temperature of what you are interested in. A sealed stainless probe takes time to match the liquid around it, and a probe in still air reacts slower than one in moving water. Wait until the reading stops creeping before you trust it.
Keep it away from heat sources. A voltage regulator, relay coil or the microcontroller itself will warm nearby air and the probe's cable. Mount the sensor on its lead, in free air, and not next to the board.
Watch out for self-heating. Reading it constantly at 12-bit means the chip draws current almost continuously, which warms it by a small amount. For slow-changing temperatures, one reading every few seconds is plenty.
Calibrate if accuracy matters. The simplest field calibration uses two points. Put the probe in a mixture of crushed ice and water, which should sit close to 0 °C once stirred, and note the reading. Then compare it with a trusted reference thermometer at a second temperature. If the difference is stable, subtract a fixed correction in your code, or fit a straight line through the two points for a gain and offset correction.
Beware of fake chips. DS18B20 clones are common on marketplaces. Some behave slightly differently, with oddities such as readings stuck at 85 °C, different conversion behaviour or a family code that does not look like 0x28. If one probe in a batch misbehaves while others work, swap it before you spend hours debugging the code.
Troubleshooting
Nearly every DS18B20 problem comes from the pull-up, wiring, power or a wrong pin. Find your symptom below.
| Symptom | Likely cause | Fix |
|---|---|---|
| "Devices found: 0" | Missing pull-up, wrong pin, DQ not connected, no common ground | Add a 4.7 kΩ resistor between DQ and VDD, check the pin number in the sketch and the ground wire |
| Temperature is −127 °C | Library's "no answer" value: broken wire, missing pull-up or bus error | Check each connection, shorten the cable, make sure the resistor goes to the same VDD that powers the sensor |
| Temperature is always 85 °C | Power-on value of the register: conversion did not complete or the result was never updated | Wait for the conversion time, check power stability, avoid parasite mode, and test a different probe |
| Chip is very hot and readings are wrong | VDD and GND reversed on the bare TO-92 | Disconnect immediately, check the pin order against the flat face, replace the chip if it has been damaged |
| Works on Uno, not on ESP32 | Pull-up connected to 5 V, input-only GPIO used, or the wrong GPIO number | Pull up to 3.3 V, use a pin such as GPIO4 and match the number in the sketch |
| Random failures on a long cable | Noise, reflection, a star topology or a weak pull-up | Use a daisy-chain bus, twisted or shielded cable, and test a lower pull-up value |
| Readings jump by several degrees | Loose connection, noisy supply or a counterfeit chip | Re-seat the connectors, add a 100 nF capacitor across VDD and GND near the sensor, and try another probe |
| Multi-probe readings swap places | Index numbers changed after a probe was added or moved | Read by address instead of by index |
| First reading after power-up is wrong | Power-on register value or a conversion that has not finished | Skip the first reading, or call requestTemperatures() once in setup() |
What to build with it
The DS18B20's mix of accuracy, waterproofing and multi-drop wiring suits a long list of projects:
- Aquarium and pond monitor that alerts you when the water is too warm or too cold.
- Greenhouse and soil logger with probes at different heights and depths.
- Fridge and freezer alarm that notices a failing compressor or an open door.
- Home brewing and sous-vide control where you drive a heater from a relay and hold a target temperature.
- Solar water heater or boiler monitor with probes on inlet and outlet pipes to show heat gain.
- ESP32 IoT dashboard that publishes readings over MQTT to Home Assistant, Node-RED or a cloud service.
Whatever you build, treat a disconnected probe as a fault state. Never let −127 °C or 85 °C flow into a control loop or a chart as a real temperature. A heater controller that believes the tank is at −127 °C can do real damage.
Frequently asked questions
Is the DS18B20 waterproof? The sealed stainless-steel probe version is designed for liquids and soil, but quality varies between sellers. The cable joint and the cable itself are the weak points, so keep the joint above the waterline and seal any soldered connections. The bare TO-92 chip is not waterproof at all.
Can I use it with a 3.3 V board like the ESP32? Yes. The chip works from 3.0 V to 5.5 V. Power it from 3.3 V and pull the data line up to 3.3 V.
Do I need one pin per sensor? No. All the probes share one pin. Each one is told apart by its unique 64-bit address.
How many probes can I connect? The protocol allows many devices on one bus, but real limits depend on cable length, capacitance, wiring layout and pull-up value. Small setups of a handful of probes on a short bus are easy. Large installations need careful wiring and testing.
Why does my reading say 85 °C? 85 °C is the value the sensor holds at power-up. If you read before the conversion has finished, or if the conversion fails, you see it. Wait for the conversion, check the power and avoid parasite mode.
Why does my reading say −127 °C? That is the DallasTemperature library's "disconnected" code. It means no valid answer reached your board, usually because of a missing pull-up, a wrong pin or a broken connection.
Is 12-bit resolution worth using? Only if you need the fine steps and can afford 750 ms per reading. For most monitoring projects 10-bit or 11-bit is fine and faster. Remember the accuracy is still ±0.5 °C whichever resolution you pick.
What is the difference between the DS18B20 and the DHT22? The DS18B20 measures temperature only, but it is more accurate, waterproof in probe form, much better on long cables and can share one pin between many sensors. The DHT22 also measures humidity but is not waterproof and needs one pin per sensor.
Sources This page is based on the manufacturer's DS18B20 datasheet and the public documentation of the OneWire and DallasTemperature libraries. For safety-critical measurement, validate the complete probe and installation against the exact datasheet and a calibrated reference.