Interfacing DS18B20 Temperature sensor with Raspberry Pi

Digital Fiber, Photoelectric, Laser Range, Optical, Temperature, Rotary Encoder, Ultrasonic, Gas, Gyro, Accelerometer, FlexiBend, Flexiforce, Compass......

Interfacing DS18B20 Temperature sensor with Raspberry Pi

Postby pololo » Tue Aug 23, 2016 2:04 pm

DS18B20 is a commonly used 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.

Circuit Diagram
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,
cd /sys/bus/w1/devices/28*

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 28-00042d8165ff
Display temperature,
cat w1_slave

Terminal Output
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.

Python code
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
os.system('modprobe w1-therm')                                                 
base_dir = '/sys/bus/w1/devices/'                          # point to the address
device_folder = glob.glob(base_dir + '28*')[0]             # find device with address starting from 28*
device_file = device_folder + '/w1_slave'                  # store the details
def read_temp_raw():
   f = open(device_file, 'r')
   lines = f.readlines()                                   # read the device details
   return lines

def read_temp():
   lines = read_temp_raw()
   while lines[0].strip()[-3:] != 'YES':                   # ignore first line
      lines = read_temp_raw()
   equals_pos = lines[1].find('t=')                        # find temperature in the details
   if equals_pos != -1:
      temp_string = lines[1][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

while True:
   print(read_temp())                                      # Print temperature   

Python Output
The python output consists of temperature in both Celsius and Fahrenheit scale displayed at an interval of one second.

Any doubts or suggestions? Comment below.
Posts: 3
Joined: Mon Aug 22, 2016 11:38 am

Re: Interfacing DS18B20 Temperature sensor with Raspberry Pi

Postby ZaM » Wed Aug 24, 2016 5:12 pm

Hi, what kind of suggestion? If you want to show only one type of temperature, just delete line that have that type.
Posts: 73
Joined: Tue Nov 23, 2010 4:16 pm

Return to Sensor

Who is online

Users browsing this forum: No registered users and 3 guests