[SOLVED] NTC Thermistors

Hello all

Is it possible to use more than one NTC thermistor in the code?
My first one is showing the temperature for a boiler, and I need to get the temperature from a second probe, for a steam arm.

I actually use the code from the library NTC_Thermistor, and I can't see how to rename like probe 1 and probe 2, not using the same caracteristics.

Thanks for your reply.

show. i help to rename.

1 Like

You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

//PRE-SETUP************************************************************************************************************************
#if 1//                                                                                                                           *
#include <Adafruit_GFX.h>//                                                                                                       *
#include <MCUFRIEND_kbv.h>//                                                                                                      *
MCUFRIEND_kbv tft;//                                                                                                              *
#include "ICONES_RGB.h"//                                                                                                         *
#include <TouchScreen.h>//                                                                                                        *
#define MINPRESSURE 100//                                                                                                         *
#define MAXPRESSURE 2000//                                                                                                        *
const int XP = 6, XM = A2, YP = A1, YM = 7;//                                                                                     *
const int TS_LEFT = 924, TS_RT = 210, TS_TOP = 959, TS_BOT = 203;//                                                               *
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);//                                                                              *
Adafruit_GFX_Button espresso_btn, doubleespresso_btn, the_btn, cafe_btn, latte_btn, reglages_btn;//                               *
int pixel_x, pixel_y;//                                                                                                           *
bool Touch_getXY(void)//                                                                                                          *
{//                                                                                                                               *
    TSPoint p = ts.getPoint();//                                                                                                  *
    pinMode(YP, OUTPUT);//                                                                                                        *
    pinMode(XM, OUTPUT);//                                                                                                        *
    digitalWrite(YP, HIGH);//                                                                                                     *
    digitalWrite(XM, HIGH);//                                                                                                     *
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);//                                                                    *
    if (pressed) {//                                                                                                              *
        pixel_x = map(p.y, TS_LEFT, TS_RT, 0, 480); //                                                                            *
        pixel_y = map(p.x, TS_BOT, TS_TOP, 0, 320);//                                                                             *
    }//                                                                                                                           *
    return pressed;//                                                                                                             *
}//                                                                                                                               *
#define BLACK   0x0000//                                                                                                          *
#define BLUE    0x001F//                                                                                                          *
#define RED     0xF800//                                                                                                          *
#define GREEN   0x07E0//                                                                                                          *
#define CYAN    0x07FF//                                                                                                          *
#define MAGENTA 0xF81F//                                                                                                          *
#define YELLOW  0xFFE0//                                                                                                          *
#define WHITE   0xFFFF//                                                                                                          *
//                                                                                                                                *
//Sonde temp                                                                                                                      *
int decimalPrecision = 0;//                                                                                                       *
#include <Thermistor.h>//                                                                                                         *
#include <NTC_Thermistor.h>//                                                                                                     *
#include <AverageThermistor.h>//                                                                                                  *
#define SENSOR_PIN             A10//                                                                                              *
#define REFERENCE_RESISTANCE   5130//                                                                                             *
#define NOMINAL_RESISTANCE     3300//                                                                                             *
#define NOMINAL_TEMPERATURE    100//                                                                                              *
#define B_VALUE                1410//                                                                                             *
#define READINGS_NUMBER 20//                                                                                                      *
#define DELAY_TIME 5//                                                                                                            *
Thermistor* thermistor = NULL;//                                                                                                  *
//                                                                                                                                *
void majtemp(){//                                                                                                                 *
    Serial.println("MISE A JOUR TEMPERATURE");//                                                                                  *
    const double celsius = thermistor->readCelsius();//                                                                           *
    if (celsius <= 80){//                                                                                                         *
    tft.fillRect(153, 295, 175, 20, BLACK);//                                                                                     *
    tft.setTextColor(RED, BLACK);//                                                                                               *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(153, 295);//                                                                                                    *
    tft.print("TEMP:");//                                                                                                         *
    tft.setCursor(210, 295);//                                                                                                    *
    tft.print("TROP");//                                                                                                          *
    tft.setCursor(265, 295);//                                                                                                    *
    tft.print("BASSE");//                                                                                                         *
    Serial.println("--> TEMPERATURE TROP BASSE");//                                                                               *
    }else{//                                                                                                                      *
    tft.fillRect(153, 295, 175, 20, BLACK);//                                                                                     *
    tft.setTextColor(WHITE, BLACK);//                                                                                             *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(180, 295);//                                                                                                    *
    tft.print("TEMP:");//                                                                                                         *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(237, 295);//                                                                                                    *
    tft.print(celsius,decimalPrecision);//                                                                                        *
    tft.setTextSize(1);//                                                                                                         *
    tft.setCursor(277, 295);//                                                                                                    *
    tft.print("o");//                                                                                                             *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(287, 295);//                                                                                                    *
    tft.print("C");//                                                                                                             *
    Serial.println("-->");(celsius);//                                                                                            *
    }//                                                                                                                           *
    if (celsius <= 115){//                                                                                                        *
    digitalWrite(relaiselementchauffephase, LOW);//                                                                               *
    Serial.println("--> TEMP TROP BASSE: PHASE ELEMENT DE CHAUFFE ALIMENTE");//                                                   *
    tft.fillRect(153, 265, 175, 20, BLACK);//                                                                                     *
    tft.setTextColor(WHITE, BLACK);//                                                                                             *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(180, 270);//                                                                                                    *
    tft.print("EN CHAUFFE");//                                                                                                    *
    }else{//                                                                                                                      *
    digitalWrite(relaiselementchauffephase, HIGH);//                                                                              *
    Serial.println("--> TEMPERATURE OK: PHASE ELEMENT DE CHAUFFE NON ALIMENTE");//                                                *
    tft.fillRect(153, 265, 175, 20, BLACK);//                                                                                     *
    tft.setTextColor(WHITE, BLACK);//                                                                                             *
    tft.setTextSize(2);//                                                                                                         *
    tft.setCursor(156, 270);//                                                                                                    *
    tft.print("JERONIMO PRETE");//                                                                                                *
    }//                                                                                                                           *
}//                                                                                                                               *

The thermistor math is too easy to use a library. Put the math into a function and all your analog channels can use that single function.

Thermistor Experiments for Science Labs & Science Fair Projects (juliantrubin.com)

// Utility routines for GPS time by M. Ray Burnette

float TempCelsius (void) {
  uint8_t i;
  float average;
  int samples[] = { 0, 0, 0, 0, 0} ;
 
  // take 5 samples in a row, with a slight delay
  for (i=0; i<5; i++) {
   samples[i] = analogRead(0);
   delay(10);
  }
 
  // average all the samples
  average = 0;
  for (i=0; i< 5; i++) {
     average += samples[i] ;
  }
  average /= 5 ;
  average = 1023 / average - 1 ;
  average = 10000.0 / average ;
  float steinhart ;
  steinhart = average / 10000.0 ;      // (R/Ro)
  steinhart = log(steinhart) ;         // ln(R/Ro)
  steinhart /= 3950.0 ;                // 1/B * ln(R/Ro)
  steinhart += 1.0 / (25.0 + 273.15) ; // + (1/To)
  steinhart = 1.0 / steinhart ;        // Invert
  steinhart -= 273.15 ;                // convert to C
  return (steinhart) ;
}

Wow, when did that get fixed? It used to make a mess.
Since 2011 I use Select All then Copy to get the sketch text from the IDE tab
and then in the forum post window use </> and Paste the code

type or paste code here

Thanks
Ok but "thermistor" = A10, the name that we use to read is thermistor...
So, if I add a second thermistor in A11, I can't have the same name, you see what I mean ?

Use an array of 2 pin numbers. One name, 2 values to choose from.

@Jabberwohk

why did you post two code-sections? What is so hard about posting a complete sketch as a

single code-section ?

Stefan my code has 660 lines, sorry. I only copy paste the problem

rule zero: the bug is somewhere else than you think.
So please post the complete sketch

If somebody wants to support you and wants to do a test if his modified code compiles this user needs the complete sketch.

float TempCelsius (AnalogPinNumber) {

then

 samples[i] = analogRead(AnalogPinNumber);

For 10K NTC with 10K series resistor.

1 Like

The first is 3.3K with 5.1K serie resistor, and the second is 100K with a 100k serie resistor

Hi,
This code I wrote last week here will do it and it explains how to set up with all different combinations, annotated and with a convention that lets you add as many as your board allows.

/*
Measuring values from two thermistors

B57891S0103 (10K) Thermistor (Black Head) - referred to as THERM_R1
METALL 4,32K bias resistor - referred to as BIAS_R1

B57164K0103 (10K) Thermistor in use with METALL 4,32K bias resistor (Blue Head)
METALL 4,32K bias resistor - referred to as BIAS_R2

Written to make it easy to add as many as required
*/

#define BIAS_R1 4320 // bias resistor Ω value (METALL 4,32K) 
#define BIAS_R2 4320  
#define THERM_R1 10000 // thermistor Ω value @ 25C 
#define THERM_R2 10000 

int THERM_R1_Pin = A1; // ADC pin reference
int THERM_R2_Pin = A2;

int THERM_R1_ADC; // ADC variable (0-1023)
int THERM_R2_ADC; 

float THERM_R1_Pin_Voltage; // Voltage variable
float THERM_R2_Pin_Voltage;

float logTHERM_R1_PV, THERM_R1_PV, THERM_R1_TEMP_C, THERM_R1_TEMP_F;
float logTHERM_R2_PV, THERM_R2_PV, THERM_R2_TEMP_C, THERM_R2_TEMP_F;

// Steinhart-Hart and Hart Coefficient Values (_A-_C)
// have been calculated using online calculator
// https://www.thinksrs.com/downloads/programs/Therm%20Calc/NTCCalibrator/NTCcalculator.htm

float THERM_R1_A = 1.104082153e-03, THERM_R1_B = 2.361447651e-04, THERM_R1_C = 0.9594141481e-07; 
float THERM_R2_A = 1.306158783e-03, THERM_R2_B = 2.140448657e-04, THERM_R2_C = 0.9782409084e-07;  

void setup() {
Serial.begin(9600); // int serial @9600 bits per second
}

void loop() {

THERM_R1_ADC = analogRead(THERM_R1_Pin);  // ADC variable (0-1023)
THERM_R1_Pin_Voltage = THERM_R1_ADC *(3.3 /1024); // Voltage variable
// Equation for THERM_Rx_PV = ((Vin - Vout) * R2) / Vout where R2 is BIAS_Rx
// THERM_R1_PV = BIAS_R1 * (3.3 - (THERM_R1_ADC *(3.3 /1024.0)))/ (THERM_R1_ADC *(3.3 /1024.0));
// simplified to
THERM_R1_PV = BIAS_R1 * (3.3 - THERM_R1_Pin_Voltage)/ THERM_R1_Pin_Voltage;

// Steinhart and Hart Equation. 
// T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
// where:
// T = Temp in kelvins (THERM_Rx_TEMP_C)
// R is Resistance at T in ohms (THERM_R1_PV) - Note PV usage is present value
// A,B and C are Steinhart–Hart coefficients taken from the data sheets calculated using the link above

logTHERM_R1_PV = log(THERM_R1_PV);
THERM_R1_TEMP_C = (1.0 / (THERM_R1_A + THERM_R1_B*logTHERM_R1_PV + THERM_R1_C*logTHERM_R1_PV*logTHERM_R1_PV*logTHERM_R1_PV)); 
THERM_R1_TEMP_C = THERM_R1_TEMP_C - 273.15;
THERM_R1_TEMP_F = (THERM_R1_TEMP_C * 1.8) +32;

// The next code block is for THERM_R2 without the extensive comments above
THERM_R2_ADC = analogRead(THERM_R2_Pin);
THERM_R2_Pin_Voltage = THERM_R2_ADC *(3.3 /1024);
THERM_R2_PV = BIAS_R2 * (3.3 - THERM_R2_Pin_Voltage)/ THERM_R2_Pin_Voltage;
logTHERM_R2_PV = log(THERM_R2_PV);
THERM_R2_TEMP_C = (1.0 / (THERM_R1_A + THERM_R1_B*logTHERM_R2_PV + THERM_R1_C*logTHERM_R2_PV*logTHERM_R2_PV*logTHERM_R2_PV));  
THERM_R2_TEMP_C = THERM_R2_TEMP_C - 273.15;
THERM_R2_TEMP_F = (THERM_R2_TEMP_C * 1.8) +32;

// The following breaks out all values for debugging and calibration
// Comment out all the block or vice versa to have only temp or debug
// Note: Useful because can use multimeter to check THERM_Rx_PV

String str1_THERM_R1_ADC = "THERM_R1_ADC: ";
Serial.println(str1_THERM_R1_ADC + THERM_R1_ADC); // Raw ADC value
  delay(1000);
String str1_THERM_R2_ADC = "THERM_R2_ADC: ";
Serial.println(str1_THERM_R2_ADC + THERM_R2_ADC); // Raw ADC value
  delay(5000);

String str1_THERM_R1_Pin_Voltage = "THERM_R1_Pin_Voltage: ";
String str2_THERM_R1_Pin_Voltage = " V";
Serial.println(str1_THERM_R1_Pin_Voltage + THERM_R1_Pin_Voltage + str2_THERM_R1_Pin_Voltage); // thermistor 1 pin voltage
  delay(1000);
String str1_THERM_R2_Pin_Voltage = "THERM_R2_Pin_Voltage: ";
String str2_THERM_R2_Pin_Voltage = " V";
Serial.println(str1_THERM_R2_Pin_Voltage + THERM_R2_Pin_Voltage + str2_THERM_R2_Pin_Voltage); // thermistor 2 pin voltage
  delay(5000);

String str1_THERM_R1_PV = "THERM_R1_PV: ";
String str2_THERM_R1_PV = " Ω";
Serial.println(str1_THERM_R1_PV + THERM_R1_PV + str2_THERM_R1_PV); // thermistor Ω present value
  delay(1000);
String str1_THERM_R2_PV = "THERM_R2_PV: ";
String str2_THERM_R2_PV = " Ω";
Serial.println(str1_THERM_R2_PV + THERM_R2_PV + str2_THERM_R2_PV); // thermistor Ω present value
  delay(5000);

// The following is for temperature output in Celsius

String str1_THERM_R1_TEMP_C = "T1 Temp: ";
String str2_THERM_R1_TEMP_C = " C";
Serial.println(str1_THERM_R1_TEMP_C + THERM_R1_TEMP_C + str2_THERM_R1_TEMP_C); // Outputs Temperature in Celsius
  delay(1000);
String str1_THERM_R2_TEMP_C = "T2 Temp: ";
String str2_THERM_R2_TEMP_C = " C";
Serial.println(str1_THERM_R2_TEMP_C + THERM_R2_TEMP_C + str2_THERM_R2_TEMP_C); // Outputs Temperature in Celsius
  delay(5000);

// The following is for temperature output in Fahrenheit

String str1_THERM_R1_TEMP_F = "T1 Temp: ";
String str2_THERM_R1_TEMP_F = " F";
Serial.println(str1_THERM_R1_TEMP_F + THERM_R1_TEMP_F + str2_THERM_R1_TEMP_F); // Outputs Temperature in Fahrenheit
  delay(1000);
String str1_THERM_R2_TEMP_F = "T2 Temp: ";
String str2_THERM_R2_TEMP_F = " F";
Serial.println(str1_THERM_R2_TEMP_F + THERM_R2_TEMP_F + str2_THERM_R2_TEMP_F); // Outputs Temperature in Fahrenheit
  delay(5000);

}

You will need to have 2 equations OR pass into the function the Tr and Rr values (my choice.)

@ardyouino
lot's of Strings

How long did you run that code?

Haha @StefanL38. You are right, yeah but I like it because it works in the serial monitor every time, right number of lines, right spacing, no mis-formatted stuff.

And I don't use the serial monitor anyway in my program, it all gets logged but for debugging it provides something that looks exactly right which is what I wanted. I played with the delays and it comes out every single time.

It is not about the serial monitor. It is about eating up all RAM available in your microcontroller and if all RAM is in use starting to overwrite other variables which will make your code act very strange and you have no clue why

There is an alternative to String : ---- SafeString.
This name is program

1 Like

@StefanL38 I hadn't heard about SafeString, thanks for telling me about it.
I just got so pissed off with the code not outputting properly I wrote it like that.

Anyway, @Jabberwohk my code is easy to implement for 1, 2 or even 8 or more NTC thermistors. I sometimes have up to six, or possibly 8 so that's why I wrote it.

Just take into consideration what @StefanL38 says about using Strings.

@StefanL38 I will look into SafeString and let you know how it works out comparatively.