Wegens vertragingen bij de aankomst van goederen in de EU vanuit het VK, gelieve extra tijd te voorzien voor de levering


How To Build A Raspberry Thermostat

Scroll down

Now we all know that the ubiquitous Raspberry Pi can be used for gadgets galore like voice controlled LEDs and steampunk radios. (We should know – we’ve covered most of them.) But then we thought: maybe we ought to include something a little more…grown up. Then we had an idea. Could we use our Pi to build a thermostat?

Of course we could.

So we did. And here’s how.

The sensor

For starters, we’ll be using a DS18B20 1-Wire temperature sensor and doing something like this:

Check your Pi and you’ll see the GPIO pins are only digital and don’t have an ADC (analogue to digital converter).Which is a shame. No matter – we simply used the DS18B20 digital temperature sensor, which works on a digital 1-Wire interface. Solved.

Before we went any further, we needed to consider some of the features of Dallas DS18B20:

  • 1-Wire interface.
  • Operating temperatures from -55ºC to 125ºC (with an accuracy of +/-0.5ºC when measuring between -10ºC and 85ºC).
  • Works on a power supply range of 3.0V to 5.5V.

This gave us a pretty good idea about what we could expect from this gadget (the full specifications of this temperature sensor can be found in the manufacturer’s data sheet).


Now we looked at the connecting hardware.

We hooked up the DS18B20 to our Pi like this:

Pi                              DS18B20

Pin 6 (GND)      –     Pin 1 (GND)

Pin 1 (VCC)       –      Pin 3 (VDD)

Pin 7 (GPIO)      –      Pin 2 (DQ)

Be sure to hook up a 4.7K–10K ohm pull-up resistor in parallel between VCC and GPIO. Since the DS18B20 can be powered between 3.0 and 5.5v, we decided to do this at 3.3v on Pin 1 of our Raspberry Pi using a 4.7K ohm pull-up resistor.

Configure the Pi

When it came to sorting out our software, we chose to SSH into our Pi so we could connect remotely and not have the hassle of plugging-in a monitor, keyboard and mouse.

$ ssh -Y pi@

The next step was to enable using 1-Wire with the GPIO. To do this we opened an existing text file using the nano command which lets us edit it. We then simply added an extra line of configuration at the bottom of the file. This way, we didn’t need to include any 1-Wire libraries in the code. The kernel now takes care of using the 1-wire protocol to communicate with any 1-wire devices connected to GPIO.

$ sudo nano /boot/config.txt

Then we added a line at the bottom of the file:


Next we rebooted our Pi to action the changes by entering:

$ sudo reboot

Find the sensor's device ID

Every DS18B20 has a hard-coded unique address, so different temperature sensors can be connected on the same bus and all still be accessed individually. By using the line below it’ll return the unique addresses for your devices (the Kernel creates a directory for each 1-Wire device it detects):

$ ls -l /sys/bus/w1/devices

Exploring the sensor data

Now we were faced with the question of how to squeeze the raw data from the sensor.

The answer was simple enough – use the cat command to print readings to the terminal:

$ cat /sys/bus/w1/devices/28-000006780b89/w1_slave

Once the raw data has been returned, check out the section of the second line which says t=***** This is the temperature reading. To get a value in degrees Celsius, just divide by 1000.

We liked this way of using the terminal to get readings from the DS18B20 – it proved the temperature sensor was wired up and working just fine. But we did want repeated readings so we could tweak things to build our thermostat, which meant accessing them from within code was a must.

Extracting readings

This Python code reads the sensor every second and outputs the temperature in degrees Celsius. (See the comments for the full story.)

Or you can convert the temperature reading from Celsius to Fahrenheit by including this step and altering the variable name after the print command.

$ TempF = temperature * 1.8 + 32

We ran the code with:

$ python Thermostat.py

Add an output

Next, we added an LED which would glow when the temperature sensor returned a reading greater than a set value. We chose any temperature exceeding a balmy 30ºC.

As it happens, this was easy enough. We simply added an LED on the breadboard and connected the anode via a 68 ohm resistor to pin 11 (GPIO 17) and the cathode to the GND rail of the breadboard.

Update the code

We updated the Python code by adding the lines which would make the LED light up when the temperature sensor had reached 30ºC or above…

…and bingo! We were the proud owners of our very own home-made thermostat!

We have to say, the LED which tells us when we’ve hit the ‘maxTemp’ is a really cool touch, too.

All in all, taking readings from a temperature sensor using GPIO on your Pi is pretty easy. And it struck us that, by adding more sensors, we could develop some awesome projects. Which got us thinking…

But we’ll save that for another day.


Zoals wat je leest? Waarom niet uw waardering tonen door het geven van wat liefde.

Van een snelle tik tot het verpletteren van die liefdesknop en laat zien hoeveel plezier je aan dit project hebt beleefd.


Onze website maakt gebruik van cookies en vergelijkbare technologieën om u een betere service te bieden bij het zoeken naar of plaatsen van een bestelling, voor analytische doeleinden en om onze advertenties te personaliseren. U kunt aan de hand van ons cookiebeleid uw cookie-instellingen wijzigen. Indien u geen wijzigingen aanbrengt, nemen we aan dat u akkoord gaat met ons gebruik van cookies.


Please select an option to see content specific to your location and shop online.

Browse the US site