#define question

Hello,

I am trying to replace some #define that I found in an example code.
However, if I do so, I get the wrong or no reading from the NTC that I am addressing.

This version with the #define works perfectly fine

#define ntc_pin A15  // Pin,to which the voltage divider is connected
//#define vd_power_pin 2            // 5V for the voltage divider
#define nominal_resistance 50000  //Nominal resistance at 25⁰C
#define nominal_temeprature 25    // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 5            // Number of samples
#define beta 3970                 // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 12000                //Value of  resistor used for the voltage divider

int samples = 0;  //array to store the samples

void setup(void) {
  //pinMode(vd_power_pin, OUTPUT);
  Serial.begin(115200);  //initialize serial communication at a baud rate of 9600
}

// Working with single read Temperature seems off
void loop(void) {
  float value;
  value = analogRead(ntc_pin);
  Serial.print("Analog Read: ");
  Serial.println(analogRead(ntc_pin));
  delay(10);
  // Calculate NTC resistance
  value = 1023 / value - 1;
  value = Rref / value;
  Serial.print("Thermistor resistance ");
  Serial.println(value);
  float temperature;
  temperature = value / nominal_resistance;             // (R/Ro)
  temperature = log(temperature);                       // ln(R/Ro)
  temperature /= beta;                                  // 1/B * ln(R/Ro)
  temperature += 1.0 / (nominal_temeprature + 273.15);  // + (1/To)
  temperature = 1.0 / temperature;                      // Invert
  temperature -= 273.15;                                // convert absolute temp to C
  Serial.print("Temperature ");
  Serial.print(temperature);
  Serial.println(" *C");
  delay(2000);
}

The following version gives me no readings.
I want to change it to something like this:

#define ntc_pin A15  // Pin,to which the voltage divider is connected
//#define vd_power_pin 2            // 5V for the voltage divider
const int nominal_resistance = 50000;  //Nominal resistance at 25⁰C
const int nominal_temeprature = 25;   // temperature for nominal resistance (almost always 25⁰ C)
const int samplingrate = 5;            // Number of samples
const int beta = 3970;                 // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
const int Rref = 12000;                //Value of  resistor used for the voltage divider

int samples = 0;  //array to store the samples

void setup(void) {
  //pinMode(vd_power_pin, OUTPUT);
  Serial.begin(115200);  //initialize serial communication at a baud rate of 9600
}

// Working with single read Temperature seems off
void loop(void) {
  float value;
  value = analogRead(ntc_pin);
  Serial.print("Analog Read: ");
  Serial.println(analogRead(ntc_pin));
  delay(10);
  // Calculate NTC resistance
  value = 1023 / value - 1;
  value = Rref / value;
  Serial.print("Thermistor resistance ");
  Serial.println(value);
  float temperature;
  temperature = value / nominal_resistance;             // (R/Ro)
  temperature = log(temperature);                       // ln(R/Ro)
  temperature /= beta;                                  // 1/B * ln(R/Ro)
  temperature += 1.0 / (nominal_temeprature + 273.15);  // + (1/To)
  temperature = 1.0 / temperature;                      // Invert
  temperature -= 273.15;                                // convert absolute temp to C
  Serial.print("Temperature ");
  Serial.print(temperature);
  Serial.println(" *C");
  delay(2000);
}

any suggestions?

I can't immediately see what you have changed between the two sketches

What have I missed ?

Used const int to replace #defines.

50000 won't fit in a two byte signed int.

1 Like

as you do float math, make your constants float values

const float nominal_resistance = 50000;  //Nominal resistance at 25⁰C
const float nominal_temeprature = 25;   // temperature for nominal resistance (almost always 25⁰ C)

(if you define them as long, they will be promoted to floating point in the math formula anyway)

1 Like

You forgot to say, so I'll assume you are using an Uno.

On Uno, Mega and other 8-bit arduino, int cannot hold 50000.

Try

const unsigned int nominal_resistance = 50000;

(unsigned int can hold up to around 65,000, but that might still not be enough, depending on your formulae)

or

const long int nominal_resistance = 50000;

(long int can hold up to around 2,000,000,000)

I am not convinced that the second sketch was not changed after I posted my question, but it does not matter in the scheme of things

This is what I was missing.
Yes I use an Arduino Mega. As I never ran into this issue I wasn't aware of the limitations of "int.".

Thank you very much!

Also, since "nominal_resistance" will likely never be negative, use an unsigned 'int':

"unsigned long" or preferably "uint32_t"

Looking at usage

Signed or unsigned, you’ll get a float promotion anyway since value is a float.

That’s why I was suggesting to make them float constants anyway in this case

1 Like

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