Temperature Sensing With Raspberry Pi

This is a placeholder topic for “Temperature Sensing With Raspberry Pi” comments.

Is it hot in here, or is it just my Raspberry Pi? A Raspberry Pi sitting on a desk doing nothing is kind of boring. However, a Raspberry Pi that can measure the temperature and make decisions based off that information is awesome!
The Raspberry Pi l…

Read more


Firstly, thanks for the great tutorial. Even I managed to get it working first crack! I just want to better understand a few elements of the code that I don’t get (I am an absolute beginner). See the chunk of code below.

    while 1:
        tempStore = open("/sys/bus/w1/devices/28-0315902106ff/w1_slave")
        data = tempStore.read()
        tempData = data.split("\n")[1].split(" ")[9]   # I'm stuck with this line
        temperature = float(tempData[2:])             # and stuck with this line
        temperature = temperature/1000
        print temperature

The two commented lines is where I am stuck. I understand what the .split() method does but I don’t understand what the [1] and the [9] do in that line. And in the next line I don’t understand what the [2:] bit does. If anyone could shed some light on this for me it would be much appreciated!


Hi Julian,

You’re almost there! The two questions you have both relate to arrays in Python.

tempData = data.split("\n")[1].split(" ")[9]

Create a new variable called tempData. Set it’s value to;

  • take the data value previously create
  • Split the data value by occurances of a newline (\n), the output of this is an array object.
  • Using that array, take element at index 1 ([1]). Arrays start at index 0, so we’re ignoring the first element and anything after index 1. This will be a string object.
  • Using that string object, split it into an array using spaces as the delimiter. Then return just the element at index 9 ([9]).

To get a better understanding of how this works, add in a line underneath “print temperature”;
print data

This will output the raw data coming from the sensor, and you will see where the data for temperature is stored in the output. All this line of code is doing is honing in on the specific value you’re interested in (temperature).

temperature = float(tempData[2:])

This is short hand notation for taking elements from an array. It’s saying take all the elements from index 2 until the end of the tempData array, and create a floating point number using that as input. Likely tempData is a string, with 2 spaces or characters at the start of it that need ignoring. As per the above, if you use print tempData somewhere, it’ll show you exactly why the first 2 characters need ignoring.

  • Create a new variable called temperature
  • create a floating point number, using the tempData array as an input
  • only use elements from index 2 until the end of the array as input

Further Reading: Colon (:) in Python list index - Stack Overflow


Thanks for the detailed reply. This was a great help and answered my questions perfectly :slight_smile:


1 Like

Hey: I think your temperature checks as written are missing any measurements of exactly 20 and 24 ?

if temperature < 20:
if temperature in range(20,24):
if temperature >= 24:

(Not that it matters one iota!)


I notice you pulled the DS18B20 sensor up to +5V, then fed that line to the RPi GCLK0 (GPIO 4) pin. I thought that pin was supposed to be limited to +3.3V? I didn’t see anything in the DS18B20 spec sheet that would limit it to 3.3V, or did I miss something?
BTW, I did build it as you suggest and it all worked. Great tutorial.

Thanks, Phil