Using NTC thermistor

Hi,
I bought this (https://robu.in/product/4pin-ntc-thermistor-temperature-sensor-module/ ) thermistor. Im new to arduino. i directly connected GND, A0 and VCC to arduino (without any resister).
I followed the code from thermistor interfacing with arduino temperature meter and got analog value in the range of 550 (connected to 3.3v supply). But the temperature is coming as ~21C.

Is this correct? To me, it looks less than what it is supposed to be (as the room feels warmer than 21C).

Appreciate any help.

Thanks,
Pragalathan M

Note: When i change to 5v supply, the analog reading is ~830 and goes to -5.7C.

Given what you have told us, I would say yes, it is probably correct and you have a fever!

Why don't you compare it to another thermometer of some kind, instead of guessing based on your feelings, or asking people who are not even in the room with you?

The tutorial you linked to connects the sensor Vcc to 5V, not 3.3V, so the code will have been written assuming that. This may be why you are not getting the temperature you expect. Why you do not get a correct reading when you connect 5V I cannot say. Please post a schematic diagram showing how you connected the circuit and some clear, bright photos in case we can spot any wiring mistakes.

1 Like

// #define DIGITAL_INPUT 3                              // defining digital input at pin 3
#define ANALOG_INPUT   A0                       // defining analog input at A0

int    digital_output ;                                                        // This will read the digital value
int    analog_output ;                                                       // This will read the analog value
int    revised_output;                                                      // variable to store the corrected value

float  temp_C ;                                                                  // Variable for storing the temperature
float  temp_f ;                                                                   // Variable for storing the fahrenheit

void setup ( )                                                                     // Anything written I it will run once.
{
  // pinMode ( DIGITAL_INPUT, INPUT ) ;                   // declaring pin 3 as input
  pinMode ( ANALOG_INPUT, INPUT )  ;                 // declaring A0 as input pin

  Serial.begin ( 9600 ) ;                                                     // selecting the baud rate at 9600
  Serial.println (" microcontrollerslab.com : The data is " ) ;
}

void loop ( )                                                                                        // Anything written in it will run continuously
{
  analog_output = analogRead ( ANALOG_INPUT ) ;         // Reading the analog value and storing in analog_output

  Serial.print ( " Analog value: " ) ;                                       
  Serial.print ( analog_output, DEC) ;                                    // This will display the analog value

  // The module has thermistor connection reversed
   revised_output= map ( analog_output, 0, 1023, 1023, 0 ) ;

   temp_C    = Thermistor ( revised_output ) ;
  temp_f = ( temp_C * 9.0 ) / 5.0 + 32.0 ;


// This will print the temperature
  Serial.print ( " Temperature: " ) ;
  Serial.print ( temp_f, 1 ) ;                                           // This will display the temperature in Fahrenheit
  Serial.print (" F  " ) ;

  Serial.print  (temp_C, 1 ) ;                                          // This will display the temperature in Celcius
  Serial.println (" C " ) ; 

  delay ( 10000 ) ;                                                                   // Wait for 1 second
}

double Thermistor ( int RawADC )
{
  double Temp ;
  

  Temp = log (10240000 / RawADC - 10000);
  Temp = 1 / ( 0.001129148 + ( 0.000234125 * Temp ) + ( 0.0000000876741 * Temp * Temp * Temp ) ) ;
  Temp = Temp - 273.15 ;            // This will Convert Kelvin to Celcius

  return Temp ;
}

Please find the code and the picture of the wiring.

There are errors in the code too

  • you don’t use pinMode with analog inputs .
  • the procedure starting “double” doesn’t seem to be used and RawADC doesn’t do anything either - it should represent your analog read ??? You only print values taken from the ADC not after conversion to temperature by the equations (I think!)

I would use the 5v as the ADC also uses that as it’s voltage reference so if the 5v varies so does the voltage to your sensor and any variation cancels out .

It is possible that the module you have is different to that in the project ( eg thermistor type different ) you followed and so the equations are wrong - that might be worth following up. Check on the output range of your board and theirs , and see what you get at different temperatures . Have a look at the series resistor used with the thermistor ( if you can find it - should be 100k.)

You might be better off calibrating it yourself and then fitting a curve to it , the accuracy “out of the box” won’t be great due to component tolerances ( very cheap parts are used in these modules , which maybe out of spec)

If you want a simple temperature measurement system a DS18b20 is worth a look and will give you an accurate reading

These modules (especially the added circuitry) always seemed a little bit unusual to me. They use a comparator circuit to modify the analog output to a digital one? It is lacking a solid explanation...

This specific circuit is used on all sorts of modules these days.

The page says:

The output format: Digital switching output (0 and 1).

Maybe someone here has experience with this specific sensor...

Experience of similar , there are schematics about .
There is an analog output , which I think is raw from the voltage divider ( thermistor and 100k?) . The op amp is a comparator taking an input from the pot and from the voltage divider to give you a digital out with a switching point set by the pot .

This line from the loop method calls the Thermistor method.

This line from the Thermistor method, uses the RawADC variable.

when i use 5v, output goes to ~830 and Celsius goes to -5.7C.

true. this formula may not be suitable for this thermistor. However since this being my first arduino project, i dont know how to proceed further; nor i know much about thermistor except their purpose.

It's unnecessary, but it's not an error.

It's used in loop().

Ermmm...???

@pragalathan-m try using 5V for the module and change this line:

temp_C    = Thermistor ( analog_output ) ;

@PaulRB I changed it to 5v and the line to

here is the output:

Analog value: 842 Temperature: 147.1 F  63.9 C

Soz if I got that bit about the code wrong - hard to see on my phone ( my excuse anyhow )and I didn’t see the procedure call. ( it looked like “thermistor” was a reserved word as it was not black )
My bad .

That module says “ 20 to80 deg”. Some of those on eBay are given as “-20 to 125C”
You might be better off buying a good bare thermistor with known curve; using that and it’s characteristic to design your code - there are examples about . You don’t know what thermistor you have or what resistor with it on your module .
Really a ds18b20 is much easier .

Unless you do it on A7 on a Nano. That will set the pinMode() on Pin 9.

1 Like

IMO, a reading assignment for using thermistors:
Steinhart–Hart equation - Wikipedia

The above is easily implemented even on the long-in-tooth UNO. Using a 10K NTC with a 10K series resistor:


  // average all the samples
  float average = 0;
  for (i=0; i< 5; i++) {
     average += samples[i] ;
  }
  average /= 5 ;
  average = 1023 / average - 1 ;
  average = 10000.0 / average ;
  float steinhart ;
  steinhart = average / 10000.0 ;      // (R/Ro)
  steinhart = log(steinhart) ;         // ln(R/Ro)
  steinhart /= 3950.0 ;                // 1/B * ln(R/Ro)
  steinhart += 1.0 / (25.0 + 273.15) ; // + (1/To)
  steinhart = 1.0 / steinhart ;        // Invert
  steinhart -= 273.15 ;                // convert to C
  return (steinhart) ;
}

My friend Dr. Peter Anderson (Pete is deceased) has a set of notes that the more inquisitive my find of interest:
https://www.phanderson.com/picaxe/lin_thermistor.html

Ray

Oh well, sorry, it was just a crazy theory! Undo that change.

That's pretty weird! Why pin 9? And what if you use pinMode() on A6? Which random other pin does that affect?

Thermistors are designed to have a known resistance at a temperature of 25C. For example, a "NT10K" thermistor is designed to have a resistance of 10K Ohms at 25C. Others may have a resistance of 100K at 25C for example. The curves of resistance Vs temperature are usually similar, and pass through that known fixed point of 10K @ 25C or whatever.

Can you see any part number printed on the thermistor on your board? You will need bright light and a strong magnifying lens to read it, I think.

Because the lookup tables only have 20 entries (Pins 0 to 19). When you fetch the entry for Pin 21 (A7) you go off the end of the tables by two bytes.

When you go off the end of the table for mapping pin number to PORT ( const uint8_t PROGMEM digital_pin_to_port_PGM[]) you get a byte that means "PORTB".

When you go off the end of the table for mapping pin number to bit mask ( const uint8_t PROGMEM digital_pin_to_port_PGM[]) you get a byte 0x02 (bit 1).

The result is PORTB pin 1 (PB1) or Arduino Pin 9.

The first byte after the bitmask table is 0x00 so no bits are changed.

1 Like

@PaulRB This is what i got from the IC. please see if this is useful.

We already know the IC is an LM393 comparator.

My question is can you read a number printed on the thermistor? The thermistor is the shiny black bead on long leads.

You probably did not realise it, but because you are using the AO analog output, you are only using two components on the sensor board: the thermistor and a fixed resistor (marked "103" = 10K Ohms). All the other components are only used when you use the DO digital output.