I have done a very simple starter project to read the temperature from a TMP36…
Basic connectivity:
Plus side connected to the 3.3v*
The other side to the Ground
The reading is done on A0
Nothing more…
here is my code:
int reading = 0;
int temp_calc = 0;
char whichApp[64] = "READ TEMPERATURE BASIC";
// This routine runs only once upon reset
void setup()
{
//Register our Spark function here
Spark.variable("whichapp", &whichApp, STRING);
Spark.variable("reading", &reading, INT);
Spark.function("degres", tempCalculation);
Spark.function("volt", analogReading);
pinMode(A0, INPUT);
}
void loop()
{
reading = analogRead(A0);
}
int tempCalculation(String command) {
int tempCalc = (reading*3.3/4095)*100 - 50;
return tempCalc;
}
int analogReading(String command) {
return reading*3.3/4095;
}
Initially I had issue when the USB power supply was connected, then I would get numbers all over the place but if I was on battery it was looking ok (although still very unstable but in a range that made sense).
But now no matter how I have the power supply this is the kind of data I get:
I mean… -50 degrees, changing by 20 or 30 degrees every second… Then I reboot, and I’m in the 40 degrees, etc… It’s totally crazy.
@BDub I rewired my board with shorter cable initially without adding anything and ended up having a stable 60 degres… It was wrong but at least stable.
Following on I added the 0.1uf capacitor like you describe (awesome little image you sent!!) and now I get a something around 25 degres… It’s a bit too high but closer to what it should be. I’m expecting the room to be about 22/23 or so…
0.1uF is actually too much for the TMP36. It has a output capacitance requirement of 0.01uF or less, or it can oscillate. Got anything that small or even a little smaller? You are basically just lowering the impedance of the input so something is better than nothing.
Dialing in your reading accuracy can be achieved by measuring your 3V3* rail with respect to GND with a good voltmeter, and use that number in the calculation instead of 3.3.
The sensor also has a accuracy of ±2°C accuracy over temperature (typ) and ±0.5°C linearity (typ) … so you probably may still have an offset… which you can compensate for in your equation as well
I’ll try measuring the 3.3 output. And sorry it was a typo on my part I did put a 0.01uf (not 0.1) or at least I think (103 written on it).
The other thing I’ve done is to take 5 measures (1 every second) and then only upload the Average to Xively to avoid variation…
The result is as follows:
it did stabalize around 14 degrees in the end (58F which is too cold) but at least it’s a fairly stable reading so I just need to adjust it.
thanks for all the tips.
It did crash after 1 hours due to some TCPClient bug going on, but that’s another issue that the Spark team is working on I think. They have to fix this…
Very nice @apassemard! Still seems to jump around a bit… since it’s room temp I know it’s not quite that spiky… you might want to add a heavier filter to it… I wrote this one for 8-bit micros, but it works even faster on the spark core’s STM32 microcontroller. It could probably be redone to take advantage of multply and divide instructions that I’m sure the STM32 has.
Keep in mind too that if you’re getting the room temperature from your wall thermostat it may not be the same where you have the Spark Core. Due to drafts and the fact hot air rises and cold air sinks, you can have quite a large variation (5+ degrees sometimes). So you should have another thermometer where your TMP36 is to confirm.