Interfacing DS18B20 Temperature sensor with Raspberry Pi

Got a hardware problem? ask for help

Interfacing DS18B20 Temperature sensor with Raspberry Pi

Postby kimi » March 13th, 2017, 7:52 pm

Image
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
Image
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.
Image
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,
dtoverlay=w1-gpio,gpiopin=4
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 eg.cd 28-00042d8165ff
Display temperature,

cat w1_slave

Terminal Output
Image
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
Code: Select all
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
   f.close()
   return lines

def read_temp():
   lines = read_temp_raw()
   while lines[0].strip()[-3:] != 'YES':                   # ignore first line
      time.sleep(0.2)
      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   
   time.sleep(1)

Image
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.
kimi
 
Posts: 4
Joined: August 11th, 2016, 8:01 pm

Return to Help me! Hardware

Who is online

Users browsing this forum: No registered users and 4 guests

cron