Parts: 1-Wire temperature sensor (DS1822)
posted Dec 10th 2008 5:35pm by Ianfiled under: home hacks, parts, tool hacks

Download: buspirate.v0d.zip
Dallas/Maxim’s 1-Wire protocol is the most requested addition to the Bus Pirate. We finally got some 1-Wire parts, and today we’ll demonstrate the DS1822 1-Wire digital thermometer. Grab the datasheet (PDF) and follow along.
This post is accompanied by release v.0d of the Bus Pirate firmware for hardware version 0. This includes the new 1-Wire protocol library, more configuration options, and other improvements.
DS1822 Economy Digital Thermometer (Digikey #DS1822+-ND, $3.87) We found a footprint in the 1-wire library for Eagle on the Cadsoft download page.
The 1-Wire protocol uses a single wire for data transfer, and sometimes power. Data is transferred in time-sensitive ‘slots’ because there isn’t a separate clock to delineate bit periods.

|
Bus Pirate
|
DS1822 |
|
SDA
|
DQ |
| +5volts | Vdd |
|
Ground
|
GND |
The DS1822 connections are shown in the table. We used the Bus Pirate’s 5volt supply to power the DS1822, but it also works at 3.3volts. A resistor (R1, ~5K) holds the bus high.
All 1-Wire commands start with a reset procedure, followed by one of five ROM commands.
| Command | Description |
| 0×33 | READ ROM. Read single device address. |
| 0×55 | MATCH ROM. Match device address, followed by 64bit address. |
| 0xCC | SKIP ROM. Address all devices together. |
| 0xEC | ALARM SEARCH. Search for alarm condition. |
| 0xF0 | SEARCH ROM. Part of address enumeration procedure. |
ROM commands are described on page 10 of the datasheet. All ROM commands are available as macros in the Bus Pirate 1-Wire library, see (0) for a menu. ROM command macros include the 1-Wire bus reset procedure.
Single device

All 1-Wire devices have a unique 64bit (8 byte) address, and some 1-Wire devices are used exclusively to give electronics a unique tracking number. When a single device is connected to a 1-Wire bus, the READ ROM command will extract its address.
1-WIRE>{ 0×33 r:8 <–command
xxx 1WIRE BUS RESET OK
xxx 1WIRE WRITE: 0×33 <–READ ROM
xxx 1WIRE BULK READ, 0×08 BYTES:
0×22 0×47 0×45 0×22 0×00 0×00 0×00 0×29 <–ID#
1-WIRE>
The command sends a bus reset ({), the READ ROM command (0×33), and reads the 64bit address (r:8, 8 bytes *8bits/byte=64bits).
The first byte (0×22) identifies this as a DS1822 thermometer. The next 6 bytes are unique to this device, and the final byte is a CRC of the previous 7 bytes.
Now we can address the device with the MATCH ROM command and send it further instructions.
1-WIRE>{ 0×55 0×22 0×47 0×45 0×22 0×00 0×00 0×00 0×29 0×44
xxx 1WIRE BUS RESET OK
xxx 1WIRE WRITE: 0×55<–MATCH ROM command
xxx 1WIRE WRITE: 0×22<–start address
xxx 1WIRE WRITE: 0×47
xxx 1WIRE WRITE: 0×45
xxx 1WIRE WRITE: 0×22
xxx 1WIRE WRITE: 0×00
xxx 1WIRE WRITE: 0×00
xxx 1WIRE WRITE: 0×00
xxx 1WIRE WRITE: 0×29
xxx 1WIRE WRITE: 0×44 <–start conversion
1-WIRE>
First, we send the MATCH ROM command (0×55) and the device address (8 bytes). Next is the CONVERT T command (0×44, datasheet page 11) that starts the temperature conversion.
A second command sequence retrieves the temperature reading from the DS1822.
1-WIRE>{ 0×55 0×22 0×47 0×45 0×22 0×00 0×00 0×00 0×29 0xbe r:9
xxx 1WIRE BUS RESET OK
xxx 1WIRE WRITE: 0×55
xxx 1WIRE WRITE: 0×22
…long 1-Wire address…
xxx 1WIRE WRITE: 0×29
xxx 1WIRE WRITE: 0xBE <–read scratchpad command
xxx 1WIRE BULK READ, 0×09 BYTES:
0×71 0×01 0xFF 0×00 0x7F 0xFF 0x0F 0×10 0xF8
1-WIRE>
The READ SCRATCHPAD command (0xBE, datasheet page 11) returns 9 bytes. We only care about the first two bytes, the rest can be decoded according the the table on page 7 of the datasheet. Temperature is calculated according to page 4 of the datasheet: 0×0171 HEX=369 DEC, 369*0.0625=23C (74F).
Multiple devices

When multiple 1-Wire devices share a bus it’s more difficult to determine all the addresses. The fastest way to find attached devices is with the SEARCH ROM command (0xF0) and a binary branching procedure. The Bus Pirate automates this with macro (240).
1-WIRE>(240) <–macro 240
xxx 1WIRE ROM COMMAND: SEARCH (0xF0)
Found devices at:
Macro 1-WIRE address
1.0×22 0×50 0×28 0×22 0×00 0×00 0×00 0x0A <–address
*DS1822 Econ Dig Therm <–type according to family code
2.0×22 0xD0 0xC7 0x1A 0×00 0×00 0×00 0×01
*DS1822 Econ Dig Therm
3.0×22 0×47 0×45 0×22 0×00 0×00 0×00 0×29
*DS1822 Econ Dig Therm
Found 0×03 devices.
The first 10 device IDs are available by MACRO, see (0).
1-WIRE>
The SEARCH ROM command shows the devices it found, and the type according to the family code.
We think typing 8 byte 1-Wire addresses is really tedious, so the first 10 device addresses are stored in memory and can be accessed with the macros (1)…(10). A buffer for up to 50 device addresses can be defined in the 1-Wire library at compile time. Ideally, this data will be stored in a global scratch buffer shared by all modules in a future firmware update.
1-WIRE>(0) <–show macro list
0.Macro menu
Macro 1-WIRE address <–enumerated device addresses
1.0×22 0×50 0×28 0×22 0×00 0×00 0×00 0x0A
*DS1822 Econ Dig Therm
2.0×22 0xD0 0xC7 0x1A 0×00 0×00 0×00 0×01
*DS1822 Econ Dig Therm
3.0×22 0×47 0×45 0×22 0×00 0×00 0×00 0×29
*DS1822 Econ Dig Therm
1-WIRE ROM COMMAND MACROs:<–normal commands
51.READ ROM (0×33) *for single device bus
85.MATCH ROM (0×55) *followed by 64bit address
204.SKIP ROM (0xCC) *followed by command
236.ALARM SEARCH (0xEC)
240.SEARCH ROM (0xF0)
1-WIRE>
The macro menu (0) will also include the device addresses stored in the roster. Now we can just address devices by macro, rather than typing the whole 64bit address every time.
1-WIRE>(85) (1) 0×44 <–start conversion
xxx 1WIRE BUS RESET OK
xxx 1WIRE WRITE ROM COMMAND: MATCH (0×55) *follow with 64bit address
xxx 1WIRE ADDRESS MACRO 1: 0×22 0×50 0×28 0×22 0×00 0×00 0×00 0x0A
xxx 1WIRE WRITE: 0×44
1-WIRE>(85) (1) 0xbe r:9 <–fetch reading
xxx 1WIRE BUS RESET OK
xxx 1WIRE WRITE ROM COMMAND: MATCH (0×55) *follow with 64bit address
xxx 1WIRE ADDRESS MACRO 1: 0×22 0×50 0×28 0×22 0×00 0×00 0×00 0x0A
xxx 1WIRE WRITE: 0xBE
xxx 1WIRE BULK READ, 0×09 BYTES:
0×81 0×01 0x4B 0×46 0x7F 0xFF 0x0F 0×10 0×71
1-WIRE>
(85) is a shortcut for a bus reset and MATCH ROM command. (1) is the device address macro, and 0×44 is the command to begin a temperature conversion. Retrieving the reading involves the same macros, but substitutes the command to read the device (0xBE) and grabs 9 bytes (r:9). The temperature is 0×0181, or 24C next to the PC fan.
Taking it further
We used the Bus Pirate to give a visual demonstration of the 1-Wire protocol, but the real challenge is integrating it into your own design. Maxim provides example code, Microchip has an app note (PDF), and you can check out the example code we used.
Firmware download: buspirate.vod.zip






Awesome. Thanks for sharing the knowledge!