Having Quick2Wire Analog Board up and running it was time to test my sensors.
TMP36 is a quite small and cheap analog temperature sensor (here's its specification), and it's really easy to use.
It has 3 legs - ground, plus and output. Output is in the middle - can't be confused.
The other two - well, the first time I looked at the specification, I thought: "oh, yeah, pretty easy". Only that I didn't notice the words "bottom view", so I got it 180 degrees wrong.
What I got in the result was a very abnormal reading, like, ~100 degrees in my room. Obviously, that made me suspicious and I touched that little sensor... and burnt my fingers.
So I pulled the plug, checked what was wrong and soon realized my mistake. Interesting that the sensor survived and actually gave a probably correct output value.
Well, back to the normal operation.
This sensor has a range from -40 degrees Celsius to +125. Working with input voltages from 2.7 to 5.5, its internal scheme always outputs, according to the specification, 0.1 V to 1.75 V. So 0.1 V means -40 degrees and 1.75 V means 125.
Output is linear, 0.01 V per degree, so no advanced computation is needed.
The analog board reads only a 8-bit value, 0 - 255, and to know the actual voltage read, it needs to be converted. But this is really easy. The board is set up for 3.3 V, so "INPUT / 255 * 3.3" and that's it, there's the voltage.
Now I need just to convert this voltage to temperature. As I know that the sensor outputs starting from 0.1 V, reads from -40 degrees and the step is 0.01 V/degree, I get: "((-0.1 - (0.01 * 40) + (INPUT / 255 * 3.3))) * 100".
So I can now check a reading from the board:
[root@alarmpi ~]# gpio -x pcf8591:120:0x48 aread 120 53
Put this reading into my formula and got ~18.6 degrees Celsius. And that seems about what could actually be the temperature in the room right now. Done!