How to use the 1-WIRE bus

How to manage a Maxim DS18B20 thermal sensor

1-Wire is a device communications bus system designed by Dallas Semiconductor that provides low-speed data, signaling, and power over a single signal. 1-Wire is similar in concept to I2C, but with lower data rates and longer range. It is typically used to communicate with small inexpensive devices such as digital thermometers and weather instruments.

Dallas/Maxim DS18B20 thermal sensor

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements. It communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor.

It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of °10°C to +85°C. (read more on datasheet).

Pinout of 1-wire

The 1-wire bus is managed in bit banging so anu GPIO can be used as 1-wire bus. The pin used depends from the device tree definition

Enable the Linux Kernel 1-wire driver

Follow this tutorial: to know how to cross compile the Linux Kernel and how to configure the drivers to enable inside it.

Enable the Dallas's 1-wire support and the Thermal family implementation as shown below:

Device Drivers  --->
    <*>   Dallas's 1-wire support
        1-wire Bus Masters  --->
            <*> GPIO 1-wire busmaster
        1-wire Slaves  --->
            <*> Thermal family implementation

Configure the device tree

Edit the device tree source of your board adding these lines:

onewire@0 {
    compatible = "w1-gpio";
    gpios = <&pioA 21 GPIO_ACTIVE_LOW>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_w1_0>;
};

Changing the gpio parameter you can chaneg the GPIO to use as 1-wire bus

Arietta G25 wirings

Following is an example on how to wire a Dallas 1-wire sensor to the Arietta G25 using the port PC2 (J4.35). It is possible to wire more sensors on the same lines. Don't forget to enable the 1-wire bus on the device tree file using this utility: Arietta pinout.

Read the temperature

The 1-wire driver automatically scans every 10 seconds if new sensors are plugged on the 1-wire bus.

For each 1-wire device detected a new directory is created on /sys/bus/w1/devices/w1 bus master.

Type:

cd "/sys/bus/w1/devices/w1 bus master"
ls

28-0000028f6667  w1_master_add              w1_master_remove
28-0000028fa89c  w1_master_attempts         w1_master_search
driver           w1_master_max_slave_count  w1_master_slave_count
power            w1_master_name             w1_master_slaves
subsystem        w1_master_pointer          w1_master_timeout
uevent           w1_master_pullup

The two directories 28-xxxx indicate that two thermal sensors are probed on the bus (28 is the family ID) and their unique IDs are 0000028f6667 and 0000028fa89c.

The file w1_master_slaves contains an updated list:

ls
cat w1_master_slaves

28-0000028fa89c
28-0000028f6667

To read the temperature for each sensor type:

cat 28-0000028f6667/w1_slave
49 01 4b 46 7f ff 07 10 f6 : crc=f6 YES
49 01 4b 46 7f ff 07 10 f6 t=20562
t=20562 indicates that the temperature read is 20.562 &deg;C 

Reading the temperature in Python

These simple programs in Python scan the 1-wire bus to detect the thermal sensors available:

scan.py

Download this example from playground then launch it by typing:

python scan1w.py

Scan for the available thermal sensors
Sensor ID = 0000028fa89c

This other example reads the temperature from a specific sensor.

read.py

Change the sensor ID in the source and try it:

python read.py

Temp=20.38 C

Links

Sergio Tanzilli
Systems designer, webmaster of www.acmesystems.it and founder of Acme Systems srl

Personal email: tanzilli@acmesystems.it
Web pages: https://www.acmesystems.it --- https://www.acmestudio.it
Github repositories: https://github.com/tanzilli --- https://github.com/acmesystems
Telegram group dedicated to the Acme Systems boards: https://t.me/acmesystemssrl