Tuesday, September 3, 2013

Raspberry Pi and temperature sensing; to GPIO

Connecting a temperature sensor to GPIO
In this exercise, we're going to connect a Dallas DS18B20 temperature sensor to a breadboard, and read the temperature through the Raspberry Pi's GPIO pins.
The DS18B20 has a 1-Wire interface, which means that one of its leads is used for serial communications. The other two leads need to be connected to 3.3V and 0V. The sensor itself contains a small circuit that generates serial output. It also contains a unique serial number so that several sensors can be connected in parallel and still be addressed individually.
The circuit diagram and photoraphs on the right show how I set up my bread board. Pin 1 of the sensor is connected to either of the 3.3V pins on the GPIO connector. Pin 3 of the sensor must be connected to one of the ground pins on the GPIO connector. Pin 2 of the sensor is connected to GPIO pin 4. Pin 2 must not be allowed to float, so a pull up 4.7kOhm resistor must be used to connect pin 2 to 3.3V.
Before you can use the sensor, you need to load two kernel modules with these commands:
sudo modprobe w1-gpio
sudo modprobe w1-therm
These modules are device drivers that interpret data from the DS18B20.
Next, you need to type these commands to find the address of the DS18B20 and read from it:
cd /sys/bus/w1/devices/28*
cat w1_slave
When the sensor is detected, a directory is created using the serial number as the name of the directory. All serial numbers for this type of device start with '28', so we can use the '*' operator to change to any directory that starts with '28'. The sensor is represented by the file w1_slave, and you can get a reading from it using the 'cat' command. You should see output similar to this:
67 01 4b 46 7f ff 09 10 3b : crc=3b YES
67 01 4b 46 7f ff 09 10 3b t=22437
The temperature is shown in the last five digits on the second line. You need to divide this number by 1000 to get the temperature in degrees celcius.
As well as getting readings from the sensor by accessing it via the command line, we can also use a python script to get readings. The python script below goes through the same steps as above.
#!/usr/bin/env python

import os
import glob
import time

# load the kernel modules needed to handle the sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

# find the path of a sensor directory that starts with 28
devicelist = glob.glob('/sys/bus/w1/devices/28*')
# append the device file name to get the absolute path of the sensor 
devicefile = devicelist[0] + '/w1_slave'

# open the file representing the sensor.
fileobj = open(devicefile,'r')
lines = fileobj.readlines()
fileobj.close()

# print the lines read from the sensor apart from the extra \n chars
print lines[0][:-1]
print lines[1][:-1]
Save this code as ds18b20.py, and make sure it's executable with this command:
chmod +x ds18b20.py
Run this script by typing this command:
./ds18b20.py
You should see the same output as before:

No comments:

Post a Comment