AN2017 PSoC 1 Temperature Measurement with Thermistor.pdf

AN2017
PSoC® 1 Temperature Measurement With Thermistor
Author: David Van Ess
Associated Project: Yes
Associated Part Family: CY8C28xxx
®
Software Version: PSoC Designer™ 5.4
Related Application Notes: AN66477
If you have a question, or need help with this application note, contact the author at
[email protected]
®
AN2017 shows how to use PSoC 1 to accurately measure temperature with a thermistor. The associated project
measures the resistance of a thermistor to calculate its temperature using lookup tables and equations, and is also used
with other PSoC 1 devices that have the required resources.
Introduction
Thermistors: A Primer
A thermistor is a temperature-sensitive resistor in which
resistance varies with temperature. There are two types of
thermistors: positive temperature coefficient (PTC)
thermistors and negative temperature coefficient (NTC)
thermistors. This application note describes the more
commonly used NTC thermistors, in which resistance
decreases with increase in temperature. Based on this
principle, temperature is calculated by measuring the
resistance. Thermistors are available as elements, probes,
and in packages designed for specific end applications.
Their resistances can vary typically from a few Ω to
several kΩ.
The variation of resistance with temperature for a
thermistor is nonlinear. Figure 1 shows a typical resistance
versus temperature of a thermistor.

Essential





ACDINCVR User Module
PGA User Module
Two analog output buffers
Analog Multiplexer
200
Resistance (kΩ)
150
100
50
125
110
95
80
65
50
35
20
5
-10
-25
0
-40
This application note comes with an associated project,
which measures the resistance of the thermistor. The
temperature is then calculated using an equation or a
lookup table. This application note also comes with an
excel sheet that calculates the Steinhart-Hart constants
and builds the lookup table. This application uses the
following PSoC 1 resources:
Figure 1. Resistance versus Temperature Curve of
Thermistor NCP18XH103F03RB
Temperature (°C)
As explained earlier, a NTC thermistor is a semiconductor
device that becomes less resistive as its temperature
increases. The change in resistance is roughly expressed
by the following equation.
R(t1)
 A(t1t 2)
R(t 2)
Equation 1
Optional

Display (LCD)
Where:
A is an empirical constant less than one.
t1 and t2 are two different temperatures.
R(t1) and R(t1) are the resistances at these temperatures.
www.cypress.com
Document No. 001-40882 Rev. *D
1
PSoC® 1 Temperature Measurement With Thermistor
“Roughly,” in this case, means that it is a great equation
for some academic introduction to semiconductor
materials, but will not do for any real world,
temperature-measuring application.
Some datasheets provide the three Steinhart coefficients
(A, B, and C). Other datasheets provide “Temperature
coefficient” (Alpha) values, “Sensitive index” (Beta) values,
or both. Although the Alpha or Beta coefficients can
determine temperature, they are limited to a specific
temperature range for which they are specified. The
Steinhart-Hart equation does not have this limitation.
The Steinhart-Hart equation describes the resistance
change of a semiconductor thermistor as related to its
temperature. The following equation shows it to be a
third-order logarithmic polynomial using three constants.
1
 A  B  ln( R)  C  ln( R)3
TK
Because the parameters provided for thermistors can
vary, their usage and interchangeability in an application
can be complicated. To address this issue, the attached
AN2017_S_H_Constant_Calc.xls file, calculates the
required A, B, C Steinhart-Hart coefficients, based on the
resistance versus temperature table or curve available in
datasheets. If the resistance versus temperature value is
not provided in the datasheet, users can measure them on
a test bench.
Equation 2
Where:
A, B, and C are empirical constants.
R is the thermistor’s resistance in Ω.
Reading Ohms the PSoC Way
TK is the temperature in kelvin.
The setup to measure the resistance of a thermistor using
PSoC 1 is shown in Figure 2. PSoC 1 Output Buffers and
Input Multiplexer are connected to significantly remove
gain and offset errors from the resistance calculation.
A more useful equation shows the temperature in Celsius.
TC 
1
 273.15 Equation 3
A  B  ln( R)  C  ln( R)3
Temperature calculations are only as accurate as the
resistance measurement of the thermistor.
Figure 2. Measuring Ohms the PSoC Way
Analog Buffer
(AnalogOutBuf_0)
V0
P0[3]
PSoC®1
VrefHi
V1
P0[1]
Mux
Rtherm
Input
ADC
AGND
PGA
Rref
V2
Analog Buffer
(AnalogOutBuf_1)
P0[5]
VrefLo
The current through Rref also flows through the thermistor,
which give us:
V0  V1 V1  V2

Rtherm
Rref
www.cypress.com
Equation 4
Solving for Rthermistor, we get the following equation:
 V  V1 

Rtherm  Rref *  0
 V1  V2 
Document No. 001-40882 Rev. *D
Equation 5
2
PSoC® 1 Temperature Measurement With Thermistor
As shown in the previous equation, any offset errors in the
measurement system are removed by the subtraction of
two measured voltages. The ratio of these two different
values removes any measurement path gain error. This
leaves the measurement error to be determined by Rref.
The reference resistor’s accuracy requirement is
determined by the specific application requirements.
This is valid as long as the measured signal is never
outside the range of the ADC. To guarantee this the PGA
is set for a gain slightly less than unity.
Interface With PSoC 1
Figure 2 shows that there are only two discrete
components required outside the PSoC 1:


The thermistor
The generated lookup table is shown in Purple.
Note:

The lookup table is generated for the temperature
range 0 °C to 80 °C in steps of 1 °C.

Smaller the difference between upper and lower
bound, more reliable will be the values of the
Steinhart-Hart constants generated.
Table 2. Steinhart-Hart Coefficients for
NCP18XH103F03RB
Steinhart-Hart Coefficient
Value
A
0.000891358
B
0.000250618
C
0.000000197
The reference resistor
Thermistor
For this application, a NCP18XH103F03RB thermistor is
selected. This thermistor is available on the CY8CKIT-025
Temperature Sensor EBK. It has the following
specifications:


The Steinhart-Hart constants are calculated and shown in
the Blue blocks
10,000 Ω at 25 °C
Reference Resistor
Given an ADC of finite resolution, the most accurate
measure is made when:
Rthermistor  Rref
–40 °C to +125 °C operating range
Calculate the Steinhart-Hart Constants for the Thermistor
using the following equation.
1
 A  B  ln( R)  C  ln( R)3
TK
Equation 6
To use the Equation 6, to calculate the temperature with
the measured resistance, the three Steinhart-Hart
constants A, B, C are required. To solve for the three
constants we need three equations. Table 1 gives the
resistance of the thermistor at three different
temperatures. These values have been taken from the
device’s datasheet.
Table 1. Three Data Points for NCP18XH103F03RB
Thermistor
Temperature (°C)
Resistance (Ω)
0
27219
25
10,000
80
1669


Temperature in Red Blocks
Resistance in Green Blocks
www.cypress.com
When the equation holds true, each resistor has half of the
ADC’s range across it. Half of the range effectively cuts
the resolution by one bit. If one resistance becomes four
times bigger than the other, then 80% of the range is
across the larger resistance and 20% across the smaller.
80% of the range is effectively a reduction of resolution of
one third of a bit. A 20% range reduces the resolution by
over two bits.
The problem is that the thermistor resistance, over
temperature varies several decades in magnitude. Table 1
verifies this.
For this case, a reference resistor of 10 kΩ is selected for
the most resolution at 25 °C. With the ADC set for 13 bits,
the resolution of the reference resistor and thermistor at
three different temperatures is shown in Table 3:
Table 3. Effective Resolution for a 13-Bit ADC
°C
Feed the data from Table 1 in the appropriate location in
the attached excel sheet AN2017_S_H_Constant_Calc.xls
Equation 7
Rthermistor
Rthermistor
Rreference
Rreference
Ohms
ADC
Resolution
Ohms
ADC
Resolution
0
27219
13 Bits
10,000
8 Bits
25
10,000
12 Bits
10,000
12 Bits
80
1669
8 Bits
10,000
13 Bits
With a 1% tolerance in thermistor resistance, eight bits of
resolution is adequate.
Document No. 001-40882 Rev. *D
3
PSoC® 1 Temperature Measurement With Thermistor
For this example, the architecture in Figure 1 is used. The
reference resistor is selected to be 10 kΩ. Because the
thermistor has an uncertainty of 1%, choosing a tolerance
of 0.1% for the reference resistor removes any error it can
contribute.
Figure 3. Routing Analog Out Buffer to a Pin
Sample Project
Figure 3 shows the User Module placement, following the
schematic in Figure 1.
PGA User Module - Buffer
The Buffer is a PGA User Module placed in ACB00. It is
connected to the multiplexer AMUX4, which connects to
external pins.
Software enables ACC00’s testmux to connect VrefHigh to
the column 0 analog bus. Select AnalogOutBuf_0 in the
Interconnect View to bring this reference out to P0[3] as
shown in Figure 3.
PGA User Module - RefLow
A second PGA User Module in ACC01 as a placeholder.
The gain stage is not used. The sole purpose is to allow
access to the testmux. Software enables ACC01’s testmux
to connect VrefLow to the column 1 analog bus. Select
AnalogOutBuf_1 in the Interconnect View to bring this
reference out to P0[5] (similar to the Figure 3).
ADCINCVR User Module
The analog block of the ADCINCVR is placed just below
the PGA User Module - Buffer, from which it receives the
signal. The clock for the ADCINCVR, with a 13-bit
resolution, is set to 333 kHz for a sample rate of 10 sps.
This sample rate causes any 60 Hz or 50 Hz interference
to be removed from the signal (sampling at a sub-multiple
of a frequency will reject that frequency). The sample rate
can be increased if the application requires a faster
conversion.
AM U X 4 U s e r M o d u l e – AM U X 4
AMUX4
User
Module
is
placed
at
AnalogColumn_InputMux_0. AMUX4 is used to switch
between the three pins P0[1], P0[3], and P0[5].
LCD User Module
LCD is used to display the calculated temperature and
resistance of the thermistor.
www.cypress.com
Document No. 001-40882 Rev. *D
4
PSoC® 1 Temperature Measurement With Thermistor
Figure 4. User Module Placement
AMUX4
(AnalogColumn_InputMux_0)
Analog Output Buffer
(AnalogOutBuf_0)
Analog Output Buffer
(AnalogOutBuf_1)
www.cypress.com
Document No. 001-40882 Rev. *D
5
PSoC® 1 Temperature Measurement With Thermistor

Firmware
The firmware is written in C and is explained in this
section.
Float Math

Start

Plug the thermistor constants into the SteinhartHart equation to calculate the temperature. This
has the advantage of being the most accurate. Its
disadvantage is that it requires floating-point math.
The Steinhart-Hart coefficients are calculated in
the attached AN2017_S_H_Constant_Calc.xls file.
Long Math

Using the Steinhart-Hart coefficients, calculate a
table of temperature versus resistance over the
range required (This is done in the attached
AN2017_S_H_Constant_Calc.xls file). This table
can be in line integers. Finer resolution can be
obtained if required. This has the advantage of
being faster to calculate. The disadvantage is the
ROM space used to store the table and is less
accurate.
Both techniques are valid; it is up to the user to
decide which best fits his or her application. For this
example, both methods are implemented. The user
has to comment out the line of code in the header file
thermistor.h to select the method of measurement.
Thermistor_Start()
Measure_Resistance()
Calculate_Temperature()
#define LONG_MATH
or
#define FLOAT_MATH
4.
Display_Results()
1.
2.
3.
Thermistor_Start()
Display_Results()
a.
The measured temperature and resistance of the
thermistor are displayed on the LCD.
b.
This function can be updated if the user wants to
procure data through another interface such as
2
UART/I C.
a.
Performs the required initialization for the User
Modules involved.
Add the files thermistor.c and thermistor.h to the project.
Copy the code given below into main.c.
b.
Enables ACC00’s testmux to connect VrefHigh to
the column 0 analog bus.
#include "m8c.h"
#include "PSoCAPI.h"
#include "thermistor.h"
c.
Enables ACC01’s testmux to connect VrefLow to
the column 1 analog bus.
Measure_Resistance()
a.
This function as stated measures the resistance
of the thermistor.
b.
The voltage at P0[3], P0[1] and P0[5] is
measured.
c.
The resistance value is calculated and stored to
be processed further. The value is calculated by
both long or float math using Equation 5.
Calculate_Temperature()
After measuring the thermistor resistance, it must be
converted to a temperature value. This can be done
either with float or long math:
www.cypress.com
void main(void)
{
M8C_EnableGInt;
Thermistor_Start();
while(1)
{
Measure_Resistance();
Calculate_Temperature();
Display_Results();
}
}
Note Update the Steinhart-Hart coefficients and the LUT
(LUT is needed only if LONG_MATH is used for
calculations)
from
the
attached
file,
Document No. 001-40882 Rev. *D
6
PSoC® 1 Temperature Measurement With Thermistor
AN2017_S_H_Constant_Calc.xls, which was updated as
explained in section Thermistor.
A comparison of the two methods for measurement and
calculation is given in Table 4.
Hardware Connection
Table 5 lists the hardware connections between the two
boards:
Table 5. Hardware Connections
Table 4. Comparison of Long and Float Methods
Wire
Method
Time Taken
ROM Used
Long
310 ms
3779 kB
Float
340 ms
7966 kB
Evaluate the Example Project
1.
Build the associated project for the required type of
method to be used i.e., Long_Math or Float_Math.
(Refer the section Firmware).
2.
Program the CY8CKit-001.
3.
Connect the CY8CKit-025 to CY8CKit-001 as shown
in the Table 4.
4.
Power the board and observe the results on the LCD.
CY8CKit-001
CY8CKit-025
RefHi
Red
P0[3]
P0[0]
Signal
Yellow
P0[1]
P0[1]
RefLow
Black
P0[5]
GND_A
LCD
N/A
Port_2
N/A
Figure 5. CY8CKit-025 Connected to CY8CKit-001
Figure 6. Thermistor User module in the Catalog
Thermistor User Module
PSoC Designer 5.4 includes a thermistor user module in
the user module catalog (see Figure 6), which implements
the ‘Long-math’ method explained in the application note’s
example project for temperature measurement using a
thermistor.
www.cypress.com
Document No. 001-40882 Rev. *D
7
PSoC® 1 Temperature Measurement With Thermistor
The user module provides the user option to have either
VSS as RefLo signal or RefMux’s RefLo signal as the
RefLo for the thermistor connection (see Figure 7).
Figure 7. Thermistor User Module RefLo Selection
The user module wizard generates the LUT automatically
without
your
entering
the
values
in
the
AN2017_S_H_Constant_Calc.xls file and copying the
values in to the code. You need to enter resistance values
at three temperature settings – Min, Mid, and Max
temperature setting – in the user module wizard as shown
in Figure 8.
Figure 8. Thermistor User Module Wizard
Reference
Resistor value
Resistance values to
generate the LUT
Thermistor Pin
For details on the user module and its usage, refer to the
Thermistor user module datasheet.
www.cypress.com
Document No. 001-40882 Rev. *D
8
PSoC® 1 Temperature Measurement With Thermistor
How to Improve the Resolution?
More resolution can be obtained with use of multiple
reference resistors. Figure 3 shows architecture to allow
multiple reference resistors.
Figure 9. Selectable Reference Resistors
Analog Buffer
(AnalogOutBuf_0)
VrefHi_1
V0
Rref_1
Analog Buffer
(AnalogOutBuf_1)
VrefHi_2
V1
PSoC®1
V2
Mux
Rref_2
Input
ADC
AGND
PGA
RTherm
Analog Buffer
(AnalogOutBuf_2)
V3
VrefLo
When AnalogOutBuf_0 is disabled and AnalogOutBuf_1 is
driven, the reference resistance is Rref_1. V1 is sensed
through Rref_2. When AnalogOutBuf_1 is disabled and
AnalogOutBuf_0 is driven, the reference resistance is
Rref_1 + Rref_2.
Although this architecture allows better resolution, it does
so at the cost of an external pin, a resistor, and an extra
buffer (and its power).
Self-Heating of Thermistor
Self-heating is a phenomenon in which the thermistor
temperature increases because of the current flow through
it. This self-heating introduces an error in the measured
temperature. The self-heating effect is provided as
dissipation factor (mW/°C) in the datasheet. It is defined
as the power required to raise the temperature of the
www.cypress.com
thermistor by 1 °C above the ambient temperature and is
expressed as Equation 6.
Dissipation Factor = TPower / Terror
Equation 8
Where TPower is the power supplied to the thermistor and
Terror is the difference between the ambient and the
measured temperature value.
The NCP18XH103F03RB thermistor, considered as an
example in this application note, has a dissipation factor of
1 mW/°C. Consider voltages supplied for V0 and Vss,
shown in Figure 1, are 3.9 V and 1.3 V. Solving Equation 6
for a thermistor with resistance of 10 kΩ at 25 °C, the
temperature error (Terror) is 0.17 °C. This error can be
decreased by decreasing the reference voltage range, and
thus the current flow through the thermistor.
Document No. 001-40882 Rev. *D
9
PSoC® 1 Temperature Measurement With Thermistor
Summary
The right circuit topology makes it possible to measure a
resistance with its accuracy determined by a single
reference resistor. An understanding of the Steinhart-Hart
equation makes conversion to temperature, either by
calculation or table lookup, a straightforward task.
Document History
®
Document Title: PSoC 1 Temperature Measurement With Thermistor - AN2017
Document Number: 001-40882
Revision
ECN
Orig. of
Change
Submission
Date
Description of Change
**
1536344
JVY
10/07/2007
OLD APP. NOTE: Obtain spec. # for note to be added to spec. system.
*A
3155592
YARA
01/27/2011
Changed the title.
Added Calculations section.
Updated the content.
*B
3260418
YARA
06/14/2011
Added a section on self-heating.
*C
3692836
ADIY
07/26/2012
Created and excel file to calculate Steinhart-Hart constants and the Lookup Table.
Updated for Thermistor NCP18XH103F03RB on CY8CKit-025.
Updated associated project to CY28xxx.
Updated firmware to be more modular.
Removed reference to obsolete Application Notes.
Updated template.
*D
4323179
MSUR
03/27/2014
Added section on Thermistor User module
Updated project to PSoC Designer 5.4
www.cypress.com
Document No. 001-40882 Rev. *D
10
PSoC® 1 Temperature Measurement With Thermistor
Worldwide Sales and Design Support
Cypress maintains a worldwide network of offices, solution centers, manufacturer’s representatives, and distributors. To find
the office closest to you, visit us at Cypress Locations.
PSoC® Solutions
Products
Automotive
cypress.com/go/automotive
psoc.cypress.com/solutions
Clocks & Buffers
cypress.com/go/clocks
PSoC 1 | PSoC 3 | PSoC 5
Interface
cypress.com/go/interface
Lighting & Power Control
cypress.com/go/powerpsoc
cypress.com/go/plc
Memory
cypress.com/go/memory
Optical Navigation Sensors
cypress.com/go/ons
PSoC
cypress.com/go/psoc
Touch Sensing
cypress.com/go/touch
USB Controllers
cypress.com/go/usb
Wireless/RF
cypress.com/go/wireless
Cypress Developer Community
Community | Forums | Blogs | Video | Training
Technical Support
cypress.com/go/support
PSoC is a registered trademark of Cypress Semiconductor Corp. "Programmable System-on-Chip," and PSoC Designer are trademarks of Cypress
Semiconductor Corp. All other trademarks or registered trademarks referenced herein are the property of their respective owners.
Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone
Fax
Website
: 408-943-2600
: 408-943-4730
: www.cypress.com
© Cypress Semiconductor Corporation, 2007-2014. The information contained herein is subject to change without notice. Cypress Semiconductor
Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any
license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or
safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as
critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The
inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies
Cypress against all charges.
This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide
patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a
personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative
works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress
integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source
Code except as specified above is prohibited without the express written permission of Cypress.
Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the
right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or
use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a
malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems
application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.
Use may be limited by and subject to the applicable Cypress software license agreement.
www.cypress.com
Document No. 001-40882 Rev. *D
11