Amateur Radio Station in Napoli - Italy
ITU Zone: 28 - CQ Zone: 15 - Locator: JN70ct
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
2. Then, if you have not already done so, download and install rpi-update (later you will upgrade the Raspberry Pi system firmware)
4. Now enable the 1-Wire kernel modules
a. Open for editing the file /etc/modules
b. Add these lines at the end of the file (for Kernel version before 3.18):
b. Add this line at the end of the file (for Kernel version equal or greater then 3.18):
So the file will be:
# /etc/modules: kernel modules to load at boot time.
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# 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
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
Now you should have a directory like this:
Enter into this directory and opend the file w1_slave:
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.