- 1 Circuit Diagram
- 2 Sensor not detecting after OS update
- 3 Reading temperature using Terminal
- 4 Terminal Output
- 5 Python code
- 6 Output
DS18B20 is a commonly used temperature temperature sensor providing 9 bit to 12 bit digital Celsius temperature measurements. The sensor communicates over one wire bus. Each sensor has a 64 bit serial code enabling multiple sensors to be connected to the same one wire bus.
The Raspberry Pi has drivers for one wired devices to be connected to GPIO pin-4 by default. 1-wire is a device communication data bus system developed by Dallas semiconductor providing low speed data, signalling and power over a single signal wire. One wire is similar to I2C with longer range and low data rates. It can be used to communicate with inexpensive devices like thermometers, humidity sensors and other one wire sensors over a long range. The main advantage is since each device has a unique address, any number of devices can be connected on a single wire limited by the drive capacity of Raspberry Pi’s GPIO and the total capacitance seen on the line.
The one wire bus requires a weak pull up resistor as all devices linked to this bus via a tri-state or open drain output. Here a 4.7KΩ resistor is used as pull up.
Sensor not detecting after OS update
Users having Rasbian with updated Linux kernel 3.18 need to edit boot config file to work with one wire sensors.
- In terminal edit config file,
sudo nano /boot/config.txt
- And add,
- And save (Ctrl+X)
Reading temperature using Terminal
The one wire communication device kernel modules can be loaded by typing,
sudo modprobe w1-gpio
sudo modprobe w1-therm
Point to the address of the temperature sensor,
Note: If more than one device is connected use cd /sys/bus/w1/devices/ and ls to list all the devices and cd address where address is the unique address of the device required. For eg.cd 28-00042d8165ff
The displayed data consist of two lines – first line representing the value read and CRC check and second line temperature in Celsius x 1000.
This can also be done in Python with python handling CRC errors and the temperature displayed both in Celsius and Fahrenheit.
import os # import os module
import glob # import glob module
import time # import time module
os.system(‘modprobe w1-gpio’) # load one wire communication device kernel modules
base_dir = ‘/sys/bus/w1/devices/’ # point to the address
device_folder = glob.glob(base_dir + ’28*’) # find device with address starting from 28*
device_file = device_folder + ‘/w1_slave’ # store the details
f = open(device_file, ‘r’)
lines = f.readlines() # read the device details
lines = read_temp_raw()
while lines.strip()[-3:] != ‘YES’: # ignore first line
lines = read_temp_raw()
equals_pos = lines.find(‘t=’) # find temperature in the details
if equals_pos != -1:
temp_string = lines[equals_pos+2:]
temp_c = float(temp_string) / 1000.0 # convert to Celsius
temp_f = temp_c * 9.0 / 5.0 + 32.0 # convert to Fahrenheit
return temp_c, temp_f
print(read_temp()) # Print temperature
Read More Information….