Monitoring Temperature With Raspberry Pi


The Problem:

I recently remodeled my home office and I now have a dedicated closet for my electronics (Server, NAS, AV Receiver, etc.) During the build I planned for heat remediation by installing an exhaust fan that dumps air from the closet into my adjoining office. However, the temperature in the closet hovers around 90°F (32°C), even with the fan on. Although this temperature is within hardware thresholds, it’s a bit warmer than I would prefer. To get a better understanding of my heat dissipation needs, I decided to monitor and record temperature fluctuations over several days to see what temperature ranges I was experiencing.

Monitoring temperature levels is a perfect project for the Raspberry Pi. I have used an analog TMP36GZ low voltage temperature sensor before in an Arduino project but this would be my first attempt at using the Raspberry Pi’s GPIO pins. Unfortunately, after a bit of research, I discovered that my analog temperature sensor wouldn’t work with the Raspberry Pi’s “digital only” IO pins. While I could have prototyped a solution using an ADC and some spare components, I really wanted a simple build so I could just start coding on the Pi.

The solution to my problem was a DS18B20 Digital Temperature Sensor IC which I found on The DS18B20 uses the 1-Wire communication bus which is perfect for the BCM GPIO4 pin (PIN 7) on the Raspberry Pi. Other caveats, you can work with the DS18B20 from the Linux terminal, and you can connect multiple 1-Wire devices, in series, to PIN 7.

The Build:

I had some spare CAT5e cable so I stripped and soldered 3 wires to the three pins on the sensor – orange for +3.3v, brown for ground, and green for data. Also, the DS18B20 requires a pull-up resistor between the power and data leads.

DS18B20 CAT5e connection

Then, I used electrical tape to insulate the exposed areas and I shrink-wrapped everything to protect the connections.

DS18B20 Shrink-wrapped

To the other end of the CAT5e cable I attached three female jumper wire cable housing connectors. These will principally be used for quick connections to a splitter rather than connecting directly to the Pi because I need to connect several devices to a single pin (specifically PIN7 for 1-wire).

DS18B20 Female Connectors

Next, I manufactured three tiny Y-splitters (2 male to 1 female) to join the VDD, DQ, and GND lines from 2 sensors before connecting to the Pi.

DS18B20 Y-Splitter

Finally, I made a second sensor and attached both to the Raspberry Pi using the following arrangement.
DS18B20 Schematic

Here is the finished build. Note the three splitters are plugged into PIN1 (orange/3.3v), PIN6 (brown/GND), and PIN7 (green/data).


The Code:

After connecting the DS18B20’s to the Raspberry Pi, you can interact with the devices using the below terminal commands. Note, your device IDs will be specific to your 1-Wire devices. In my case, my devices are 28-0000055f311a and 28-0000055f327d.

Here is what my terminal window looks like after running the above.

Click for larger image

In the terminal window above, the interesting information from reading from the device is the first line ending with “YES” – which means no errors – and the second line ending with “t=” followed by the temperature in thousandths of degrees Celsius (°C * 1000). Using this information, we can divide the temperature by 1000 to get Celsius and convert to Fahrenheit using…
°F = °C * (9/5) + 32.

Once we verify that the DS18B20 is operating correctly using terminal commands, we can construct a python script to read from the devices and write the temperature to a CSV file. Later we will plot the temperature readings to see how much they fluctuate over time.

Once created, you can run the above Python script from the terminal by using the following command.

Running the above terminal command a few times produces output, like the below, in a CSV file. Note that I have 2 Temperature sensors connected to the Pi. One is hanging in the center of the top of the closet, measuring ambient temperature, and the other is attached to the hottest exterior surface on my server.

Device TimeStamp Temp °F
28-0000055f311a 2014-05-21 22:21:02.585486 80.9366
28-0000055f327d 2014-05-21 22:21:02.585486 119.3
28-0000055f311a 2014-05-21 22:21:09.331944 81.05
28-0000055f327d 2014-05-21 22:21:09.331944 119.4116
28-0000055f311a 2014-05-21 22:21:13.082604 81.05
28-0000055f327d 2014-05-21 22:21:13.082604 119.3
The Schedule:

Now that we have a script to collect temperature data and save it to a CSV file, we need to schedule it to run periodically. In my case, I wanted to run the script every minute of every day for several days. To schedule the python script, we can use crontab from the terminal.

While in edit mode, add a crontab job using the following syntax. Note that this command should be all on one line in crontab.

The five asterisks (*) mean to run the job every minute, every hour, every day of month, every month, and every day of the week. The part about “> /dev/null 2>&1″ just means don’t save the output or errors in a log and don’t show anything on any screens (i.e. run silently even if errors occur).

The Results:

After a few days of collecting data, I can plot the results to see how effective I am at dissipating heat from my closet. Below is a chart of the output after running the Python script, via crontab, for several days.


Now that I have a baseline, I can experiment with different heat dissipating methods to find what works best to keep my electronics cool. When I’m done monitoring my electronics closet, I can see myself redeploying the rig to other projects such as attics, crawl spaces, automotive projects, and mini-fridge hacking. Let me know in the comments if you come up with your own temperature monitoring projects.

    13 thoughts on “Monitoring Temperature With Raspberry Pi”

    1. Thanks for a good post about the DS18B20 and Raspberry Pi.
      I learned what I needed to use my thermometer from this post.

      I think you only should use one resistor for the pull-up and not two, even though you have two DS18B20s. With two resistors of 10K ohm in parallel the combined resistance is 5K ohm.

      1. To keep the post simple I just dumped the output to a CSV file. You could, however, change it to update a database such as SQLite (locally) or MySql (remotely). Then using your language of choice you could plot the data. I’m partial to .Net. Therefore, if I wanted to do something like that, I might use the Microsoft Chart controls in an ASP.Net web application.

        Getting Started with MS Chart Controls

    2. Hello really like your instruction, followed them to a “T”, however when I run the script I keep on getting the below error. It would be great if you can help. I did verify I am getting all temp to work manually. All I changed was line 15 to the below directory. I put the on my Desktop.

      Traceback (most recent call last):
      File “/home/pi/Desktop/”, line 42, in
      with open(temp_log, ‘a’) as f:
      IOError: [Errno 2] No such file or directory: ‘/home/pi/Desktop/temp.csv’

    3. Sorry I figured it out, was pointing to the wrong directory. Works awsome and pretty accurate.

      One additional question. I am not a pearl expert, mostly bash.
      However is there a way to add one additional column to correspond sensor name to actual name. In your example; change to , , , , ?

      On additional item, is there a way to change the time output to omit the 6 digits. Stop at sec.

      For example:
      28-000007666b05,2016-01-16 19:46:20.715939,15.625
      28-000007666b05,2016-01-16 19:46:20,15.625
      28-000007666b05,Server_Room, 2016-01-16 19:46:20.715939,15.625

      1. There are a few ways to convert the device ID to a friendly name. One way would be to delimit the device list using something like pipe.
        lst = []

        Then you can use split to get the left and right (Path and Name) elements.

        1. To trim off the fractional portion of the time just the left most 19 characters.
          date_log = str(
          ’2016-02-12 18:37:13.553248′
          ’2016-02-12 18:37:13′

    4. Kevin,

      What a life saver. I am trying to build a CoolBot clone with a RPi3B board. I need 2 sensors for this and heres a ton of work already done. Fantastic. This is going to save me so much time.

      Thank you!

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    9 + = fourteen

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">