Sunday, 26 April 2015

Raspberry pi temperature sensor

Today we've been having a go at another Raspberry pi project. This one is an O'Reilly "Raspberry Pi Cookbook" recipe.

Just like last time, the process went quite well, but getting the right colours of wires connected to the right terminals was really quite hard. I'm noting it here to help others who may be attempting the same project.

I used this temperature sensor from ebay, with the part number (DS18B20) as listed in the book. The three wires were coloured yellow, red and black, even though the documentation stated that they should be output lead: red (VCC), green (DATA) , yellow (GND).

The wires have to be connected from the pi, to the breadboard, to the temperature sensor as in the diagram in Figure 12-16 in the book.

Below is the bird's eye view of the breadboard. The temperature sensor colours go red, yellow, black from top to bottom. That was the hard part, and once we got it right, we were pretty much there. The pi wires are red at top left, white at bottom left, and black in the middle on the right. The resistor was only 1kΩ, as that was what we had handy and it didn't seem to matter which way it faced.

Below are a few more angles to show the detail:

This photo below also shows the jumpers attached to the raspberry pi, as in the Raspberry Pi Cookbook diagram. The positions are made clear in the book. 

We took the python programme from the website that comes with the book at and saved it on the pi as

We controlled from a laptop connected to the pi via putty over the wireless internet. 

It worked just fine except that the very top line of the programme was missing so that it was not automatically run as a python script. We added "#!/usr/bin/env python" to solve this problem. 

We typed "chmod +x" just the first time to change the permissions. 

We then used "sudo ./" to run the programme each time. This is what the output looked like.

Success! Thank you to Simon Monk, the author. That was fun.