Skip to main content

How to: DS18B20 Thermometer

The DS18B20 is a digital thermometer on the 1-Wire bus. You connect it to the Sensor Module; several sensors can share one bus, each with its own 64-bit address.

References​

Wiring​

Connect the sensor with three wires. The data line is always on channel B; the power terminal depends on the revision of the Sensor Module:

DS18B20 wireSensor Module R1.1Sensor Module R1.0
Red (VDD)VDDA
Black (GND)GNDGND
Yellow or white (DATA)BB

The SDK detects the revision of the Sensor Module and powers the sensors itself: on R1.1 through the VDD terminal, on R1.0 through the 56 Ω pull-up of channel A. The data line gets the 4.7 kΩ pull-up. The Soil Sensor uses the same wiring, and its page shows it on both revisions.

Sensor Module R1.0 reliably powers only two sensors; R1.1 handles many more. For long cables and larger buses, see the wiring recommendations.

caution

Parasitic power (a two-wire connection without VDD) is not supported. Always connect VDD.

One sensor​

Single Sensor Code Example

#include <application.h>

static twr_ds18b20_t ds18b20;

void ds18b20_event_handler(twr_ds18b20_t *self, uint64_t device_address, twr_ds18b20_event_t event, void *param)
{
(void) param;

float temperature = NAN;

if (event == TWR_DS18B20_EVENT_UPDATE)
{
twr_ds18b20_get_temperature_celsius(self, device_address, &temperature);
twr_log_debug("UPDATE %" PRIx64 " = %0.2f", device_address, temperature);
}
else
{
twr_log_error("%" PRIx64, device_address);
}
}

void application_init(void)
{
twr_log_init(TWR_LOG_LEVEL_DEBUG, TWR_LOG_TIMESTAMP_ABS);

twr_ds18b20_init_single(&ds18b20, TWR_DS18B20_RESOLUTION_BITS_12);
twr_ds18b20_set_event_handler(&ds18b20, ds18b20_event_handler, NULL);
twr_ds18b20_set_update_interval(&ds18b20, 5 * 1000);
}

More sensors​

For more sensors, prepare an array for them and initialize the driver with twr_ds18b20_init_multiple(). In the event handler, device_address tells which sensor the value comes from, and twr_ds18b20_get_index_by_device_address() gives its index in the array. Print the 64-bit address with the PRIx64 format.

info

The SDK searches the bus only after the application has started, so twr_ds18b20_get_sensor_found() returns the number of sensors found from the first measurement on, not in application_init(). To search the bus again, for example after connecting another sensor, call twr_ds18b20_rescan().

Multiple Sensors Code Example

#include <application.h>

#define DS18B20_SENSOR_COUNT 10

static twr_ds18b20_t ds18b20;
static twr_ds18b20_sensor_t ds18b20_sensors[DS18B20_SENSOR_COUNT];

void ds18b20_event_handler(twr_ds18b20_t *self, uint64_t device_address, twr_ds18b20_event_t event, void *param)
{
(void) param;

float temperature = NAN;

if (event == TWR_DS18B20_EVENT_UPDATE)
{
twr_ds18b20_get_temperature_celsius(self, device_address, &temperature);

int index = twr_ds18b20_get_index_by_device_address(self, device_address);

twr_log_debug("UPDATE %" PRIx64 " (%d) = %0.2f", device_address, index, temperature);
}
else
{
twr_log_error("%" PRIx64, device_address);
}
}

void application_init(void)
{
twr_log_init(TWR_LOG_LEVEL_DEBUG, TWR_LOG_TIMESTAMP_ABS);

twr_ds18b20_init_multiple(&ds18b20, ds18b20_sensors, DS18B20_SENSOR_COUNT, TWR_DS18B20_RESOLUTION_BITS_12);
twr_ds18b20_set_event_handler(&ds18b20, ds18b20_event_handler, NULL);
twr_ds18b20_set_update_interval(&ds18b20, 5 * 1000);
}

Power consumption​

By default, the SDK keeps the sensors powered all the time. On a battery-powered node, twr_ds18b20_set_power_dynamic(&ds18b20, true) turns VDD and the 4.7 kΩ pull-up on only for the measurement. On a long bus with many sensors, keep the power on: a stable supply makes the bus more reliable.

Troubleshooting​

  • Only TWR_DS18B20_EVENT_ERROR, never an update: check the wiring against the table above, measure the voltage between the power terminal (VDD, or A on R1.0) and GND, and try one sensor alone.
  • Update the SDK. The 1-Wire driver of newer SDK versions also works with DS18B20 clones that do not keep the timing of the original exactly. In a project based on the TOWER skeleton, run git submodule update --remote sdk.
  • A single sensor can be faulty. If a sensor does not answer even alone on the bus, try another one; cheap clones are not always reliable.
  • The bus itself: with an oscilloscope or a logic analyzer, check that the sensor answers the reset pulse of the master with a presence pulse.