segunda-feira, dezembro 10, 2012

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

    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. Smile

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 Smile 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… 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

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 ROTFL

Then once that was working well, Adafruit helped with the data logging part too…

(Online Data Logging Tutorial

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. Smile

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… Wink


Sem comentários:


Related Posts Widget for Blogs by LinkWithin