Arduino DallasTemperature Library

The DallasTemperature library provides a high-level interface for Maxim/Dallas 1-Wire digital temperature sensors. This tutorial starts with installation and a first measurement, then covers device addresses, resolution, asynchronous measurements, error handling, and alarm functions.

Difficulty: Intermediate

Created: January 2024

Technically reviewed: August 2026

Reference platform: General Arduino API, Arduino Uno R3

What is the DallasTemperature library for?

When working directly with the 1-Wire protocol, a program must discover devices, process addresses and scratchpad data, verify CRC values, and start temperature conversions. The DallasTemperature library hides these tasks behind a convenient API.

The library supports the DS18B20, DS18S20/DS1820, and DS1822 families, among others. The examples use the widely adopted DS18B20. The exact list of supported devices depends on the installed library version.

Key features of the DS18B20

  • unique factory-programmed 64-bit 1-Wire address;
  • measurement range from −55 to +125 °C;
  • programmable 9-, 10-, 11-, or 12-bit resolution;
  • standard three-wire or parasite power operation;
  • multiple sensors on a shared data line;
  • programmable low and high alarm thresholds.

Higher resolution increases the maximum conversion time: approximately 94 ms at 9 bits, 188 ms at 10 bits, 375 ms at 11 bits, and 750 ms at 12 bits.

Wiring and power

For a reliable beginner setup, use three-wire power: connect VDD to the appropriate supply voltage, GND to common ground, and DQ to a digital Arduino pin. A 4.7 kΩ pull-up resistor is typically connected between DQ and VDD. Long cables or multiple devices may require careful selection of the resistor and bus topology.

Three-wire connection of a DS18B20 sensor to an Arduino Uno with a 4.7 kilo-ohm pull-up resistor
The DQ data line connects to pin D10, with a 4.7 kΩ resistor pulling it up to the supply voltage. Verify the pinout of your particular sensor before wiring it.

Parasite power requires fewer wires, but it needs a suitable strong pull-up during temperature conversion and careful bus design. External power is simpler and more reliable for a first project.

Installing the libraries

  1. Open the Library Manager in the Arduino IDE.
  2. Search for DallasTemperature and install the library maintained by Miles Burton.
  3. Install the required OneWire library as well if the IDE does not install it automatically.

Creating objects and initializing the library

#include <OneWire.h>
#include <DallasTemperature.h>

const byte oneWirePin = 10;

OneWire oneWire(oneWirePin);
DallasTemperature sensors(&oneWire);

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

The OneWire object manages the bus, while the DallasTemperature instance refers to that object and provides higher-level operations for the temperature sensors. The begin() function initializes the library and discovers supported devices.

Your first temperature measurement

#include <OneWire.h>
#include <DallasTemperature.h>

const byte oneWirePin = 10;
OneWire oneWire(oneWirePin);
DallasTemperature sensors(&oneWire);

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

void loop()
{
  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0);

  if (temperatureC == DEVICE_DISCONNECTED_C)
  {
    Serial.println("Sensor cannot be read");
  }
  else
  {
    Serial.print(temperatureC);
    Serial.println(" °C");
  }

  delay(1000);
}

The requestTemperatures() function starts the conversion. By default, the library waits for it to finish. The getTempCByIndex(0) function returns the latest reading from the first detected sensor in degrees Celsius.

Access by index is convenient for a demonstration, but the relationship between an index and a physical device is not a reliable permanent identifier when several sensors are connected. Use the unique 64-bit address instead.

Device count, addresses, and validation

Counting devices

The getDeviceCount() function returns the number of devices found by the library on the bus.

uint8_t deviceCount = sensors.getDeviceCount();

Retrieving an address by index

The getAddress(address, index) function returns true if it successfully retrieves a supported device address for the specified index. DeviceAddress is an eight-byte address type.

DeviceAddress address;

if (sensors.getAddress(address, 0))
{
  // The address variable now contains the device address.
}

Validating an address and device family

The validAddress(address) function checks the address CRC and format, while validFamily(address) checks whether the family code belongs to a supported temperature sensor. The isConnected(address) function tests whether the device responds.

bool usable = sensors.validAddress(address)
           && sensors.validFamily(address)
           && sensors.isConnected(address);

Device address finder

#include <OneWire.h>
#include <DallasTemperature.h>

const byte oneWirePin = 10;
OneWire oneWire(oneWirePin);
DallasTemperature sensors(&oneWire);

void printAddress(const DeviceAddress address)
{
  for (byte i = 0; i < 8; i++)
  {
    if (address[i] < 0x10) Serial.print('0');
    Serial.print(address[i], HEX);
    if (i < 7) Serial.print(':');
  }
}

void setup()
{
  Serial.begin(115200);
  sensors.begin();

  uint8_t count = sensors.getDeviceCount();
  Serial.print("Number of devices: ");
  Serial.println(count);

  for (uint8_t index = 0; index < count; index++)
  {
    DeviceAddress address;
    if (sensors.getAddress(address, index))
    {
      Serial.print(index);
      Serial.print(": ");
      printAddress(address);
      Serial.println();
    }
  }
}

void loop() {}

Reading temperature by device address

Replace the address bytes with the address of your own sensor. Address-based measurements identify the physical sensor unambiguously even when several devices share the bus.

DeviceAddress roomSensor = {
  0x28, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void loop()
{
  sensors.requestTemperaturesByAddress(roomSensor);
  float temperatureC = sensors.getTempC(roomSensor);

  if (temperatureC != DEVICE_DISCONNECTED_C)
  {
    Serial.println(temperatureC);
  }

  delay(1000);
}

The address above is only a formatting example and cannot be used unchanged. Retrieve your own address with the address finder program. The library also provides requestTemperaturesByIndex(index) and index-based Celsius and Fahrenheit readings.

Setting the resolution

The DS18B20 supports 9- to 12-bit resolution. The setResolution() function sets the resolution globally or for a specified address, while getResolution() retrieves it.

sensors.setResolution(12);

// For a single addressed device:
sensors.setResolution(roomSensor, 11);
uint8_t resolution = sensors.getResolution(roomSensor);

Do not select a higher resolution without a reason: the extra bits increase conversion time and do not improve the sensor’s fundamental accuracy.

Blocking and asynchronous measurements

By default, requestTemperatures() waits for the conversion to finish. With setWaitForConversion(false), the request returns immediately, allowing the program to perform other work in the meantime. The program must then manage the appropriate waiting time or conversion state.

const unsigned long conversionTimeMs = 750;
unsigned long conversionStartedAt = 0;
bool conversionRunning = false;

void setup()
{
  Serial.begin(115200);
  sensors.begin();
  sensors.setResolution(12);
  sensors.setWaitForConversion(false);
}

void loop()
{
  if (!conversionRunning)
  {
    sensors.requestTemperatures();
    conversionStartedAt = millis();
    conversionRunning = true;
  }

  if (conversionRunning
      && millis() - conversionStartedAt >= conversionTimeMs)
  {
    float temperatureC = sensors.getTempCByIndex(0);
    if (temperatureC != DEVICE_DISCONNECTED_C)
      Serial.println(temperatureC);

    conversionRunning = false;
  }

  // Other non-blocking tasks can run here in the meantime.
}

The getWaitForConversion() function reports the current waiting setting. The setCheckForConversion(), getCheckForConversion(), and isConversionComplete() functions can help check conversion completion, although their behavior may also depend on the power mode and library version.

Checking the power mode

The readPowerSupply(address) function returns true when the specified device uses parasite power. Without an address, it checks whether any parasite-powered device is present on the bus. The isParasitePowerMode() function returns the bus state maintained by the library.

bool parasite = sensors.readPowerSupply(roomSensor);

Scratchpad access

The readScratchPad(address, scratchPad) function reads the sensor’s nine-byte scratchpad area. The isConnected(address, scratchPad) overload checks the connection and reads the scratchpad in one operation. writeScratchPad() is a low-level operation; incorrect data can change alarm thresholds or configuration.

ScratchPad scratchPad;

if (sensors.readScratchPad(roomSensor, scratchPad))
{
  for (byte i = 0; i < 9; i++)
  {
    Serial.println(scratchPad[i], HEX);
  }
}

Alarm thresholds

For supported sensors, setLowAlarmTemp() and setHighAlarmTemp() set the low and high alarm thresholds. The getLowAlarmTemp() and getHighAlarmTemp() functions read them back.

sensors.setLowAlarmTemp(roomSensor, 5);
sensors.setHighAlarmTemp(roomSensor, 30);

bool alarmActive = sensors.hasAlarm(roomSensor);

For alarm searches and processing, the library may also provide resetAlarmSearch(), alarmSearch(), hasAlarm(), processAlarms(), and setAlarmHandler(). Availability may depend on compile-time settings and the library version; check the installed version’s header and examples before use.

Other API groups

TaskKey operations
Starting a conversionrequestTemperatures(), requestTemperaturesByAddress(), requestTemperaturesByIndex()
Raw or converted valuegetTemp(), getTempC(), getTempF(), getTempCByIndex(), getTempFByIndex()
ConversionstoFahrenheit(), toCelsius(), rawToCelsius(), rawToFahrenheit(), celsiusToRaw()
User datasetUserData(), getUserData()

Exact function signatures in the public API may vary between library versions. For advanced use, always treat the installed release’s DallasTemperature.h file and bundled examples as authoritative.

Error handling

  • DEVICE_DISCONNECTED_C indicates a connection or read error; do not treat it as a real temperature.
  • 85 °C is often the DS18B20’s power-on default. If it appears repeatedly, the conversion may not have finished or the read sequence may be incorrect.
  • −127 °C is typically a library error value, not an actual measurement.
  • With multiple sensors, check the success of every address retrieval and reading separately.
  • For intermittent errors, inspect the power supply, pull-up resistor, wiring, and bus length.

🧪 Try it!

  1. Read the address of one DS18B20, then copy it into the address-based example.
  2. Set the resolution first to 9 bits and then to 12 bits, and measure the execution time of the blocking request with millis().
  3. Convert the program to use non-blocking measurements.
  4. Connect two sensors to the same bus and identify them by address.
  5. Disconnect one sensor and verify that the program handles the result as an error.

Common mistakes

  • Missing pull-up resistor: the bus may become unreliable or fail completely.
  • Reversed pins: bare TO-92 devices and ready-made sensor modules may use different connections; check the datasheet.
  • Reading immediately after an asynchronous request: you may receive the previous value or the power-on default.
  • Using an index as a permanent identifier: use the 64-bit address when several devices are connected.
  • Displaying an error code as a temperature: validate every reading.
  • Assuming parasite power automatically: you must account separately for the energy required during conversion.

Summary

The DallasTemperature library simplifies discovery, addressing, configuration, and reading of supported 1-Wire temperature sensors. A simple project may only need blocking measurements by index, but multiple sensors or more complex programs call for address-based identification, non-blocking conversion, and consistent error handling.


← Further Arduino Tutorials