I've been working on a data logging system for my greenhouse - basically just a way of tracking soil moisture, sunlight, rain barrel water level, and of course temperature inside and outside of the greenhouse.
I built most of it for Arduino, as I have several of them, and only one KL25z - and I have other plans for the KL25z board
But I thought I'd try the TMP36 temperature sensor with the KL25z, as the Arduino has a couple of limitations that are bugging me for the project:
* First, to keep things simple I've been saving the data in a plain array in memory, but the Arduino Uno only has 2Kb RAM available, of which much is taken up by my program as it runs. The KL25z has a whopping 16Kb.
* Second, the Uno's ADC is only 10bit where the KL25z has 16bit ADCs. For the TMP36 sensor, which returns a set 10mV per degree, this should give us some extra accuracy.
3.3 Volts ?
One of the first things I noticed is that the temperature returned was always much too high. So I checked the actual voltage on the 3.3v pin, and it actually shows as being a steady 2.908 volts. That's kind of odd, but adjusting the code to use that number to find the proper temperature seems to give me the correct results.
I don't know why, but the Arduino was returning much steadier results from the TMP36 sensor, even when I decided to take the average of something like 100 readings on the KL25z. Not sure why that would be, but maybe a hardware guru can enlighten us on that I'm guessing that the added speed of the KL25z might have something to do with it. From a software perspective, it might be a good idea to take a lot of samples and return the statistical mean - given the extra speed and memory, the KL25z should be able to tackle that quite easily without significant delay.
Here is the code I used, if you want to try this at home:
#include "mbed.h" AnalogIn tmp36sensor(PTB0); float sum; int main() { while(1) { sum = 0.0; for(int i=0; i<200; i++) { sum = sum + (tmp36sensor.read() * 290.8 - 50.0); // the 3.3v from the KL25Z is really only 2.908 volts } printf("Temperature = %0.1f \n", sum / 200.0); wait(2.0); } }
The sensor read returns a value between 0 and 1, which is the percentage the input voltage is at, compared to the reference voltage. The 50 is the offset required for the TMP36 (allows for negative temperatures).
Cheers,
-Nico
Top Comments