NTC Thermistor readings going down when actual temperature increases

Hallo, so I'm making a pulstruder using old parts from an CL-260 3D printer and i reached a stop. The same thermistor that gave me good readings till 300celsius on the printer, connected to the same boards gives me now some strange readings: when i heat up the thermistor the readings go down instead of up and the same happens when i cool it, i tried to fix the problem but i couldn't manage to do it. Ill be frank, i made most of my code using chatgpt, it is a good tool to understand basic ideas while also making things move and it worked great till now but i think the one i am using is not capable of writing the beta formula correctly and i have no idea how to do it.
If you know how to help me ill greatly appreciate it!
Here are some more INFO:
Thermistor type: NTC 10K-ohm Beta Value 3500

Here is the code i am using, it was mostly written poorly by chatgpt and then i had to try to fix it (it currently works fine except of the thermistor readings)

// Define pin connections & motor's steps per revolution
const int stepPin = 26;
const int dirPin = 28;
const int enablePin = 24;
const int heaterPin = 10;
const int coolerPin = 9;
const int blueLED = 4;
const int whiteLED = 5;
const int stepsPerRevolution = 200;
unsigned long stepDelay = 2000;

const int thermistorPin = 13;  // Pin connected to the thermistor (analog pin)

// Constants for thermistor calculation
const float BETA = 3500.0;  // Beta value for thermistor (adjust based on your thermistor)
const float R25 = 10000.0;  // Resistance at 25°C in ohms (adjust based on your thermistor)
const int ADC_MAX = 1023;   // Maximum ADC value for 10-bit ADC (0-1023)
const float VCC = 5.0;      // Supply voltage for the Arduino (adjust if needed)

// Calibration value to adjust raw thermistor reading (experimentally determined)
const float CALIBRATION_OFFSET = 0;

float currentTemperature;
float desiredTemperature;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(heaterPin, OUTPUT);
  pinMode(coolerPin, OUTPUT);

  digitalWrite(enablePin, LOW);  // Enable the stepper motor driver

  Serial.begin(9600);
  Serial.println("Enter desired temperature (in Celsius):");
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    input.trim();
    desiredTemperature = input.toFloat();

    Serial.print("Desired Temperature Set to: ");
    Serial.println(desiredTemperature);
  }

  // Read the thermistor value from pin 13
  int thermistorValue = analogRead(thermistorPin);

  // Calculate the voltage across the thermistor (Vout)
  float Vout = (thermistorValue / (float)ADC_MAX) * VCC;

  // Calculate the thermistor resistance using the voltage divider formula
  float resistance = (R25 * (VCC - Vout)) / Vout;

  // Calculate temperature using the Beta equation
  // Beta formula: 1/T = 1/T0 + (1/BETA) * ln(R/R0)
  // where T is in Kelvin, T0 is 298.15K (25°C), R0 is resistance at 25°C (R25)
  float temperatureK = 1.0 / (1.0 / 298.15 + (1.0 / BETA) * log(resistance / R25));  // In Kelvin
  float temperature = temperatureK - 273.15;                                         // Convert to Celsius

  // Apply calibration offset (if necessary)
  temperature += CALIBRATION_OFFSET;

  // Print current temperature for debugging
  Serial.print("Current Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  // Heater control logic
  if (temperature < desiredTemperature) {
    digitalWrite(heaterPin, HIGH);  // Keep heater on
    digitalWrite(coolerPin, LOW);   // Keep cooler off
    Serial.println("Heater ON");
  } else {
    digitalWrite(heaterPin, LOW);   // Turn off the heater
    digitalWrite(coolerPin, HIGH);  // Turn on the cooler
    Serial.println("Heater OFF");
  }

  // Motor control
  if (temperature == desiredTemperature) {
    for (int i = 0; i < stepsPerRevolution; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(stepDelay);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(stepDelay);
    }
  } else {
    for (int i = 0; i < stepsPerRevolution; i++) {
      digitalWrite(stepPin, LOW);
      delayMicroseconds(stepDelay);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(stepDelay);
    }
  }

  // LED logic based on temperature comparison
  if (temperature == desiredTemperature) {
    digitalWrite(blueLED, HIGH);
  } else {
    digitalWrite(blueLED, LOW);
  }

  digitalWrite(whiteLED, HIGH);
}

I didn't study your code...

I assume you put a fixed resistor in series with the thermistor to make a voltage divider? If the resistors are switched-around (or the voltage and ground reversed) the temperature reading will go up when you expect it to go down.

1 Like

Thanks for your reply, i did not use any voltage dividers, the thermistor is connected to the board as it was in its 3d printer days. BUT RN I JUST FIGURED IT OUT :))))))
I just needed to multiply the beta formula with -1 and now it works perfectly

Just as DVDdoug said. you will need need a voltage divider for it to work right. You are use the steinhart equation which only works with a divider.

Most unfortunately, the code you likely copied from somewhere assumes that you are using a voltage divider, with a 10K Ohm fixed resistor.

How could i simply insert a divider in the hardware?
I want to add that in his 3D printer years the thermistor was connected to the board the same way, whiteout divider, and it worked great. Maybe the ramps 1.4 has an internal divider?

4k7 though..

What board is it? Is it custom for your printer?

Supposed to be ramps 1.4
No? I don't own one personally.
There you have resistors, but with arduino you need to add it to your circuit.

To work properly, it must have had the required voltage divider somewhere. This equation in the code is for the case that the thermistor is connected between Vcc and the analog input, with a 10K resistor from analog input to GND.

  // Calculate the thermistor resistance using the voltage divider formula
  float resistance = (R25 * (VCC - Vout)) / Vout;
1 Like

It is a RAMPS 1.4 Shield connected to an Arduino Mega2560

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