DS18b20 and the Raspberry PI example

In this example we connect a DS18b20 temperature sensor to our Raspberry PI and read in the value, here is the schematic for this.


pi and ds18b20
pi and ds18b20

Start the terminal and entering the following

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
cd 28-xxxxxxxxxx (change this to match the serial number that pops up)
cat w1_slave

You should see something like this


In the output above the t=18875 is the temperature, in this case this is equal to 18.875

Now we will try this in python

Open the terminal and type in the following

sudo idle

Wait for IDLE to open,  then click File > New to open a new window. Or you can use CTRL + N.

Now type in the following code

import time
while 1:
 tempfile = open("/sys/bus/w1/devices/28-0000027a4334/w1_slave")
 temptext = tempfile.read();
 tempdata = temptext.split("\n")[1].split(" ")[9]
 temperature = float(tempdata[2:])
 temperature = temperature / 1000
 print temperature

Click File > Save when you are finished (Ctrl + S).

To run your program, click Run > Run (Ctrl + F5), your should output like the following