Calculating NTC temperature

MrChips

Joined Oct 2, 2009
30,802
ADC reading on AN0, with a 33K resistor, and calculations according to post #60.

Rich (BB code):
x=adc_raw_ch0;
a3=0.00000018624250;
a2=-0.00025641150597;
a1=0.19058077255347;
a0=-55.86182880225911;

temperature1 = a3*pow(x,3)+a2*pow(x,2)+a1*x+a0;
ADC reading | T0 | Commercial
770 | 24.0 | 25.8|
633 | 9.2 | 13.1|
575 | 4.0 | 9.4|
564 | 3.5 | 6.7|
515 | -0.3 | -0.3|
510 | -0.6 | -0.6|
496 | -1.7 | -2.6|
495 | -1.8 | -2.6|
Comments:

1. There is a typo error in a3. It should be

a3 = 0.00000018524250;

You should avoid typing in numbers in order to avoid mistakes. Use copy and paste.

2. The commercial (reference) reading vs ADC reading don't fall on a nice smooth curve. There is sufficient variability to suggest that something is not quite right.

How much time are you waiting before taking a measurements?
How stable are the ADC counts? What is the observable variation?
How stable is the commercial (reference) reading?
What are you using for a power supply? How many volts?
Do you have proper power supply decoupling at the MCU and at the thermistor? 0.1μF and 10μF at both VDD and thermocouple VDD.
What is your mcu clock source and frequency?
What is your ADC clock frequency?

The maximum source resistance suggested is 10kΩ. I don't know what effect this should have with regards the readings not falling on the curve.
 

Thread Starter

nerdegutta

Joined Dec 15, 2009
2,684
Comments:

1. There is a typo error in a3. It should be

a3 = 0.00000018524250;

You should avoid typing in numbers in order to avoid mistakes. Use copy and paste.
OK, I got MPLAB on a different computer than the one I'm surfing the Net on.
2. The commercial (reference) reading vs ADC reading don't fall on a nice smooth curve. There is sufficient variability to suggest that something is not quite right.

How much time are you waiting before taking a measurements?
About 5 seconds.
How stable are the ADC counts? What is the observable variation?
If I remove the 5 sec delay, the ADC variation is 1-3 values.
How stable is the commercial (reference) reading?
It has a refresh rate at about 15-16 seconds.
What are you using for a power supply? How many volts?
My DMM shows 4.98-4.99 volts. A plug in the wall, down to 15vdc and a 7085 regulator with capacitors on input pin and output pin. 470uF - 0.1uF - 7805 - 0.1uF - 10uF
Do you have proper power supply decoupling at the MCU and at the thermistor? 0.1μF and 10μF at both VDD and thermocouple VDD.
No, I don't. I will add them now.
What is your mcu clock source and frequency?
What is your ADC clock frequency?
This part I'm not sure I got right.
Configuration bits:
Rich (BB code):
#define _XTAL_FREQ 20000000

// Configuration bits
#pragma config FOSC = INTIO67 		//     FOSC = INTIO67       Internal oscillator block
#pragma config PLLCFG = ON 			//     PLLCFG = ON          Oscillator multiplied by 4
#pragma config PRICLKEN = ON 		//     PRICLKEN = ON        Primary clock enabled
#pragma config FCMEN = OFF 			//     FCMEN = OFF          Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF 			//     IESO = OFF           Oscillator Switchover mode disabled
#pragma config PWRTEN = ON 			//     PWRTEN = ON          Power up timer enabled
#pragma config BOREN = OFF 			//     BOREN = OFF          Brown-out Reset disabled in hardware and software
#pragma config WDTEN = OFF 			//     WDTEN = OFF          Watch dog timer is always disabled. SWDTEN has no effect.
#pragma config CCP2MX = PORTB3 		//     CCP2MX = PORTB3      CCP2 input/output is multiplexed with RB3
#pragma config PBADEN = OFF 		//     PBADEN = OFF         PORTB<5:0> pins are configured as digital I/O on Reset
#pragma config CCP3MX = PORTC6 		//     CCP3MX = PORTC6      P3A/CCP3 input/output is mulitplexed with RC6
#pragma config HFOFST = ON 			//     HFOFST = ON          HFINTOSC output and ready status are not delayed by the oscillator stable status
#pragma config T3CMX = PORTB5 		//     T3CMX = PORTB5       T3CKI is on RB5
#pragma config P2BMX = PORTC0 		//     P2BMX = PORTC0       P2B is on RC0
#pragma config MCLRE = EXTMCLR 		//     MCLRE = EXTMCLR      MCLR pin enabled, RE3 input pin disabled
#pragma config STVREN = OFF 		//     STVREN = OFF         Stack full/underflow will not cause Reset
#pragma config LVP = OFF 			//     LVP = OFF            Single-Supply ICSP disabled
#pragma config XINST = OFF 			//     XINST = OFF          Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
#pragma config DEBUG = OFF 			//     DEBUG = OFF          Disabled
Oscillation configuration:
Rich (BB code):
void osc_conf(void){
 /*Primary Clock modes are selectable by the
FOSC<3:0> bits of the CONFIG1H Configuration
register. The primary clock operation is further defined
by these Configuration and register bits:
1. PRICLKEN (CONFIG1H<5>)
2. PRISD (OSCCON2<2>)
3. PLLCFG (CONFIG1H<4>)
4. PLLEN (OSCTUNE<6>)
5. HFOFST (CONFIG3H<3>)
6. IRCF<2:0> (OSCCON<6:4>)
7. MFIOSEL (OSCCON2<4>)
8. INTSRC (OSCTUNE<7>)
*/

 OSCCON2bits.PRISD=1;			// Primary Oscillator Circuit Shutdown
 OSCTUNEbits.PLLEN=1;			// Primary Clock Freq 4x multiplier
 OSCCONbits.IRCF=0b111;			// Internal Freq selection bits
 OSCCONbits.SCS=3;				// Main system clock selection
 OSCCON2bits.MFIOSEL=0;			// MFIOSEL not used
 OSCTUNEbits.INTSRC=0;			// Internat Frequency Selection bit
} // end osc_conf


The maximum source resistance suggested is 10kΩ. I don't know what effect this should have with regards the readings not falling on the curve.
Neither do I.
 

Thread Starter

nerdegutta

Joined Dec 15, 2009
2,684
Do you have proper power supply decoupling at the MCU and at the thermistor? 0.1μF and 10μF at both VDD and thermocouple VDD.
The ADC readings are not varying so much now, after adding the capacitors.


(Commercial reads both 25.6°C)

ADC0 = AN0 = 33K resistor. This is the T0.
ADC4 = AN4 = 10K resistor. This is the T4.

Calculations:
Rich (BB code):
x=adc_raw_ch0;
a3=0.00000018624250;
a2=-0.00025641150597;
a1=0.19058077255347;
a0=-55.86182880225911;

temperature1 = a3*pow(x,3)+a2*pow(x,2)+a1*x+a0;

temperature2 = 0.0942833 * adc_raw_ch4 - 22.9818;
temperature1 = T0
temperature2 = T4
 

Attachments

Last edited:

MrChips

Joined Oct 2, 2009
30,802
Now you've changed the range on me.
I was going on the assumption that about -10 to 40C would do.
Then I tried to accommodate -40 to 60C.
Now you're asking for -40 to 100C?
Every extended range will present increasing difficulty because of the non-linearity.
You have two choices:
1. Go with a limited range for best accuracy. You can have multiple ranges.
2. Go with one catch all range -40 to 100C but accept lower accuracy across the range.
 

MrChips

Joined Oct 2, 2009
30,802
The new range is -10 to 60C.
I take the mid point of the range = 25C and set the load resistance to the thermistor value at this temperature.

Hence the load resistance, i.e the resistor from the thermistor to ground, is 10kΩ.

Here are the new coefficients:

a3 = 0.00000016801831
a2 = -0.00022614472747
a1 = 0.18880568667086
a0 = -34.98200380534273

You don't have to type in all those digits. Up to 5 significant digits should be ok.
For example, a0 = -35.0

Here is the curve:

 
Top