Temperature sensor, DS18B20
A thermometer with a 64-bit serial number, so any number share one wire. Needs a 4k7 pull-up.
- 3 pins
- 3 settings
- In 5 projects
- In 1 lesson
The Temperature sensor, DS18B20 in brief
What it is, and every setting the properties panel gives it.
The DS18B20 is the three-legged digital thermometer in every kit or the same chip in a steel probe, and it reports -55 to 125 degrees Celsius to a sixteenth of a degree as a number rather than a voltage. With the flat face toward you the legs are GND, DQ and VDD; VDD takes 3.0 to 5.5 volts, so it runs from an Uno’s 5 V or an ESP32’s 3.3 V, and DQ goes to any digital pin. DQ is open drain and needs one 4.7 kilohm pull-up to VDD for the whole bus; forgetting it is the classic reason a sensor is never found, and the pullup property stands in for the three-pin modules that carry one. Mokxi runs the real 1-Wire protocol bit by bit with the datasheet’s timings: the 480 microsecond reset and presence pulse, a ROM search that finds several sensors on one wire, CRC-8 checks, and conversions of 93.75 to 750 milliseconds by resolution. The OneWire and DallasTemperature libraries work as tutorials use them. Read before converting and you get 85,. Parasite power and the half-degree error a real sensor is allowed are not modeled.
- Family
- Sensors
- Pins
- 3
- temperature
- 22
- serial
- pullup
- false
A circuit built around it, running and explained: the DS18B20 simulator.
3 pins
Named as the editor names them, so a wire lands where you expect it.
How to use the DS18B20 with an Arduino
Wire VDD to 5 V, GND to GND and DQ to pin 2, then put one 4.7 k resistor from DQ to 5 V. That single pull-up serves every sensor on the wire, so a second DS18B20 needs only its DQ joined to the same row. Without it the line floats and the library reports no devices at all.
Call sensors.begin() once, then sensors.requestTemperatures() before every read: it starts every sensor converting and waits the 750 ms twelve bits take. Read without it and you get 85, the power-up value. With several sensors, fetch each address once with getAddress() and read it by address after that, which is quicker than searching by index every time.
On an ESP32, power it from 3.3 V and pull DQ up to 3.3 V instead; the sensor runs from 3.0 V and the same libraries compile.
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}What is true about the Temperature sensor, DS18B20, here
In its help article’s own words: what the simulation gets right, and where it stops.
Modeled
The wire, bit by bit, with the datasheet's timings: the 480 µs reset and the presence pulse after it, write slots sampled 30 µs in, read slots where a 0 holds the line low from the master's fall. The ROM commands (READ ROM, MATCH ROM, SKIP ROM, SEARCH ROM and ALARM SEARCH) and the function commands (CONVERT T, READ and WRITE SCRATCHPAD, COPY SCRATCHPAD, RECALL E2, READ POWER SUPPLY), with the Dallas CRC-8 on the ROM code and on the scratchpad.
The search works because the wire does. Several sensors on one net answer a
SEARCH ROM together and the open-drain line ANDs what they send, exactly as on the
bench, so search() finds every one of them. Nothing in the part arbitrates.
Resolution costs time. Nine bits (half a degree) convert in 93.75 ms and twelve bits (a sixteenth) in 750 ms, the datasheet's worst case, and the undefined low bits of a coarse reading read zero. Read slots after CONVERT T return 0 until it is done.
Before the first conversion the temperature register holds +85 °C, as a real one does at power-up. A sketch that reads without converting prints 85, which is the most famous number in the datasheet.
Not modeled
Parasite power. A real DS18B20 can run with VDD tied to ground, living off the
data line, if the master holds the line hard high during a conversion. Here a DS18B20
with no VDD is off the bus, and READ POWER SUPPLY always answers "external".
No error in the reading. The part reports temperature exactly, to its
resolution, with none of the ±0.5 °C a real one is allowed, and it does not warm
itself up. The EEPROM keeps the alarm limits and resolution for the run, not between
runs.
From DS18B20 1-Wire thermometer, in full.
See the Temperature sensor, DS18B20 in a project
5 built-in projects use it. Each opens in the editor, already wired.
Where it turns up in a lesson
One lesson places it in its own circuit.
The boards it is shown on
This board has a built-in project with the Temperature sensor, DS18B20 in it.
The Temperature sensor, DS18B20 on each board
The pins, the voltage and the code change from board to board. Each page has that board’s wiring and a circuit you can run.
Arduino libraries for the Temperature sensor, DS18B20
Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.
The rest of the bench
Every one of these is drawn and simulated the same way.
Wire up the Temperature sensor, DS18B20
Open the editor and push it into the breadboard. It is free, and it runs on your own machine.