NTC Thermistor Coefficient Calculator

Here's a sketch I created for calculating the 4 Steinhart-Hart Coefficients for a Thermistor NTC. This is the extended equation that provides slightly more accuracy and not the more common Classic equation.

In the Opening Menu select which method you want:
Method 1: Enter 4 pairs of Resistance/Temperature readings
Method 2: Direct enter the 4 Coefficients (Decimal or E-Notation)

Once either Methods are completed, then select "R-T Testing" to evaluate different resistances based on the coefficients.

This does require the Streaming.h Library Streaming

Hope this helps others.
Cheers,
Bwanna

#include <Streaming.h>  // http://arduiniana.org/libraries/streaming/
#include <MathHelpers.h>          //MUST BE ADD to
#include <MatrixMath.h>

#define N (4)

float ntcR1[4];
float ntcT1[4];
// matrices for calculations
mtx_type A[N][N];    //LN
mtx_type v[N];       //This is a row vector
mtx_type ntcTK1[N];  //This is the Temp converted to K
float Coefficient[N];
float T,Rt;
double logRt,logRt2,logRt3;
double Coef1;
double Coef2;
double Coef3;
double Coef4;

String termtext, CoefText;
String NTC_Type; 

void setup() {
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  Serial  << endl << "Select method for inputing NTC Data:" << endl
          << "'1' for R/T readings, '2' for Coefficients, '3' for R-T Testing:";
  while (Serial.available() == 0) {}
  int x = Serial.parseInt();
  termtext = Serial.readStringUntil('\n');
  switch (x) {
    case 1:
      NTC_Editor();
      break;
    case 2:
      Input_Coefficient();
      break;
    case 3:
      CalcTemp();
  }
}

void NTC_Editor() {  //CHECK SERIAL TERMINAL FOR MANUAL NTC INFO
int x;
  do {
    Serial  << endl << endl
            << "Input 4 NTC Resistor/Temp readings" << endl
            << "Resistance = ohms, Temp = Celsius" << endl
            << "Use Format 'R1,T1,R2,T2,R3,T3,R4,T4:'" << endl
            << "Example: 1500,34,406,74,250,90,190,100 (this is for common BWD WT5132 (T89) automotive Temp sensor)" << endl;
    while (Serial.available() == 0) {}  //Wait for user input
    if (Serial.available() >= 20) {     //Cycles through capturing MANUAL User input from Serial Terminal
      float r1 = Serial.parseFloat();         //Parses EACH integar
      float t1 = Serial.parseFloat();
      float r2 = Serial.parseFloat();
      float t2 = Serial.parseFloat();
      float r3 = Serial.parseFloat();
      float t3 = Serial.parseFloat();
      float r4 = Serial.parseFloat();
      float t4 = Serial.parseFloat(); 
      // assigns Arrays from R/T inputs
      ntcR1[0] = r1;
      ntcR1[1] = r2;
      ntcR1[2] = r3;
      ntcR1[3] = r4;
      ntcT1[0] = t1;
      ntcT1[1] = t2;
      ntcT1[2] = t3;
      ntcT1[3] = t4;
    }
    while (Serial.available() > 0) Serial.read();  // dump any extraneous input

    Print_RT_Array();

    for (int i = 0; i < N; i++) {
      ntcTK1[i] = 1 / (273.15 + ntcT1[i]);
    }
    Serial << endl << "Are these Correct? (1=Y or 2=N):";  //Verifies Input from Terminal
    while (Serial.available() == 0) {}
    Serial.readStringUntil('\n');
    x = Serial.parseInt();
  } while (x == 1);
  
  Serial << endl << "Calculating Coefficients" << endl;
// Calculating LOGs
  for (int i = 0; i < N; i++) {
    v[i] = i + 1;  // vector of sequential numbers
    for (int j = 0; j < N; j++) {
      switch (j) {
        case 0:  //left column fill
          A[i][j] = 1;
          break;
        case 1:
          A[i][j] = log(ntcR1[i]);
          break;
        case 2:
          A[i][j] = pow(log(ntcR1[i]), 2);
          break;
        case 3:
          A[i][j] = pow(log(ntcR1[i]), 3);
          break;
      }
    }
  }  
  // Calculating Coefficients
  Matrix.Invert((mtx_type *)A, N);
  Matrix.Multiply((mtx_type *)A, (mtx_type *)ntcTK1, 4, 4, 1, (mtx_type *)Coefficient);
  // Matrix.Print((mtx_type *)Coefficient, N, 1, "NTC Coefficients (Decimal)");
  Print_Coefficients();
  CopyCoefficients();
  PrintCoefficientsVariables();
  // TerminputSensorInfo();
}

void  Input_Coefficient() {
  int x, i, Charlength;
  double TextTest;
  Serial  << endl << "Enter Coefficient Values in DECIMAL format (0.1234567890)";
  for (i = 0; i < 4; i++) {
    do {                                                       //user Input Recieve and verification
      do {
        Serial  << endl << "Enter " << NTC_Type << " Coefficient " << i+1 << ":  ";
        while (Serial.available() == 0) {}           //Wait for user input
        termtext = Serial.readStringUntil('\n');     //Read user input into string variable
        if (termtext == "x") {break;} //EXIT
        TextTest = termtext.toDouble();
        Charlength = termtext.length();
        // Serial  << endl << "Test termtext: " << termtext << " has " << termtext.length() << " characters"             //TESTING
        //                   << endl << "Test TextTest: "; Serial.println(TextTest, 19);       //TESTING
      } while (Charlength <= 6);
      Serial  << termtext << " ->Correct? YES=1,NO=2: ";
      while (Serial.available() == 0) {}             //Wait for user input
      Serial.readStringUntil('\n');
      x = Serial.parseInt();
    } while (x == 1);                                          //Only "1" allowed to continue
    Coefficient[i] = termtext.toDouble();                     //Assigns verified value to array
  }
  CopyCoefficients();
  PrintCoefficientsVariables();
}

void CalcTemp () {
  Serial  << endl << endl << "Enter Resistance in Ohms to calculate Temperature in Celsius" << endl
          << "'-1' to Exit:" << endl;
  Rt = 0;
  while (Rt != -1.00) {
    Serial << "? ";
    while (Serial.available() == 0) {}
    termtext = Serial.readStringUntil('\n');  //Read user input into variable
    Rt = termtext.toFloat();
    logRt = log(Rt);
    double logRt2 = pow(logRt, 2);
    double logRt3 = pow(logRt, 3);
    T = (1 / (Coef1 +(Coef2*(logRt)) +(Coef3*(logRt2))+(Coef4*(logRt3)))) - 273.15;
    // T = round(T);
    Serial << Rt << " = " << T << "C" << endl;
  }
}

void CopyCoefficients() {  //Copies Coefficients into Variables
  int i;
  for (i = 0; i < 4; i++) {
      switch (i) {
        case 0:
          Coef1 = Coefficient[i];
          break;
        case 1:
          Coef2 = Coefficient[i];
          break;
        case 2:
          Coef3 = Coefficient[i];
          break;
        case 3:
          Coef4 = Coefficient[i];
          break;
      }
    // CoefText = sci(Coefficient[i], 9);
  }
}

void Print_Coefficients() {
  int i;
  Serial << endl << "NTC Coefficients (E-Notation)" << endl;
  for (i = 0; i < 4; i++)  {
    Serial.println(sci(Coefficient[i], 9));
  }
}
  
void PrintCoefficientsVariables() {
  Serial  << endl <<  "NTC Coefficients (Decimal)" << endl
          << "Coef1: ";
  Serial.println (Coef1, 17);
  Serial  << "Coef2: ";
  Serial.println (Coef2, 17);
  Serial  << "Coef3: ";
  Serial.println (Coef3, 17);
  Serial  << "Coef24: ";
  Serial.println (Coef4, 17);
  Serial.println();
}

void Print_RT_Array() {
  // Prints information entered into serial terminal
  Serial  << endl << "R-T Arrays:" << endl
          << "NTC Resistance" << endl;
  for (int i = 0; i < 4; i++) {
    Serial << ntcR1[i] << endl;
  }
  Serial  << endl << "NTC Temperature" << endl;
  for (int i = 0; i < 4; i++) {
    Serial << ntcT1[i] << endl;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.