Вы находитесь на странице: 1из 3

Easy Temperature Sensing with Raspberry Pi

I had a few DS18B20 sensors lying around, and decided to create a easy temperature
monitoring system with the old Raspberry Pi Model B+ (Version 1). It turned out to be a
lot more challenging than expected, but hopefully this guide helps you get started.
NOTE: I have opted to go with the DS18B20P for Parasitic sensing and this
guide will cover that - this is different in circuitry to the DS18B20!
Things you'll need

Raspberry Pi (B+ or above)

DS18B20 PAR Sensor

A few wires

puTTY

What is Parasitic Mode?


Parasitic mode allows the DS18B20 sensor to operate without a dedicated power source
or resistor for pull-up. However, it requires a stronger pull-up from the Raspberry Pi
itself. Most online tutorials use the standard DS1820 sensor with the 4.7k resistor
present. The key disadvantage to using parasitic is it can be a bit unreliable and your
cable needs to be relatively short for the RPi to pick up the data.
Hardware
Connect your temperature sensor to the Raspberry Pi as per the diagram below

If the sensor starts getting hot, you've connected it wrong. Because we're in parasitic
mode, remember to use short length cables and don't plug in the power.
Disable Device Tree
With Raspbian 3.18 and above, we need to disable the device tree. go to 'sudo raspiconfig' > Advanced > Device Tree > Disable. Ensure you restart.
Code
I want my python script to display the temperature every second for the purposes of
testing. I also want it to be able to swap out sensors, etc. The following script, adopted
from a number of sources, lets me do just this:
Create the file: sudo nano temp.py

Paste the following:


# (c) Jay Janarthanan 2015.
# http://jay.appavate.com
# You can use the code below freely under a GNU license.
import os, glob, time, sys, datetime
#initiate the temperature sensor in Parasitic mode
os.system('sudo modprobe w1-gpio pullup=1')
os.system('sudo modprobe w1-therm strong_pullup=1')
#set up the location of the sensor in the system
device_folder = glob.glob('/sys/bus/w1/devices/28*')
device_file = [device_folder[0] + '/w1_slave']
def read_temp():
f = open(device_file[0], 'r')
line = f.readline()
crc = line.rsplit(' ',1)
crc = crc[1].replace('\n', '')
if crc=='YES':
line = f.readline()
mytemp = line.rsplit('t=',1)
else:
mytemp = 99999
f.close()
return int(mytemp[1])
while True:
temp = read_temp()
print(temp)
if(temp == "99999" or temp == "85000"):
print("ERROR: Please check your sensor")
time.sleep(1) #wait 1 minutes
view rawtemp.py hosted with by GitHub

Now run it: python temp.py


The script should now display the current temperature every second. You may see that
the temperature remains constant - gently touch the sensor and see if the temperature
changes. If it's not changing within a few seconds, something has gone wrong.

Errors
If you get '85000', then there is a problem. This is can be caused by

Bad or loose wiring

Using Power incorrectly

Switched ports somewhere

Faulty sensor

Conclusion
And there you go! A simple temperature sensing device using the Raspberry Pi!
As always, feel free to drop me a line, comment below or hit me up at @jayjanarthanan.

Вам также может понравиться