Skip to content

Part 3. Temperature Sensor

Rachel edited this page Jun 3, 2015 · 4 revisions

Temperature Sensor

Next, let's test the temperature sensor. The current version of Raspbian as of the writing of this tutorial (kernel 3.18) requires an addition to your /boot/config.txt file for the Pi to communicate with the DS18B20. Run the following to edit this file:

$ sudo nano /boot/config.txt

If the following line is not already in this file (if it is, it is likely at the bottom of the file), add it and save the file.

dtoverlay=w1-gpio,gpiopin=4

Restart your Pi for the changes to take effect.

$ sudo reboot

To start the temperature sensor read interface we need to run two commands. Go to a command prompt on your Pi or SSH into your Pi. Type the following commands:

$ sudo modprobe w1-gpio
$ sudo modprobe w1-therm

The output of your temperature sensor is now being written to a file on your Pi. To find that file:

$ cd /sys/bus/w1/devices

In this directory, there will be a sub-directory that starts with “28-“. What comes after the “28-” is the serial number of your sensor. cd into that directory. Inside this directory, a file named w1_slave contains the output of your sensor. The contents of this file will look something like this (you can use nano to view the contents of the file):

a2 01 4b 46 7f ff 0e 10 d8 : crc=d8 YES 
a2 01 4b 46 7f ff 0e 10 d8 t=26125

The number after “t=” is the number we want. This is the temperature in 1/1000 degrees Celsius (in the example above, the temperature is 26.125 C). We just need a simple Python script that reads this file and parses out that number.

Tip: if you don't see a sub-directory that starts with "28-" but see multiple sub-directories that start with "00-", you might have the resistor plugged into ground instead of into power. If your circuit is wired correctly and you continue to get "00-" sub-directories, you might have a bad temperature sensor.

Assuming you ran the "git clone https://github.com/InitialState/beerfridge.git beerfridge" command in Part 2, you have a script called temperature_test.py in your ~/beerfridge directory.

https://github.com/InitialState/beerfridge/blob/master/temperature_test.py

import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

BASE_DIR = '/sys/bus/w1/devices/'
DEVICE_FOLDER = glob.glob(BASE_DIR + '28*')[0]
DEVICE_FILE = DEVICE_FOLDER + '/w1_slave'

def readTempRaw():
    f = open(DEVICE_FILE, 'r')
    lines = f.readlines()
    f.close()
    return lines

def readTemp():
    lines = readTempRaw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = readTempRaw()
    equalsPos = lines[1].find('t=')
    if equalsPos != -1:
        tempString = lines[1][equalsPos+2:]
        tempC = float(tempString) / 1000.0
        return tempC

while True:
    tempC = readTemp()
    tempF = tempC * 9.0 / 5.0 + 32.0
    print str(tempF) + " F"
    time.sleep(.5)

This script will simply read the number after "t=" in the w1_slave file, convert that number to Fahrenheit, and return the result to the prompt. Run the following commands at a Pi terminal:

$ cd ~
$ cd beerfridge
$ sudo python temperature_test.py
72.331 F
72.331 F
73.248 F
73.913 F
74.612 F

If you are seeing valid temperature readings output to the prompt, then the temperature sensor is working. Heat up the sensor by holding it in your hand or cool it down by sticking it in ice (it is waterproof).

Once you are done playing, kill the script with CTRL+C. Place the end of the probe inside the refrigerator. I used longer jumper wires so that the door could close on the skinnier jumper wires instead of the thicker DS18B20 cable. We are now ready to put everything together.

<< Part 3: Door Sensor - Part 4: Putting it All Together >>