DS18B20 1-Wire Temperature Sensor
An inexpensive and popular temperature sensor that uses the 1-Wire bus. Devices have a unique and many can share a single data wire.
Connections
Bus Pirate | DS18B20 | Description |
---|---|---|
OWD | OWD/Q | 1-Wire Data |
Vout/Vref | VDD | 3.3volt power supply |
GND | GND | Ground |
Setup
Mode selection
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. LED
x. Exit
Mode > 2
Mode: 1-WIRE
1-WIRE> W
Power supply
Volts (0.80V-5.00V)
x to exit (3.30) >
3.30V requested, closest value: 3.30V
Set current limit? n
Power supply:Enabled
Vreg output: 3.3V, Vref/Vout pin: 3.3V, Current sense: 4.3mA
1-WIRE> P
Pull-up resistors: Enabled (10K ohms @ 3.2V)
1-WIRE>
- Use the
m
mode command and select 1-Wire - Enable the onboard power supply with the
W
command, and configure it for 3.3volts output. - Enable the onboard pull-up resistors with the
P
command.
Search for 1-Wire devices
1-Wire ROM search:
1: 28 5c aa 13 0a 00 00 19 (DS18B20 digital thermometer)
1-WIRE>
1-Wire devices have a unique 64bit ID that includes a family code for identifying the device. 1-Wire macro (1)
searches for all 1-Wire devices currently connected.
Configure
BIT 7 | BIT 6 | BIT 5 | BIT 4 | BIT 3 | BIT 2 | BIT 1 | BIT 0 |
---|---|---|---|---|---|---|---|
0 | R1 | R0 | 1 | 1 | 1 | 1 | 1 |
R1 | R0 | RESOLUTION (bits) | CONVERSION TIME |
---|---|---|---|
0 | 0 | 9 | 93.75ms (tCONV/8) |
0 | 1 | 10 | 187.5ms (tCONV/4) |
1 | 0 | 11 | 375ms (tCONV/2) |
1 | 1 | 12 | 750ms (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.
1-Wire RESET
TX: 0xCC 0x4E 0x00 0x00 0b01111111
1-WIRE>
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.
- 1-Wire transactions begin with a reset, during which connected devices pull low to indicate presence.
[
issues a 1-Wire reset and detects the device 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.
Skip ROM is useful for writing data to multiple devices, but will result in garbage data if used to read data and more than one device is connected.
Measure temperature
1-Wire RESET
TX: 0xCC 0x44
Delay: 800ms
1-WIRE> [ 0xcc 0xbe r:9
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
1-Wire RESET
TX: 0xCC 0xBE
RX: 0x1D 0x01 0x00 0x00 0x7F 0xFF 0x03 0x10
0x3D
1-WIRE>
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
=0x011D.16 =285.16 =0b0000000100011101.16
1-WIRE>
The two bytes of temperature data are the low (0x1d) and high (0x01) bits of the 12bit temperature measurement.
- Combine the temperature bytes into a single number (0x011d).
- Use the
=
convert format command to find the decimal equivalent of this hex value (285). - Divide the value by 16 to find the temperature (17.8125 degrees).
All in one line
1-Wire RESET
TX: 0xCC 0x4E 0x00 0x00 0x7F
1-Wire RESET
TX: 0xCC 0x44
Delay: 800ms
1-Wire RESET
TX: 0xCC 0xBE
RX: 0x1D 0x01 0x00 0x00 0x7F 0xFF 0x03 0x10
0x3D
1-WIRE>
All three steps can be done with a single line. The delay is key here. Without a delay, the temperature measurement will still be in progress when we try to read from the device.
Macro
RX: 1d 01 00 00 7f ff 03 10 3d
Temperature: 17.812
1-WIRE>
Macro (2)
in 1-Wire mode configures a DS18B20 and performs a temperature measurement.