© Warren Gay 2018
Warren GayAdvanced Raspberry Pihttps://doi.org/10.1007/978-1-4842-3948-3_14

14. 1-Wire Driver

Warren Gay1 
(1)
St. Catharine’s, Ontario, Canada
 

The 1-Wire protocol was developed by Dallas Semiconductor Corp. initially for the iButton.15 This communication protocol was attractive enough to be applied to other devices and soon adopted by other manufacturers. This chapter provides an overview of the 1-Wire protocol and how it is supported in the Raspberry Pi.

1-Wire Line and Power

The 1-Wire protocol actually uses two wires, but the ground wire is not counted:
  • Data: The single wire used for data communication

  • Ground: The ground or “return” wire

The 1-Wire protocol was designed for communication with low-data content devices like temperature sensors. It provides for low-cost remote sensing by supplying power over the same wire used for data communications. Each sensor can accept power from the data line while the data line is in the high state (which is also the line’s idle state). The small amount of power that is siphoned off charges the chip’s internal capacitor (usually about 800 pF).15

When the data line is active (going low), the sensor chips continue to run off of their internal capacitor (in parasitic mode). Data communications cause the data line to fluctuate between low and high. Whenever the line level returns high again, even for a brief instant, the capacitor recharges.

The device also provides an optional VDD pin, allowing power to be supplied to it directly. This is sometimes used when parasitic mode doesn’t work well enough. This, of course, requires an additional wire, adding to the cost. This chapter will focus on the parasitic mode where VDD is connected to the ground.

Line Driving

The data line is driven by open drain transistors in the master and slave devices. The line is held high by a pull-up resistor when the transistors are all in the Off state. To initiate a signal, one transistor turns on and pulls the line down to ground potential.

Figure 14-1 shows a simplified schematic of the master (GPIO) attached to the bus. Some voltage V (typically, +5 V) is applied to the 1-Wire bus through the pull-up resistor Rpullup. When the open drain transistor M2 is in the Off state, the voltage on the bus remains high because of the pull-up resistor. When the master device activates transistor M2, current will flow from the bus to the ground, acting like a signal short-circuit. Slave devices attached to the bus will see a voltage near zero.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig1_HTML.jpg
Figure 14-1

1-Wire driver circuit

Likewise, when a slave is signaled to respond, the master listens to the bus while the slave activates its driving transistor. Whenever all driving transistors are off, the bus returns to the high idle state.

The master can request that all slave devices reset. After the master has made this request, it relinquishes the bus and allows it to return high. All slave devices that are connected to the bus respond by bringing the line low after a short pause. Multiple slaves will bring the line low at the same time, but this is permitted. This informs the master that at least one slave device is attached to the bus. Additionally, this procedure puts all slave devices into a known reset state.

Master and Slave

The master device is always in control of the 1-Wire bus. Slaves speak only to the master when requested. There is never slave-to-slave device communication.

If the master finds that communication becomes difficult for some reason, it may force a bus reset. This corrects for an errant slave device that might be jabbering on the line.

Protocol

This section presents an introduction to the 1-Wire communication protocol. Knowing something about how the signaling works is not only interesting but may be helpful for troubleshooting. More information is available on the Internet.16

Reset

Figure 14-2 provides a simplified timing diagram of the reset procedure for the 1-Wire protocol. When the master driver begins, it resets the 1-Wire bus to put all the slave devices into a known state.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig2_HTML.jpg
Figure 14-2

 1-Wire reset protocol

For reset, the bus is brought low and held there for approximately 480 μs. Then the bus is released, and the pull-up resistor brings it high again. After a short time, slave devices connected to the bus start responding by bringing the line low and holding it for a time. Several slaves can participate in this at the same time. The master samples the bus at around 70 μs after it releases the bus. If it finds the line low, it knows that there is at least one slave connected and responding.

Soon after the master sampling point, all slaves release the bus again and go into a listening state. They do not respond again until the master specifically addresses a slave device. For simplicity, we’ll omit the discovery protocol used.

Note

Each slave has a guaranteed unique address.

Data I/O

The data protocol is shown in Figure 14-3. Whether writing a 0 or 1 bit, the sending device brings the bus line low. This announces the start of a data bit.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig3_HTML.jpg
Figure 14-3

1-Wire read/write of 0 data bit

When a 0 is being transmitted, the line is held low for approximately 60 μs. Then the bus is released and allowed to return high. When a 1 bit is being transmitted, the line is held low for only about 6 μs before releasing the bus. Another data bit is not begun until 70 μs after the start of the previous bit. This leaves a guard time of 10 μs between bits. The receiver then has ample time to process the bit and gain some signal noise immunity.

The receiver notices a data bit is coming when the line drops low. It then starts a timer and samples the bus at approximately 15 μs. If the bus is still in the low state, a 0 data bit is registered. Otherwise, the data bit is interpreted as a 1. Having registered a data bit, the receiver then waits further until the line returns high (in the case of a 0 bit).

The receiver remains idle until it notices the line going low again, announcing the start of the next bit.

The sender can be either the master or the slave, but the master always controls who can speak next. Slaves do not write to the bus unless the master requested it.

Slave Support

Table 14-1 lists the slave devices that are supported by Raspbian Linux. The module names listed are found in the kernel source directory arch/arm/machbcm2708/slave.
Table 14-1

1-Wire Slave Driver Support

Device

Module

Description

DS18S20

w1_therm.c

Precision digital thermometer

DS18B20

Programmable resolution thermometer

DS1822

Econo digital thermometer

DS28EA00

9- to 12-bit digital thermometer with PIO

bq27000

w1_bq27000.c

Highly accurate battery monitor

DS2408

w1_ds2408.c

Eight-channel addressable switch

DS2423

w1_ds2423.c

4 KB RAM with counter

DS2431

w1_ds2431.c

1 KB EEPROM

DS2433

w1_ds2433.c

4 KB EEPROM

DS2760

w1_ds2760.c

Precision Li+ battery monitor

DS2780

w1_ds2780.c

Stand-alone fuel gauge

Configuration

With the advent of the Linux device tree, it is now necessary to configure access for the 1-Wire driver. Edit file /boot/config.txt and add the following line:
dtoverlay=w1-gpio,gpiopin=4,pullup=on

The parameter gpiopin=4 specifies that the 1-Wire bus is on GPIO4. This used to be hard-coded in the driver but now permits you to choose differently. It is still the default if the parameter is not specified.

Parameter pullup=on is normally required for successful operation. Even though I had attached a 4.7 kohm resistor to +3.3 V for the bus, I could not get my devices to operate in parasitic mode. I recommend that you provide this parameter. Once you have edited the file, reboot for it to take effect.

There is some interesting documentation available in the /boot directory:
$ less /boot/overlays/README
...
Name:   w1-gpio
Info:   Configures the w1-gpio Onewire interface module.
        Use this overlay if you *don't* need a GPIO to drive an external
        pullup.
Load:   dtoverlay=w1-gpio,<param>=<val>
Params: gpiopin        GPIO for I/O (default "4")
        pullup         Non-zero, "on", or "y" to enable the parasitic
                       power (2-wire, power-on-data) feature

The referenced README file also contains an entry named w1-gpio-pullup, which you should probably avoid unless you know why you are using it. It will require one additional GPIO to be used to pull up the bus (by default GPIO5).

Reading Temperature

The support for the usual temperature sensors is found in the kernel module w1_therm. When you first boot your Raspbian Linux, that module may not be loaded. You can check for it with the lsmod command (root not required for listing):
$ lsmod
Module             Size   Used by
snd_bcm2835       12808   1
snd_pcm           74834   1 snd_bcm2835
snd_seq           52536   0
...
The module w1_therm depends on another driver module named wire. To verify if the driver modules are loaded, check the pseudo file system:
$ ls –l /sys/bus/w1
ls: cannot access /sys/bus/w1 : No such file or directory

Having not found the pathname /sys/bus/w1, we have confirmation that the device driver is not loaded.

Loading module w1_therm will bring in most of its module dependents:
$ sudo modprobe w1_therm
$ lsmod
Module                 Size   Used by
w1_therm               2705   0
wire                  23530   1 w1_therm
cn                     4649   1 wire
snd_bcm2835           12808   1
snd_pcm               74834   1 snd_bcm2835
...
After the wire module is loaded, you’ll see the /sys/bus/w1/devices directory. One more module is needed:
$ sudo modprobe w1_gpio
$ lsmod
Module                   Size   Used by
w1_gpio                  1283   0
w1_therm                 2705   0
wire                    23530   2 w1_therm,w1_gpio
cn                       4649   1 wire
snd_bcm2835             12808   1
...
$ cd /sys/bus/w1/devices
$ ls
w1_bus_master1
Once module w1_gpio is loaded, there is a bus master driver for the configured GPIO port. The bus master makes its presence known by creating symlink devices/w1_bus_master1. Change to the /sys/bus/w1 directory and list it to see the associated pseudo files within it. The long lines have been abbreviated:
# pwd
/sys/bus/w1
# ls -lR .
.:
total 0
drwxr-xr-x 2 root root    0 Jul  6 06:47 devices
drwxr-xr-x 4 root root    0 Jul  6 06:47 drivers
-rw-r--r-- 1 root root 4096 Jul  6 06:47 drivers_autoprobe
--w------- 1 root root 4096 Jul  6 06:47 drivers_probe
--w------- 1 root root 4096 Jul  6 06:47 uevent
./devices:
total 0
lrwxrwxrwx 1 root root 0 Jul  6 06:47 28-00000478d75e -> ...
lrwxrwxrwx 1 root root 0 Jul  6 06:47 28-0000047931b5 -> ...
lrwxrwxrwx 1 root root 0 Jul  6 06:47 w1_bus_master1 -> ...
./drivers:
total 0
drwxr-xr-x 2 root root 0 Jul  6 06:47 w1_master_driver
drwxr-xr-x 2 root root 0 Jul  6 06:47 w1_slave_driver
./drivers/w1_master_driver:
total 0
--w------- 1 root root 4096 Jul  6 06:47 bind
--w------- 1 root root 4096 Jul  6 06:47 uevent
--w------- 1 root root 4096 Jul  6 06:47 unbind
lrwxrwxrwx 1 root root    0 Jul  6 06:47 w1_bus_master1 -> ...
./drivers/w1_slave_driver:
total 0
lrwxrwxrwx 1 root root    0 Jul  6 06:47 28-00000478d75e -> ...
lrwxrwxrwx 1 root root    0 Jul  6 06:47 28-0000047931b5 -> ...
--w------- 1 root root 4096 Jul  6 06:47 bind
--w------- 1 root root 4096 Jul  6 06:47 uevent
--w------- 1 root root 4096 Jul  6 06:47 unbind

The pseudo file names 28-00000478d75e and 28-0000047931b5 are device entries for the author’s two DS18B20 devices. Don’t worry if you don’t see your entries immediately, since it takes time for the discovery protocol to find them.

Slave Devices

Figure 14-4 shows the pin-out of the Dallas DS18B20 slave device. This temperature sensor is typical of many 1-wire slave devices.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig4_HTML.jpg
Figure 14-4

DS18B20 pin-out

Slave devices are identified by a pair of digits representing the product family, followed by a hyphen and serial number in hexadecimal. The ID 28-00000478d75e is an example. You might also want to try different devices, like the similar DS18S20. Figure 14-5 illustrates the DS18B20 attached to the Raspberry Pi GPIO.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig5_HTML.jpg
Figure 14-5

1-Wire with DS18B20 slave circuit, using VCC=3.3 V and 4.7 k pull-up resistor

When things are working correctly, the bus master detects slave devices automatically as part of its periodic scan. When your device(s) are discovered, they will appear in the devices subdirectory with names like 28-0000028f6667.

The following example shows how two DS18B20 temperature sensors show up on the 1-Wire bus:
$ cd /sys/bus/w1/devices
$ ls
28−00000478d75e 28−0000047931b5 w1_bus_master1
$
Figure 14-6 illustrates the breadboard setup used by the author.
../images/326071_2_En_14_Chapter/326071_2_En_14_Fig6_HTML.jpg
Figure 14-6

The breadboard with two DS18B20 temperature sensors wired to the Raspberry Pi

Reading the Temperature

The slave device’s temperature can be read by reading its w1_slave pseudo file. In this example, we read two DS18B20 temperature sensors that are supposed to be accurate to ±0.5 °C. Reading these two sensors together should show fairly good agreement (they were in close proximity of each other):
# cd /sys/bus/w1/devices
# cat 28-00000478d75e/w1_slave
a6 01 4b 46 7f ff 0a 10 f6 : crc=f6 YES
a6 01 4b 46 7f ff 0a 10 f6 t=26375

The second line ending in t=26375 indicates a reading of 26.375°C.

If the driver is experiencing problems reading from the device, the DS18B20 response may look like this:
# cd /sys/bus/w1/devices
# cat 28-00000478d75e/w1_slave
50 05 4b 46 7f ff 0c 10 1c : crc=1c YES
50 05 4b 46 7f ff 0c 10 1c t=85000

The value t=85000 is the dead giveaway. If you see this, check your wiring—particularly the pullup resistor. The circuit needs a 4.7 kohm pull-up resistor to +3.3 V.

Summary

In this chapter, the 1-Wire Linux support was put to use to read the Dallas Semiconductor DS18B20 temperature sensor. Table 14-1 lists several other types of 1-wire sensors that you may be able to use. Having driver support makes using sensors like these a breeze.