Raspberry Pi 1-wire

The 1-wire interface uses a single data line for communicating with some attached device. There are several different devices available, such as IO-ports, memory chips, battery supervisory circuits and thermometers.

The discussion here is for the quite common case of connecting a DS18B20 thermometer. In addition to the rather simple hardware interfacing, much of the hard work in software is already done, and so this becomes about as close to plug-and-play as it gets.

Hardware - DS18B20

Now, there are i2c thermometer chips, and SPI thermometer chips, and analog ones -- but they all have their pros and cons. These 1-wire ones resemble the analog sensors in that there is only power, ground, and one wire for the data payload, but the signal on this line is of course very different from the simple analog voltage coming out of an LM35 or TMP036.

Power and ground are common for all devices. If connecting more than one, they all share the data line, much the same way as the SDA line of the i2c bus. And like SDA (and SCL) of i2c, there is a pull-up resistor required as well.

The DS18B20 here is packaged in a waterproof assembly, with a 3-core cable connected, so the connections are:

RP.P1#R-Pi nameDS18B20 name color
1 3.3V VCC Red
6 0V GND Black
7 GPIO4/1wire Data White

Then there is a 4.7 kOhm resistor for pulling up the data line, connected between 3.3V and Data (pins 1 and 7, or red and white core).

Software DS18B20

To enable this, the Raspbian has the following modules available:

w1-gpio
w1-therm

They can be added in /etc/modules along with any i2c or spi ones that might be in there. w1-therm is specific to thermometer devices, others will require different ones. Look under /lib/modules/(version)/kernel/drivers/w1/slaves for available modules for other out-of-the-box supported devices.

Once this is done, there will be some directories under /sys/bus/w1/devices:

w1_bus_master1
28-00000605c294

The directory with the long numbers, here it is 28-00000605c294, refers to the factory-set device identifier of the connected DS18B20. If several 1-wire devices are connected, each one will show up with a different directory here, similarly identified by the device.

The file w1_slave in this directory can be read and returns information:

8c 01 4b 46 7f ff 04 10 2e : crc=2e YES
8c 01 4b 46 7f ff 04 10 2e t=24750

The number after the t= is the temperature measured, reported in 1/1000s of degrees C. Real accuracy is on the order of 0.5 degree however, so don't be fooled by the exaggerated resolution.