DallasTemperature: DS18B20 thermometers on one wire
DallasTemperature, by Miles Burton, sits on top of the OneWire library and knows the commands of Maxim鈥檚 temperature sensors, the DS18B20 above all. It finds the sensors on the bus, starts a conversion on all of them with one call, and reads each one back in Celsius or Fahrenheit, so a sketch for ten waterproof probes is barely longer than one for a single probe.
Built in on Mokxi: #include <DallasTemperature.h> and it builds, with nothing to install. Free, and no account needed.
What DallasTemperature does
Reading is two steps. requestTemperatures() tells every sensor to measure, and by default waits for the conversion, which takes up to 750 ms at the full 12-bit resolution. Then getTempCByIndex(0) reads the first sensor found, getTempCByIndex(1) the second, and so on.
Index order comes from the bus search and can change when you add a sensor. For a project where each probe has a fixed place, read each sensor鈥檚 8-byte address once and use getTempC(address) from then on.
| Library Manager name | DallasTemperature |
|---|---|
| Author | Miles Burton |
| Include | #include <DallasTemperature.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Temperature sensor, DS18B20 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <DallasTemperature.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "DallasTemperature" by Miles Burton from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add DallasTemperature to lib_deps in PlatformIO. Mokxi follows the calls of release 3.11.0, so a sketch written here builds there too.
ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
OneWire oneWire(2)
The 1-Wire bus on pin 2, with a 4.7 k pull-up to the supply.
DallasTemperature sensors(&oneWire)
Creates the temperature library on that bus.
sensors.begin()
Searches the bus and counts the sensors. Call it in setup().
sensors.getDeviceCount()
How many devices begin() found, handy to print as a wiring check.
sensors.requestTemperatures()
Starts a conversion on every sensor and waits for it to finish.
sensors.requestTemperatures();
float c = sensors.getTempCByIndex(0);
if (c == DEVICE_DISCONNECTED_C) Serial.println("no sensor");
else Serial.println(c);sensors.getTempCByIndex(0)
The first sensor鈥檚 temperature in Celsius, or DEVICE_DISCONNECTED_C (-127) if it did not answer. getTempFByIndex() gives Fahrenheit.
sensors.setResolution(10)
Sets 9 to 12 bits. 9 bits converts in about 94 ms with 0.5 C steps; 12 bits takes 750 ms with 0.0625 C steps.
sensors.setWaitForConversion(false)
Makes requestTemperatures() return at once so the loop can do other work; read the result after the conversion time has passed.
Example: DS18B20 temperature sensor on pin 2
On the Arduino Uno R3. It runs in the browser exactly as printed.
// A DS18B20 on one wire: pin 2.
//
// DS18B20 DQ -> pin 2 (4.7k from DQ to 5V) VDD -> 5V GND -> GND
//
// The sensor sends its reading as a string of time slots on a single wire.
// OneWire times them; DallasTemperature asks for a conversion and reads the
// answer back in degrees.
//
// Drag the sensor's temperature.
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire bus(2);
DallasTemperature sensors(&bus);
void setup() {
Serial.begin(9600);
sensors.begin();
Serial.println("Mokxi Uno: DS18B20 on pin 2");
}
void loop() {
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" C");
delay(1000);
}Open it in the editor, wired and ready to run 路 DS18B20 temperature sensor on pin 2: the project page
Common errors and how to fix them
The temperature reads -127.00
That is DEVICE_DISCONNECTED_C: the sensor did not answer. Fit the 4.7 k pull-up between data and the supply, check the pinout of the probe (red VDD, black ground, yellow or white data on most) and the pin number.
The temperature reads 85.00
85 C is the value a DS18B20 holds at power-up. It means the read came before a conversion finished, often from a weak supply in parasite mode. Power VDD directly and keep the default wait for conversion.
getDeviceCount() says 0 but readings work
begin() ran before the sensors had power or before the bus settled. Call sensors.begin() after any power-up delay, and make sure the OneWire object uses the same pin as the wiring.
Projects that use DallasTemperature
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Learn
Try DallasTemperature without installing anything
Open the example, change the sketch, and keep your version in a free account.