Using Temperature Sensors with Gertboard and the Raspberry Pi TMP36 and LM335z
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
- 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
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.
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:
- TMP36 – costs about £1.50 and is a very simple to use, Celsius based sensor
- 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 Peripheral Interface SPI bus.
Then write a little Python script to read from the SPI bus 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
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
Outside Temperature
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…
via RasPi.tv http://raspi.tv/2012/using-temperature-sensors-with-gertboard-and-the-raspberry-pi-tmp36-and-lm335z
Comentários