Thermistors reading backwards?

I’m running a DHT-22 and a L3336 thermistor off an Arduino uno with the below code. The DHT-22 works fine, but the thermistor is running backwards i.e when we heat it the output temperature goes down, whilst when we cool it the output temperature becomes higher. I’m not sure if it is the code that isn’t working or our thermistor. From all that I’ve seen the thermistor is a NTC (because PTC is mainly used for circuit breakers rather than measuring temperature) so I believe that my code should be the right way around for this. I’ve tried a few different thermistors from this batch and they all have the same problem, any ideas?

#include <math.h>

#define ThermistorPIN 0                 // Analog Pin 0

float vcc = 4.91;                       // only used for display purposes, if used
                                        // set to the measured Vcc.
float pad = 1000;                       // balance/pad resistor value, set this to
                                        // the measured resistance of your pad resistor
float thermr = 1000;                   // thermistor nominal resistance

float Thermistor(int RawADC) {
  long Resistance;  
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=pad*((1024.0 / RawADC) - 1); 
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                      


  // Uncomment this line for the function to return Fahrenheit instead.
  //temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  return Temp;                                      // Return the Temperature
}

void setup() {
  Serial.begin(19200);
}

void loop() {
  float temp;
  temp=Thermistor(analogRead(ThermistorPIN));       // read ADC and  convert it to Celsius
  Serial.print("Celsius: "); 
  Serial.print(temp,1);                             // display Celsius
  Serial.println("");                                   
  delay(1000);                                      // Delay a bit... 
}

Looks suspicious:

  Resistance=pad*((1024.0 / RawADC) - 1);

surely?

  Resistance=pad*(1 - (1024.0 / RawADC));

but the thermistor is running backwards i.e when we heat it the output temperature goes down

You didn't show your schematic but presumably the thermistor and another resistor form a [u]voltage divider[/u]. If you swap the resistors (or swap the +5V and ground connections) that will invert the relationship.

You could also fix it in software, but it would be more complex than flipping the +/- sign because a voltage divider isn't linear.

Why did you find it necessary to post the exact same question again? That's not very grateful to the people who tried to help you, is it?
http://forum.arduino.cc/index.php?topic=447329.msg3077809

The form of the equation should either be (1-V)/V or V/(1-V), depending on which way up the
divider is.

(I've assumed V ranges 0..1, the 1 will probably be 5.0 or 1024 depending on units).

Duplicate threads merged.

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: