Sunday, February 9, 2014

About the DS18B20 Temperature Sensors
{Revised!}

I bought my first one from Adafruit, I followed their wiring plan, and I copied their Python source code. But since the .py program just executes Shell commands I wonder why is Python needed at all? Hers's a .sh file that seems to work just as well:

if [ ! -d /sys/bus/w1/devices ]; then
sudo modprobe w1-gpio
sudo modprobe w1-therm
fi
a=`cat /sys/bus/w1/devices/28num-a/w1_slave` 
b=`expr "$a" : ".*YES.*t=\([0-9][0-9]*\)"`
len=${#b}
if [ $len -ge 2 ]; then
f=`expr $b \* 18 / 10000 + 32` # Note: expr only handles integers
echo $f
else
echo "DS18B20 Read Error"
fi

The above seems to work every time I've tried it. The "28num-a" bit above has to match the unique serial number of your device.

A weakness of the expr command (I wrote the original version in 1975) is that it doesn't do fractional numbers. So instead of--

expr $b \* 1.8 / 1000 + 32


You have to multiply by 18 and divide by 10000 to display Fahrenheit.

The heart of the mystery, though: the Adafruit wiring layout shows GPIO pin 4 connected to the DS's data wire but there is no reference to pin 4 in the code above. Since I want to connect 3 DS18B20s to my Pi, do they each take another pin? Or does the code in "w1-therm" only support a single device?

{Since no one answered my question, I found out for myself: You can read several devices with the modprobe software.}



Another worry: those 2 "modprobe" statements obviously leave something running. You can find it by executing the terminal command "ps -ax" -- and here's the culprit process:

 2980 ?        S     16:05 [w1_bus_master1]

That thing is a relative CPU hog; 16 minutes in about a week. 

No comments:

Post a Comment