EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / For thermistors, use Hart and

For thermistors, use Hart and

|

Microcontroller › AVR › Linearisation of TC & RTD › For thermistors, use Hart and

December 28, 2018 at 9:38 am #14992
Sabin
Participant

For thermistors, use Hart and Steinhart Equation. RTDs are quite linear though may need to correct cold junction issues with proper resistors
 ……can use the following code –

  1. #include <SPI.h>
  2. #include "Adafruit_MAX31855.h"
  3.  
  4. #define DO 12
  5. #define CS 11
  6. #define CLK 10
  7. Adafruit_MAX31855 thermocouple(CLK, CS, DO);
  8.  
  9. void setup() {
  10. Serial.begin(9600);
  11. Serial.println("MAX31855 test");
  12. // wait for MAX chip to stabilize
  13. delay(500);
  14. }
  15. void loop() {
  16. // Initialize variables.
  17. int i = 0; // Counter for arrays
  18. double internalTemp = thermocouple.readInternal(); // Read the internal temperature of the MAX31855.
  19. double rawTemp = thermocouple.readCelsius(); // Read the temperature of the thermocouple. This temp is compensated for cold junction temperature.
  20. double thermocoupleVoltage= 0;
  21. double internalVoltage = 0;
  22. double correctedTemp = 0;
  23.  
  24. // Check to make sure thermocouple is working correctly.
  25. if (isnan(rawTemp)) {
  26. Serial.println("Something wrong with thermocouple!");
  27. }
  28. else {
  29. // Steps 1 & 2. Subtract cold junction temperature from the raw thermocouple temperature.
  30. thermocoupleVoltage = (rawTemp – internalTemp)*0.041276; // C * mv/C = mV
  31.  
  32. // Step 3. Calculate the cold junction equivalent thermocouple voltage.
  33.  
  34. if (internalTemp >= 0) { // For positive temperatures use appropriate NIST coefficients
  35. // Coefficients and equations available from http://srdata.nist.gov/its90/download/type_k.tab
  36.  
  37. double c[] = {-0.176004136860E-01, 0.389212049750E-01, 0.185587700320E-04, –0.994575928740E-07, 0.318409457190E-09, –0.560728448890E-12, 0.560750590590E-15, –0.320207200030E-18, 0.971511471520E-22, –0.121047212750E-25};
  38.  
  39. // Count the the number of coefficients. There are 10 coefficients for positive temperatures (plus three exponential coefficients),
  40. // but there are 11 coefficients for negative temperatures.
  41. int cLength = sizeof(c) / sizeof(c[0]);
  42.  
  43. // Exponential coefficients. Only used for positive temperatures.
  44. double a0 = 0.118597600000E+00;
  45. double a1 = –0.118343200000E-03;
  46. double a2 = 0.126968600000E+03;
  47.  
  48.  
  49. // From NIST: E = sum(i=0 to n) c_i t^i + a0 exp(a1 (t – a2)^2), where E is the thermocouple voltage in mV and t is the temperature in degrees C.
  50. // In this case, E is the cold junction equivalent thermocouple voltage.
  51. // Alternative form: C0 + C1*internalTemp + C2*internalTemp^2 + C3*internalTemp^3 + … + C10*internaltemp^10 + A0*e^(A1*(internalTemp – A2)^2)
  52. // This loop sums up the c_i t^i components.
  53. for (i = 0; i < cLength; i++) {
  54. internalVoltage += c[i] * pow(internalTemp, i);
  55. }
  56. // This section adds the a0 exp(a1 (t – a2)^2) components.
  57. internalVoltage += a0 * exp(a1 * pow((internalTemp – a2), 2));
  58. }
  59. else if (internalTemp < 0) { // for negative temperatures
  60. double c[] = {0.000000000000E+00, 0.394501280250E-01, 0.236223735980E-04, –0.328589067840E-06, –0.499048287770E-08, –0.675090591730E-10, –0.574103274280E-12, –0.310888728940E-14, –0.104516093650E-16, –0.198892668780E-19, –0.163226974860E-22};
  61. // Count the number of coefficients.
  62. int cLength = sizeof(c) / sizeof(c[0]);
  63.  
  64. // Below 0 degrees Celsius, the NIST formula is simpler and has no exponential components: E = sum(i=0 to n) c_i t^i
  65. for (i = 0; i < cLength; i++) {
  66. internalVoltage += c[i] * pow(internalTemp, i) ;
  67. }
  68. }
  69.  
  70. // Step 4. Add the cold junction equivalent thermocouple voltage calculated in step 3 to the thermocouple voltage calculated in step 2.
  71. double totalVoltage = thermocoupleVoltage + internalVoltage;
  72.  
  73. // Step 5. Use the result of step 4 and the NIST voltage-to-temperature (inverse) coefficients to calculate the cold junction compensated, linearized temperature value.
  74. // The equation is in the form correctedTemp = d_0 + d_1*E + d_2*E^2 + … + d_n*E^n, where E is the totalVoltage in mV and correctedTemp is in degrees C.
  75. // NIST uses different coefficients for different temperature subranges: (-200 to 0C), (0 to 500C) and (500 to 1372C).
  76. if (totalVoltage < 0) { // Temperature is between -200 and 0C.
  77. double d[] = {0.0000000E+00, 2.5173462E+01, –1.1662878E+00, –1.0833638E+00, –8.9773540E-01, –3.7342377E-01, –8.6632643E-02, –1.0450598E-02, –5.1920577E-04, 0.0000000E+00};
  78.  
  79. int dLength = sizeof(d) / sizeof(d[0]);
  80. for (i = 0; i < dLength; i++) {
  81. correctedTemp += d[i] * pow(totalVoltage, i);
  82. }
  83. }
  84. else if (totalVoltage < 20.644) { // Temperature is between 0C and 500C.
  85. double d[] = {0.000000E+00, 2.508355E+01, 7.860106E-02, –2.503131E-01, 8.315270E-02, –1.228034E-02, 9.804036E-04, –4.413030E-05, 1.057734E-06, –1.052755E-08};
  86. int dLength = sizeof(d) / sizeof(d[0]);
  87. for (i = 0; i < dLength; i++) {
  88. correctedTemp += d[i] * pow(totalVoltage, i);
  89. }
  90. }
  91. else if (totalVoltage < 54.886 ) { // Temperature is between 500C and 1372C.
  92. double d[] = {-1.318058E+02, 4.830222E+01, –1.646031E+00, 5.464731E-02, –9.650715E-04, 8.802193E-06, –3.110810E-08, 0.000000E+00, 0.000000E+00, 0.000000E+00};
  93. int dLength = sizeof(d) / sizeof(d[0]);
  94. for (i = 0; i < dLength; i++) {
  95. correctedTemp += d[i] * pow(totalVoltage, i);
  96. }
  97. } else { // NIST only has data for K-type thermocouples from -200C to +1372C. If the temperature is not in that range, set temp to impossible value.
  98. // Error handling should be improved.
  99. Serial.print("Temperature is out of range. This should never happen.");
  100. correctedTemp = NAN;
  101. }
  102.  
  103. Serial.print("Corrected Temp = ");
  104. Serial.println(correctedTemp, 5);
  105. Serial.println("");
  106.  
  107. }
  108.  
  109. delay(1000);
  110.  
  111. }

RSS Recent Posts

  • Getting into an LED bulb April 21, 2026
  • understanding of resonance in time domain April 21, 2026
  • Beginner Questions About CNC Machines – G-code, Control Systems & Accuracy April 21, 2026
  • A Must-Watch Video Showing Dangerous Construction of Cheap Lithium-Ion Cells April 21, 2026
  • S1MJ ? April 20, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise