Simple NTC-10K-3950 temperature measurement

Simple NTC-10K-3950 temperature measurement

preface

Temperature is a very important physical quantity, which is indispensable in both life and industry. We can obtain the cold and hot program of the object by measuring the temperature of the measured object through the temperature sensor. There are many kinds of temperature sensors, and NTC is undoubtedly the most widely used. It has the advantages of easy manufacture, low cost, good precision and stability. NTC is a negative temperature thermistor. We only need a simple circuit. The temperature of the measured object can be obtained by collecting the voltage through the ADC of MCU and converting it into temperature.

1, Overview

It is these advantages of NTC temperature sensor that make it applied to all walks of life, such as household appliances such as rice cooker, water dispenser, refrigerator, water heater and air conditioner. Therefore, it is necessary to master electronic technology. The figure below shows an external NTC-10K-3950 temperature sensor.

2, Circuit and principle

1. Circuit diagram

2. Principle overview

The figure above is a simple temperature measurement circuit, which only needs NTC to connect a 10K resistor in series, and the ADC of MCU collects RT at both ends of the power supply_ The resistance value of NTC can be obtained from the voltage on int through Ohm's law. The temperature value can be obtained through the thermometer or formula. If it is calculated through the formula, it is more troublesome and the error is larger than that of looking up the table. Therefore, looking up the table is a good implementation method. Although it will occupy more flash of MCU, it is a drop in the bucket for the current MCU. Why it was said that it has good accuracy and stability, mainly because it has a distance of hundreds of Ω per degree Celsius, so the epitaxial line can be long and not easy to be disturbed. If it is a PT100 temperature sensor, the difference between each degree Celsius is less than 1 Ω, which is easy to be disturbed by the circuit and extension line.

At the same time, the collected temperature accuracy also depends on the ADC of MCU, mainly including the resolution and reference voltage of ADC. The ADC accuracy of each MCU is different. For specific accuracy errors, you need to check the manual to see whether they meet your needs. As shown in the figure below, the ADC accuracy table of smart micro MM32F0130 series, The unit LSB refers to a 12 bit resolution ADC with a total of 4096 digital bits (the 12th power of 2 is equal to 4096). If the comprehensive error is 3.4LSB, under the 3.3V reference voltage, the error voltage is 3.3V / 4096 * 3.4LSB ≈ 3mV, and then push back through the NTC thermometer to see whether the error can be ignored.

The following figure shows the table value of a section of NTC thermometer. If the changes of the two intermediate values of 39 ° C and 40 ° C are compared, under the condition of 3.3V, the ADC sampling voltage at 39 ° C = 3.3V * 5.531K / (10K + 5.531K) ≈ 1.1752v, and the ADC sampling voltage at 40 ° C = 3.3V * 5.311K / (10K + 5.311K) ≈ 1.1447V, the total error is 3mV. Assuming that in the worst case, the voltage distance between the minimum value of 39 ° C and the maximum value of 40 ° C is about 15mV by calculation. If the requirements cannot be met, please calibrate the ADC and provide a stable and accurate voltage source for the ADC reference voltage. A special reference voltage IC can be used. Usually, TL431 adjustable regulator can be used to save cost.

3, Programming

1. Program code

#include "ntc_rv_table.h"
#include "adcx.h"

#define TABLE_SIZE  			181
#define ADC1_CONVDATA_SIZE		8

//NTC-10K-3950-B value
//-Resistance value table corresponding to 55 ~ 125 ° C (unit: Ω)
//Resistance to temperature conversion: the array subscript index minus 55 is the temperature value
const u32 g_rvTable[TABLE_SIZE] = {
	1034600, 959006, 889452, 825419, 766434, 											//-55~-51
	712066, 661926, 615656, 572934, 533466, 496983, 463240, 432015, 403104, 376320, 	//-50~-41
	351495, 328472, 307110, 287279, 268859, 251741, 235826, 221021, 207242, 194412, 	//-40~-31
	182460, 171320, 160932, 151241, 142196, 133750, 125859, 118485, 111589, 105139, 	//-30~-21
	99102, 93450, 88156, 83195, 78544, 74183, 70091, 66250, 66643, 59255, 				//-20~-11
	56071, 53078, 50263, 47614, 45121, 42774, 40563, 38480, 36517, 34665, 				//-10~-1
	32919, 																				//0
	31270, 29715, 28246, 26858, 25547, 24307, 23135, 22026, 20977, 19987, 				//1~10
	19044, 18154, 17310, 16510, 15752, 15034, 14352, 13705, 13090, 12507, 				//11~20
	11953, 11427, 10927, 10452, 10000, 9570, 9161, 8771, 8401, 8048, 					//21~30
	7712, 7391, 7086, 6795, 6518, 6254, 6001, 5761, 5531, 5311, 						//31~40
	5102, 4902, 4710, 4528, 4353, 4186, 4026, 3874, 3728, 3588, 						//41~50
	3454, 3326, 3203, 3085, 2973, 2865, 2761, 2662, 2567, 2476, 						//51~60
	2388, 2304, 2223, 2146, 2072, 2000, 1932, 1866, 1803, 1742, 						//61~70
	1684, 1627, 1573, 1521, 1471, 1423, 1377, 1332, 1289, 1248, 						//71~80
	1208, 1170, 1133, 1097, 1063, 1030, 998, 968, 938, 909, 							//81~90
	882, 855, 829, 805, 781, 758, 735, 714, 693, 673, 									//91~100
	653, 635, 616, 599, 582, 565, 550, 534, 519, 505, 									//101~110
	491, 478, 465, 452, 440, 428, 416, 405, 395, 384, 									//111~120
	374, 364, 355, 345, 337																//121~125
};

/**
 * @brief median filtering 
 * @param collection: Element collection
 * @param size: Collection size
 * @retval median
 */
u16 mid_filter(u16 *collection, u16 size)
{
	u16 e;
	u16 i, j;
	u8 sorted;
	
	i = 0;
	while(i < size - 1){//Number of bubbling trips
		sorted = 1;
		j = 0;
		while(j < size - 1 - i){//Number of trips
			
			if(collection[j] > collection[j + 1]){
				e = collection[j];
				collection[j] = collection[j + 1];
				collection[j + 1] = e;
				sorted = 0;
			}
			
			j++;
		}
		if(sorted) break;
		
		i++;
	}
	
	return collection[size / 2];
}

s8 ntc_get_temp(void)
{
	s16 i;
	u32 adcValue;
	float rValue;
	s8 temperature;
	u16 adc_buffer[ADC1_CONVDATA_SIZE];
	
	for(i = 0; i < ADC1_CONVDATA_SIZE; i++)
		adc_buffer[i] = GetAdcAverage(5);

	adcValue = mid_filter(adc_buffer, ADC1_CONVDATA_SIZE);
	rValue = 4095000.0;
	rValue = rValue / adcValue - 1000;
	adcValue = 10000000.0 / rValue;
	for(i = 0; i < TABLE_SIZE; i++) {
		if(adcValue > g_rvTable[i]) {
			temperature = i - 55;
			break;
		}
	}		

	return temperature;
}

2. Procedure description

Call NTC_ get_ The temp() function can obtain the temperature value, and the GetAdcAverage() function is the digital voltage collected by the ADC, and then through the mid_ Filter.
rValue = 4095000.0;
rValue = rValue / adcValue - 1000;
adcValue = 10000000.0 / rValue;

The above three statements are the conversion formula for obtaining NTC resistance. Why do you write this? This is the result of calculation. It is assumed that NTC passes through 10K resistance and the voltage on the power supply is consistent with the ADC reference voltage, which is 3.3V, that is, VCC = ADC_REF = 3.3V, then ADC acquisition voltage (network number of RT_INT in the circuit diagram) ADC_V = VCC * RT / (10K + RT). It is known that the ADC resolution of MCU is 12BIT, that is, the maximum digital quantity is 4095, then the ADC_V = ADC_REF * ADC_DATA / 4095, ADC_DATA is the digital voltage obtained by ADC. Combine the two formulas and solve the equation to obtain the RT resistance, VCC * RT / (10K + RT) = VCC * ADC_DATA / 4095. Finally, RT = 10000000 / (4095000 / ADC_DATA - 1000) is obtained by the formula. It is simplified that the result is only affected by one variable, excluding the variables related to power supply voltage, and the accuracy is better. The detailed formula calculation is shown in the figure below.

4, Summary

The circuit is simple and effective. It can accurately read out the temperature value of the measured object as long as it is matched with a small amount of code. It is sufficient in normal applications. If the accuracy is more accurate and the application environment is bad, it requires better stability. Operational amplifier and temperature compensation can be added, but it is better to use digital temperature sensor in these cases.

5, Download address

https://github.com/chengzidun/blog.git
https://download.csdn.net/download/chengzidun/21732373

Disclaimer: it is inevitable that the author's technical level is limited. If there is any mistake, he refuses to admit it. The technical materials in this paper are only suitable for learning and reference and cannot be used for business. If you want to use them for business, you don't have to tell me. It has nothing to do with me.

Keywords: Single-Chip Microcomputer

Added by sangamon on Sun, 19 Dec 2021 13:06:39 +0200