In this example we connect a DS18b20 temperature sensor to our Raspberry PI and read in the value, here is the schematic for this.
Start the terminal and entering the following
[codesyntax lang=”bash”]
sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices ls cd 28-xxxxxxxxxx (change this to match the serial number that pops up) cat w1_slave
[/codesyntax]
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
[codesyntax lang=”bash”]
sudo idle
[/codesyntax]
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
[codesyntax lang=”python”]
import time while 1: tempfile = open("/sys/bus/w1/devices/28-0000027a4334/w1_slave") temptext = tempfile.read(); tempfile.close() tempdata = temptext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 print temperature time.sleep(1)
[/codesyntax]
Click File > Save when you are finished (Ctrl + S).
To run your program, click Run > Run (Ctrl + F5), your should output like the following