Hi all,
I'm currently attempting to get a TMP36 analog temperature sensor working with my Mega 2560, but running into some trouble. I've tried using a couple of guides to help with my code, but they're all pretty similar, and I get the same results on the few slightly different code layouts I've used. This is the code I have currently:
int sensePin = A8; //This is the Arduino Pin that will read the sensor output
int sensorInput; //The variable we will use to store the sensor input
double temp; //The variable we will use to store temperature in degrees.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)
}
void loop() {
// put your main code here, to run repeatedly:
sensorInput = analogRead(A8); //read the analog sensor and store it
temp = (double)sensorInput / 1024; //find percentage of input reading
temp = temp * 5; //multiply by 5V to get voltage
temp = temp - 0.5; //Subtract the offset
temp = temp * 100; //Convert to degrees
Serial.print("Current Temperature: ");
Serial.println(temp);
delay(500);
}
When I run the code, the serial monitor gives me wildly oscillating readouts - I'm aware the TMP36 is not an especially accurate sensor, but I'm not sure if its supposed to be quite this bad. On top of this is the fact that half the values are negative values, which clearly is not correct when I'm currently in a heated house. When I add code to print out the voltage, rather than the temperature, the voltage appears to be relatively stable, but I presume the small oscillations are being magnified by the conversion calculations.
On top of this, I have tried reading the voltage on the middle sensor pin, and this gives and even more believable readout (as it should). However, the voltage reading from both methods usually reads at between 0.3mV and 0.7mV, although it does not seem to oscillate between the two too rapidly, and I can notice a voltage increase/decrease when I manually warm the sensor. Clearly the 0.5V offset subtracted from the <0.5V sensor output is leading to the negative values.
I have the sensor connected to the A8 analog pin, with the two power pins connected to the GND and 5V supply on the double-width GPIO header of the Mega. I have tried the same calculations but with the 3.3V supply instead, but this has more or less the same result (the "temperature" values are fairly different to the ones using the 5V supply however, despite altering the code to account for it).
Basically, what I'm asking is, am I likely to have a faulty sensor? It certainly seems like it to me, but if possible I'd like to make sure that I haven't just made some silly mistake with my setup, before I go and order a new temperature sensor.