DS18B20 DS18B20

An inexpensive and popular temperature sensor that uses the 1-Wire bus. Devices have a unique and many can share a single data wire.

🛒

Get Bus Pirate & Accessories

See it in action

Connections

Bus PirateDS18B20Description
OWDOWD/Q1-Wire Data
Vout/VrefVDD3.3volt power supply
GNDGNDGround

Setup

Bus Pirate [/dev/ttyS0]
HiZ> m 1wire

Mode: 1WIRE
1WIRE> W
Power supply
Volts (0.80V-5.00V)
x to exit (3.30) > 3.3
Maximum current (1mA-500mA), 0 for unlimited
x to exit (300.00) > 100
3.30V requested, closest value: 3.30V
100.0mA requested, closest value: 100.0mA

Power supply:Enabled
Vreg output: 3.3V, Vref/Vout pin: 3.3V, Current: 2.3mA

1WIRE> P
Pull-up resistors: Enabled (10K ohms @ 3.3V)

1WIRE> 
  • Use the m mode command and select 1-Wire, or m 1wire to skip the menu.
  • Enable the onboard power supply with the W command, and configure it for 3.3volts output and 100mA current limit.
  • Enable the onboard pull-up resistors with the P command.

Search for 1-Wire devices

Bus Pirate [/dev/ttyS0]
1WIRE> scan
1-Wire ROM search:
1: 28 5c aa 13 0a 00 00 19 (DS18B20 digital thermometer)

1WIRE> 
  • scan - search for attached 1-Wire devices.

Each 1-Wire device has a unique 64bit ID. The first byte (28) is a family code that can be used to identify the device, in this case a DS18B20. The next 6 bytes are the unique ID, and the last byte is a CRC checksum.

Configure

BIT 7BIT 6BIT 5BIT 4BIT 3BIT 2BIT 1BIT 0
0R1R011111

The DS18B20 configuration register is one byte (8 bits) wide. Bits 6 and 5 (R1 and R0) set the temperature measurement resolution.

R1R0RESOLUTION (bits)CONVERSION TIME
00993.75ms (tCONV/8)
0110187.5ms (tCONV/4)
1011375ms (tCONV/2)
1112750ms (tCONV)

First, we need to configure the DS18B20.

  • Bits 5 and 6 of the configuration register set the temperature measurement resolution.
  • Higher resolution measurements take longer.
  • The other bits are used internally and should be set to the default values shown.
Bus Pirate [/dev/ttyS0]
1WIRE> [ 0xcc 0x4e 0x00 0x00 0b01111111

1-Wire RESET
TX: 0xCC 0x4E 0x00 0x00 
TX: 0b01111111 
1WIRE> 

1-Wire devices can be addressed individually by their unique ID number. This is kind of tedious. The 0xCC skip ROM command can be used to address all devices without entering the ID. This is useful for configuring multiple devices at once, or triggering temperature measurement on multiple devices.

However, using the skip ROM command to read (e.g. temperature) when multiple devices are connected will result in garbage data. This walk through uses the skip ROM command for all operations and assumes a single device is connected.

  • [ issues a 1-Wire reset and detects the device presence. 1-Wire transactions begin with a reset, during which connected devices pull low to indicate presence.
  • 0xcc is the 1-Wire skip ROM command. This accesses all connected devices without using the unique ID.
  • 0x4e is the DS18B20 write “scratchpad” command. Scratchpad is the annoying 1-Wire way of saying configuration registers.
  • 0x00 0x00 the next two bytes program the high temperature alarm level. When a temperature measurement is triggered the DS18B20 will generate an alarm if the value is greater than this number. Useful in conjunction with the alarm search command on very large networks, but we just set it to 0 for this demo.
  • 0b01111111 Bits 5 and 6 of the configuration register set the measurement resolution. We’ve set them to 1 for 12 bit measurements. The other bits are reserved and set according to the defaults in the table above.

Measure temperature

Bus Pirate [/dev/ttyS0]
1WIRE> [ 0xcc 0x44 D:800

1-Wire RESET
TX: 0xCC 0x44 
Delay: 800ms
1WIRE> 

Now it’s time to trigger a temperature measurement.

  • [ 1-Wire transactions begin with a 1-Wire reset and device presence detection.
  • 0xcc is the 1-Wire skip ROM command, which addresses all devices without using their unique ID.
  • 0x44 is the DS18B20 “convert T” command, which is the annoying 1-Wire way to say measure temperature.
  • D:800 temperature measurements take time, depending on the resolution up to 750ms. We’ll delay 800ms to be safe. The delay probably isn’t needed if you enter each line one by one, but if every step is done on a single line then the measurement won’t be ready without the delay.

Read temperature

Bus Pirate [/dev/ttyS0]
1WIRE> [ 0xcc 0xbe r:9

1-Wire RESET
TX: 0xCC 0xBE 
RX: 0x7B 0x01 0x00 0x00 0x7F 0xFF 0x05 0x10 
    0xE5 
1WIRE> 

Finally, we can read out the temperature measurement.

  • [ begin with a 1-Wire reset and device presence check.
  • 0xcc send the 1-Wire skip ROM command. This step reads data back from the device, so if more than a single device is connected the read will be corrupted.
  • 0xbe is the DS18B20 read data command.
  • r:9 read 9 bytes of data from the DS18B20.

The first two bytes are the temperature value (0x1d 0x01). The next two bytes are the high temperature alarm we set during configuration (0x00 0x00). Next is the configuration register showing 12 bit measurements (0x7F). The next three bytes are reserved, and the final byte is a CRC for error detection.

Calculate temperature

Bus Pirate [/dev/ttyS0]
1WIRE> = 0x017b
 =0x017B.16 =379.16 =0b0000000101111011.16
1WIRE> 

The two bytes of temperature data are the low (0x7b) and high (0x01) bits of the 12bit temperature measurement.

  • Combine the temperature bytes into a single number (0x017b).
  • Use the = convert format command to find the decimal equivalent of this hex value (379).
  • Divide the value by 16 to find the temperature (23.6 degrees).

All in one line

Bus Pirate [/dev/ttyS0]
1WIRE> [ 0xcc 0x4e 0x00 0x00 0b01111111 [ 0xcc 0x44 D:800 [0xcc 0xbe r:9

1-Wire RESET
TX: 0xCC 0x4E 0x00 0x00 
TX: 0b01111111 
1-Wire RESET
TX: 0xCC 0x44 
Delay: 800ms
1-Wire RESET
TX: 0xCC 0xBE 
RX: 0x7B 0x01 0x00 0x00 0x7F 0xFF 0x05 0x10 
    0xE5 
1WIRE> 

All three steps can be done with a single line, as long as there is sufficient delay between triggering the measurement and reading the result.

R1R0RESOLUTION (bits)CONVERSION TIME
1112750ms (tCONV)

Recall the resolution vs conversion time in the table above. A 12bit temperature measurement takes 750ms.

  • D:800 - add a delay of 800ms to allow the measurement to complete before reading the result.

ds18b20 command

Bus Pirate [/dev/ttyS0]
1WIRE> ds18b20
RX: 7b 01 00 00 7f ff 05 10 e5
Temperature: 23.688

1WIRE> 

1-Wire mode command ds18b20 reads the temperature from a single 18B20 temperature sensor. The temperature is displayed in Celsius.

Get a Bus Pirate

🛒

Get Bus Pirate & Accessories

Community