Oct 082012

I spent much of last week’s “Pi time” wrestling to get temperature sensors working through the Atmega on the Gertboard.

It was quite time consuming, but I managed to find some great helpful web sites and glean a bit of info from each. Once I’d got two different types of sensors calibrated, installed and working (and a third type temporarily abandoned), I started fooling around with logging the temperature readings. As things do, it evolved roughly like this…

  • View the temperature data on the screen updating every second
  • Make the temparature data stop scrolling and stay in a fixed place

    Temperature Measurement

  • Log the data locally on the Pi
  • Log the data on local media server so other local users can view it
  • Put it in a self-refreshing html file so it updates in the browser every few seconds

    Displayed in browser

But this was still insufficient. So the last step, pending the arrival of more sensors, was…

  • Log the data to the internet so that everybody can view it.

    Temperature logging on the web

Needless to say, each step threw up different challenges, and there was some frustration and trouble-shooting along the way.
I’m not going to fully document everything I did this time (for the moment) but I will share the sites I used for info gleaning and give you the edited highlights of the process.

Indoor/Outdoor Temperatures Logged per Minute

I now have Indoor and outdoor temperature sensors logging temperature data every minute to the internet. The scientist in me loves measurement and data logging. It seems like a million years ago (must have been summer ’89) that I did a module on electronics as part of my BSc (Analytical Chemistry). I don’t remember much of it, but I do remember I enjoyed it.

So, which sensors worked out?

I can heartily recommend two analogue sensors:

  1. TMP36 – costs about £1.50 and is a very simple to use, Celsius based sensor
  2. LM335z – costs about £1 is fairly simple to use, but needs an additional resistor and is Kelvin based

And the one I had trouble with?

That was the DS18B20 digital thermometer, which I’m sure is a brilliant piece of kit, but I couldn’t get it working in a useful timeframe. I will come back to it when I know what I’m doing. :)

Other time wasters?

After getting frustrated with the DS18B20, I wasted a lot of time trying to get the right resistor on the LM335z to make it read something like the right voltage. Unfortunately there are web sites out there where people give bad advice, so it’s a good idea to stick to good sources like MIT :) I did have a look at the datasheet for the sensor, but didn’t quite understand it. Basically you have to use good old R = V / I to calculate what value resistor to use according to what voltage you will be hooking the sensor up to (3.3V in my case) to get the required current.

MIT and Adafruit to the Rescue

This page over at MIT explains it very nicely in language I could understand. Basically 1000 Ohms for 5 volts, but that’ll be OK for 3v3 as well. And sure enough, it works just as it should (I really need to stop being surprised when things work as they should).

Another site which came to the rescue was Adafruit. A brilliantly simple piece of advice for testing analogue temperature sensors can be found here… http://learn.adafruit.com/tmp36-temperature-sensor/testing-a-temp-sensor In fact there’s a really good tutorial on the TMP36 there as well.

I wished I’d found it before I spent so much time piddling about taking advice from bogus sources. It shows you how to hook up your sensor directly to a Voltmeter to check that it is working properly.

And then there was the sofware

It was also necessary to find out what Arduino commands to program into a sketch. Adafruit to the rescue again http://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor

Then connect the Gertboard up in the right way to output data to the serial port.

Then write a little Python script to read from the serial port and output the data.

Now I write about this, and see how many steps are involved, I’m not at all surprised it took so long :rotfl:

Then once that was working well, Adafruit helped with the data logging part too…
(Online Data Logging Tutorial http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/overview)

I pulled a few bits out of that tutorial, but didn’t use their scripts as I was using the Atmega on the Gertboard.

Here’s the result

Without boring you with further details, here’s a link to the the RasPi.TV temperature feed on Cosm.

And here are the current graphs with the most up to date data…

Inside Temperature
Graph of RasPi.TV Temperature stream

Outside Temperature
Outside Temperature at RasPi.TV HQ

I don’t promise to leave it switched on all the time, as I will need my Pi and Gertboard for other things. But I’m going to try and set it up directly (minus Gertboard) on another Pi permanently in due course. At the moment there’s 2.5 days worth of data on the log. It defaults to 6 hours display. The best view is on 1 week display. Then you can clearly see the daily temperature cycle. Not surprisingly, outside it gets colder at night and warmer in mid afternoon. Inside similar, but more even. We haven’t used the heating much yet this year.

It’s been a lengthy learning process. I’ve enjoyed it. Frustrations are what makes the success all the sweeter. But it’s much easier to say that once you’ve overcome all the difficulties. :)

One more thing

I just learned the hard way that you need to stop logging before you mess about with the circuit. I wanted to move some stuff around on the breadboard to make it easier to demo in class tomorrow. I switched over a couple of wires expecting to be offline only a second. It didn’t go as planned and I ended up logging zero volts on both sensors. On the TMP36 this was equivalent to -48.3 C and on the LM335z it gave -270 C. This totally messed up the scale on the graphs, so I had to find out how to delete data points. It’s a bit tricky and you have to learn how to interact with the software API. Best to switch off logging before fooling with the circuit.

To be honest, you really should switch off everything before tampering with the circuit. We all know this, and yet… ;)

  22 Responses to “Using Temperature Sensors with Gertboard and the Raspberry Pi TMP36 and LM335z”

  1. This project is really interesting, could you give more technical details about the implementation?

    Thank you

    • Sure I can elaborate if you can tell me which part do you want more detail on :) The sensors, the arduino sketch, the COSM interface? It would take a long time to write in detail about all of it (which is why I haven’t yet done so). I’m happy to fill in some details though.

      • I have some distance sensors I’m using on a robot, is it possible to have the arduino convert the raw values into a distance and send this data to the pi and read it in python. Thanks!

  2. To be honest, I don’t get two things:

    1. If you follow this tutorial: http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/overview, how you connect the second thermometer (and the ldr sensors)? I havent used the cobbler yet.
    2. Why do you need the Gertboard? Probably, this answers the question 1.

    I think that a diagram of your circuit could answer my questions (or generate more :D )!!!

    thank you!

    • Good questions. Fortunately they both have fairly straightforward answers.

      1. I wrote a second blog with a photo of my circuit next to theirs. That shows clearly how the second sensor was attached. You might have missed it because I called it Pi Cobbler Review. https://raspi.tv/2012/adafruit-pi-cobbler-review. But I also had to hack the python code a bit to incorporate the second sensor (not too much though)

      2. At the time I wrote this blog the Gertboard was all I had. I used the Atmega on the Gertboard to run the sensors. Now I’m using the Cobbler on the semi-permanent setup and the Gertboard is free for other experiments. The permanent setup will be on a PCB that I am still working on populating. :)

  3. hello,

    thanks for this article. May u can make a picture of your hardware setup?


  4. […] From previous blog posts, you’ll know I have a Raspberry Pi set up to read two temperature sensors and two light sensors (inside and outside) and log the data online at COSM Setting up temperature sensors and COSM feed […]

  5. Hi,

    i really like your tutorial, but i have one question: Which ports did you use to connect the sensor to your gertboard?


  6. Would you mind sharing your code for this? I’m curious as to how to you’re reading the serial data from ATMega and matching the data vs my code (I’m new at this! :) Also, how did you get COSM to display the voltage of your LDR? Looking at the eeml.unit information it appears to only allow Temp, Humidity, and a few others related to environmental. Nothing that will accept raw data in the form of voltage (1023 / 3.3) or just the full 0-1023.


    • I’ll see if I can dig out that code and match the relevant snippets (I’m not posting it all). I’ve long-since dismantled that project and superseded it with a proto-plate version, so the Gertboard isn’t tied up. I now read an ADC directly using a script from Adafruit, which I have heavily customised (not sharing that at the moment).

      Here you go, I found it, warts and all.

      import serial

      sport = serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
      #print sport.portstr # for debug
      response = sport.readline(None) # I've forgotten what None refers to

      # I altered the arduino sketch to tag all serial output with "ID1"
      # as the program was erroring out on me with some random serial noise
      # Error checking prevents bombing out with corrupt serial input

      if not response.startswith("ID1"):
      sport.close() # this line is indented
      continue # this line is indented

      Here’s how to define your own units in the “write to COSM” command
      pac.update([eeml.Data(4, volts, unit=eeml.Unit('Volt', type='basicSI', symbol='V'))])

      Obviously the important bit is unit=eeml.Unit(‘Volt’, type=’basicSI’, symbol=’V’), but I leave the whole line there for context.

  7. I really appreciate you posting the above. The COSM information was incredibly useful.
    Thanks again and keep up the posting. Your site is excellent!

  8. Hi Alex!

    Perfect project, I’m in awe. During last days I’m working to create some kind of smart installation in my home and I’m fighting to connect TMP36 temp. sensors to my GERTBOARD. Could you please give me an advice how to connect sensors to my gertboard? I mean which pins on geartboard should I use?
    Additionally the question is if it is possible to connect more than 2 temperature sensors to the gertboard to working with Raspberry Pi.

    thanks in advance for your answer.

    • There are two ways to do it with Gertboard, either using the onboard ADC or the ATMega 328p chip. In this blog I used the ATMega, but it was such a long time ago and I’ve done so many things since then that I’ve forgotten the details.

      But, having said that, the best way to connect more than two TMP-36 sensors is using an MCP3008 eight channel ADC. There is a tutorial and software for this on the Adafruit site. You can connect up to eight of these temp sensors to this ADC.


      • I have TMP-36 sensors connected to my gertboard by MCP3008 but unfortunately I’m not able to read temperature values on it. It is showing value 0 all the time. Could you give my any hints how configure wiring on the gertboard extension to connect in a proper way? Additionaly it is needed some extra software?

Leave a Reply