Hi all,
In my troubleshooting I now have a TMP36 sensor setup in the most basic circuit on a breadboard. The TMP36 is connected to an Arduino Nano via GND, A0, and 3.3v
When I test this connection with the multimeter at both the TMP36 pin and the jumper cable at A0 on the arduino I am getting 720mV reading... which is perfect! (720 - 500) / 10 = 22C which is the exact temperature of the room I am in.
However, when I run the Arduino and check the serial monitor (running the below abridged code)
Please where did you get this formula from? Every single page I have found online says that the formula for deriving mV from the analog reading is either (5000/1024) for 5v input and (3000/1024) for 3.3v input on TMP36 sensors???
Try my "no floats, no delays" sketch for Nano and LM35 / TMP36, measure voltage between GND and AREF pin, multiply by 100 and enter in variable hAref on line 8, fine tune with calValue on line 7.
EDIT: Fixed typo in function "antiDither", my fault
/*
LM35, TMP36 thermometer, no floats, no delays
*/
const bool t36 = true; // set false for LM35, true for TMP36
const byte numSamples = 8, // number of samples for smoothing
aInPin = A7; // analog input pin
const int calValue = 0, // adjust for calibration, +/- 1 tenth degree C
hAref = 109, // analog ref voltage * 100
// measured with accurate DMM
hnumSamples = numSamples * 100,
tEnd = 3000; // update time in mS
int val,
tempC,
tempF;
uint32_t total, // sum of samples
tStart; // timer start
byte cnt = 10;
const char header[] = "\nRaw Total Temp C Temp F";
void setup()
{
Serial.begin(9600);
analogReference(INTERNAL); // use 1.1V internal ref
analogRead(aInPin);
for(int i = 0;i < numSamples;i++) // for smoothing, fill total
total += analogRead(aInPin); // with numSamples * current
// reading
}
void loop()
{
if(millis() - tStart > tEnd)
{
tStart = millis(); // reset timer
if(++cnt >= 10)
{
Serial.println(header); // every 10 lines
cnt = 0;
}
val = analogRead(aInPin);
total -= (total / numSamples); // make room for new reading
total += val; // add new reading
tempC = total * hAref / hnumSamples + calValue;
if(t36)
tempC -= 500;
tempC = antiDither(tempC);
tempF = tempC * 9 / 5 + 320;
Serial.print(val);
Serial.print("\t");
Serial.print(total); // sum of samples
Serial.print("\t");
prntTemp(tempC);
prntTemp(tempF);
Serial.println();
}
}
void prntTemp(int temp)
{
Serial.print(temp / 10); // whole degrees
Serial.print(".");
Serial.print(temp % 10); // tenths
Serial.print("\t");
}
int antiDither(int newVal) // prevents +1 <-> -1 dither
{
static int val = 0, oldVal = 0;
if(newVal != val && newVal != oldVal)
{
oldVal = val;
val = newVal;
}
return val;
}