IZ8MBW Fabio

Amateur Radio Station in Napoli - Italy

Configure Raspberry Pi to read 1-Wire (also known as 1Wire) sensors

This guide describe how to do all necessary for read temperature from 1-Wire sensor chips (also in parasitic power configuration).
1-Wire protocol use by default 3 wires: 1. Vcc (the power supply), 2. Data (GPIO #4), 3. GND (Ground).
1-Wire in Parasite Power mode will go to use only 2 wires: the GND and the Data (GPIO #4), the power supply for the chip will be "stolen" from the Data Pin (in Parasite Power mode the Vcc Pin of the chip, if it is available, will be connect to GND).
So in Parasite Power mode the power supply is not needed because it derives power directly from the data line.
Based on my experience with 1-Wire I have tested Maxim DS18B20 and Maxim MAX31826 both also in Parasite Power configuration.
Gives all commands as root user.

1. As usual, first, upgrade your packages
apt-get update
apt-get upgrade

2. Then, if you have not already done so, download and install rpi-update (later you will upgrade the Raspberry Pi system firmware)
apt-get install git-core
apt-get install ca-certificates
apt-get install rpi-update

3. Now install bc (Basic Calculator)
apt-get install bc

4. Now enable the 1-Wire kernel modules
a. Open for editing the file /etc/modules
vi /etc/modules
b. Add these lines at the end of the file (for Kernel version before 3.18):
w1-gpio pullup=1

b. Add this line at the end of the file (for Kernel version equal or greater then 3.18):

So the file will be:
# Parameters can be specified after the module name.

w1-gpio pullup=1 #remove this line if your Kernel version is equal or greater then 3.18

5. Now, upgrade your system firmware

6. Enable the Device Tree (DT) Kernel parameter (IMPORTANT: do this step ONLY if your Kernel version is equal or greater then 3.18)
Open for editing the file /boot/config.txt
vi /boot/config.txt

Add this line at the end of the file:

Instead add this line at the end of the file if you want to use parasitic Power:

7. Now, anyhow, reboot
Well, now your Raspberry Pi is configured for 1-Wire.
Before to connect the 1-Wire device (or devices) to the Raspberry Pi 1-Wire bus, you need to insert a 4.7 kohm pull-up resistor between the 3V3 (GPIO #1) and the GPIO #4. Note that if you will have more than one chip, anyway, only one resistor is needed. Consider also that for long meters cable may be necessary a low value of kohm for the resistor (eg. 3.3 kohm), but starts from the value of 4.7 kohm.
The 1-Wire bus is assigned to the GPIO #4 (Raspberry Pi Name GPIO 7) on the P1 Header. To understand well the Raspberry Pi GPIO pins, see the RPi Low-level peripherals.

8. Query your device (or devices) and read temperature
Each 1-Wire chip (slave) has an unique 64 bit address (8 bit family code, 48 bit serial number and 8 bit CRC.), to identify it go in the directory /sys/devices/w1_bus_master1 and then see what files and directory are in this directory:
cd /sys/devices/w1_bus_master1

ls -la

Now you should have a directory like this:
Enter into this directory and opend the file w1_slave:
cd 28-000004b07765

cat w1_slave

You will have an output like this:
81 01 ff ff f0 ff ff ff 64 : crc=64 YES
81 01 ff ff f0 ff ff ff 64 t=24062
The temperature is 24.062 °C.

9. Create a script that will read temperature from the sensor with errors control
I have made this shell script that will read temperature from the sensor, it contains also errors control.
Based on my experience there are same cases that 1-Wire gives uncorrect temperature values for same occurrence, example high ethernet throughput, during transmission with my HF RTX on same bands, temporany cable disconnection, etc..
So the errors control will work if you will obtain "crc= NO", "No such file or directory" (it can happens also during high ethernet throughput on the Raspberry Pi), or for uncorrect temperature values such as: 85.0, -62. So please note that if the readed temperature is 85.0 °C or -62 °C, the script will exit.

The Script:
sensor=$(cat /sys/devices/w1_bus_master1/28-000004b07765/w1_slave | cut -c 30-)
crc=$(echo $sensor | cut -c 8-10)
rawtemp=$(echo $sensor | cut -c 12-)
temp=$(printf "%.1f\n" $(echo $rawtemp/1000 | bc -l))
if [ "$rawtemp" = "" -o "$crc" = "NO " -o "$crc" = "" -o "$rawtemp" = "85000" -o "$rawtemp" = "-62" ] ; then
        echo $temp
exit 0

For more informations you can contact me via email at iz8mbw ]at[ gmail.com.
Enjoy! Fabio Ancona, IZ8MBW
