You are not logged in

Log in into our community

Log in so you acess some hidden content

* Fields are required.

Register into our community!

Sign up now for some good content

* Fields are required.

Lost something?

Enter your username or email to reset your password.

* Fields are required.

Temperature Sensor

Posted on : Sun , 01 2015 by : virusi

Necessary Knowledge :

Basic ADC knowledge.

Voltage Divider knowledge.

Basic Rtos knowledge.

Basic UVision knowledge.

Basic C Langugage knowledge.

Basic Hardware knowledge.


A thermistor is a type of resistor whose resistance depends on the temperature changes. The word thermistor is a combination of words “thermal” and “resistor”.
Thermistor can be classified in two groups:

PTC(positive temperature coefficient) thermistor. Resistance increase with increase of the temperature.

NTC(negative temperature coefficient) thermistor. Resistance decreases with increase of the temperature.

Fig.1 NTC and PTC thermistor characteristic.

There are two fundamental ways to change the temperature of thermistor internally or externally. The temperature of thermistor can be changed externally by changing the temperature of surrounding media and internally by self-heating resulting from a current flowing through the device.
The dependence of the resistance on temperature can be approximated by following equation:


R is the resistance of thermistor at the temperature T (in K)
R0 is the resistance at given temperature T0 (in K) β is the material specific-constant

From the above formula we can extract the temperature value:


In general line this is all you need to know about thermistors so you can start working with them. If you don’t know the A , B and C constants you can visit thermistor calculator web page and where you can calculate desired constants.


In this practical example will take a look at the KY-013 Module. Basically it is a voltage divider made of a 10k NTC thermistor and a 10k resistance.
Connection Diagram :

Fig.2 KY-013 module.

We have 3 pins to connect to our board:

– 1 Pin “-“ this pin should be connected to board GND
– 2 Pin should be connected to the VCC(5V or 3.3V)
– 3 Pin “S” should be connected to the ADC input on out board.

Warning. If the voltage is decreasing instead of increasing or vice versa you should reverse the 1 and 2 Pins connections (1 Pin should be connected to VCC(5V or 3.3V) and the 2Pin should be connected to GND).

Electrical Circuit Diagram:

Fig.3 Voltage divider with a thermistor.

As we already know the Output voltage equals (Vout):


As you can see from the formula above if the R_1 will increase the output voltage will decrease and vice verse. If the thermistor is connected instead of R_2 and the thermistor resistance will increase the output voltage will decrease and vice versa and this is why the temperature will decrease instead of increasing.
From the formula above we can calculate the thermistor resistance which is used in the temperature calculation :


Code :

1. Configure the MCU (initialize the System, ADC, Port).

 	1. Initialize clocks 
	2. Initialize and create Rtos Tasks
	3. Initialize the ADC
	3. Initialize Ports
 static void ConfigureMCU(void)
 	/* Initialize clocks */
	/* Initialize I/O for LED's output*/
	/* Adc Init */
	/* Initialize Rtos */

2. Initialize and configure a 500ms task.

const portTickType	delay = ( 500 / portTICK_RATE_MS );
static void RtosInit(void)
	xTaskCreate( fnc_500msTask, "500msTask", delay, NULL , 1 , NULL );

	/* Start the scheduler so the tasks start executing. */

3. In the 500ms task read data from adc and convert ADC value to temperature.

double CTemperature = 0UL;
 double voltageVal = 0UL;
 double resitance = 0UL;
 double calculatedRes = 0UL;

 double AdcValToC(int RawADC)
   double res = 0UL;
   double Vo = 0UL;
   Vo = (RawADC)*(3.3/4095.0);

   calculatedRes = 10000*(3.3/Vo-1);

   res = log(10000*(3.3/Vo-1));
   res = 1 / (0.001126068758413 + ( 0.000234572594529 + (0.000000086312248 * res * res ))* res );
   res = res - 273.15;// Convert Kelvin to Celcius
   return res;
 static void fnc_500msTask(void *pvParameters)
 	portTickType xLastWakeTime;
 	xLastWakeTime = xTaskGetTickCount();

		/* read data from the ADC pin */

		//printf("%f sqrt %f\n",2.2,3.1);
		/* get input voltage */
		voltageVal = (double)((3.3/4095.0)*ADCval);
		/* get input resistance */
		resitance = (10000*(3.3 - voltageVal))/voltageVal;

		CTemperature = AdcValToC(ADCval);
		/* delay task for 500ms */	
	   	vTaskDelayUntil( &xLastWakeTime, delay );
		/* Allow the other sender task to execute. */

Download Link :

Interested in a working version of this project download NTC example.

External Links :

Last updated on Mon , 07 2015

This page is waiting for your comment.

Share and Leave a comment.

You must be logged in to post a comment.

Back to Top