Temperature sensor is negative

FIXED! Posted the new code below.
I have 2 sensors a KY-013 and a WPSE320. The K gives me a negative output. The W gives a correct output. What am I doing wrong?
I connected them both the same way to the Arduino

-23.78, 21.86
-23.78, 21.86
-23.78, 21.86
-23.78, 21.86
-23.55, 21.86
-23.78, 21.86
-24.25, 21.86
-23.78, 21.86
-23.78, 21.86
-23.78, 21.86
-23.78, 21.86
-23.78, 21.86
const int thermistorPin1 = A0; // analog pin the first thermistor is connected to
const int thermistorPin2 = A1; // analog pin the second thermistor is connected to
int Vo1, Vo2;
float R1 = 10000; // value of the fixed resistor connected in series with the thermistor
float logR2_1, R2_1, T1;
float logR2_2, R2_2, T2;
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; // Steinhart-Hart coefficients

void setup() {
    pinMode(thermistorPin1, INPUT); // set the first thermistor pin as input
    pinMode(thermistorPin2, INPUT); // set the second thermistor pin as input
    Serial.begin(9600);
}

void loop() {
    // Read the voltage on the first thermistor pin
    Vo1 = analogRead(thermistorPin1);
    // Calculate the resistance of the first thermistor
    R2_1 = R1 * (1023.0 / (float)Vo1 - 1.0);
    logR2_1 = log(R2_1);
    // Calculate the temperature of the first thermistor in Kelvin
    T1 = (1.0 / (c1 + c2*logR2_1 + c3*logR2_1*logR2_1*logR2_1));
    T1 = T1 - 273.15; // convert Kelvin to Celsius
    // T1 = (T1 * 9.0)/ 5.0 + 32.0; // convert Celsius to Fahrenheit
    

    // Read the voltage on the second thermistor pin
    Vo2 = analogRead(thermistorPin2);
    // Calculate the resistance of the second thermistor
    R2_2 = R1 * (1023.0 / (float)Vo2 - 1.0);
    logR2_2 = log(R2_2);
    // Calculate the temperature of the second thermistor in Kelvin
    T2 = (1.0 / (c1 + c2*logR2_2 + c3*logR2_2*logR2_2*logR2_2));
    T2 = T2 - 273.15; // convert Kelvin to Celsius
    // T2 = (T2 * 9.0)/ 5.0 + 32.0; // convert Celsius to Fahrenheit
    Serial.print(T1);
    Serial.print(", ");
    Serial.println(T2);

    delay(500);
}

Thankyou for using code tags correctly! But please do not post images of serial monitor. Always copy the text from serial monitor and paste that into your post using code tags.

Can you please post links to the data sheets for the 2 sensors?

I noticed that the sensors are different but you are using, I think, identical code/calculation for each sensor. My theory is that the code or calculation is only suitable for one of the sensors and not the other.

Changed the screenshot to a copy and paste in code tags.
KY:


WPSE:

You fixed your code? Please post the updated version in case it is useful to other forum members in the future.

Oh im sorry I meant changed the image to copied text

Oh, ok. Thanks for that.

The specs you posted are not enough. Please post links to the specs or data sheets of the exact modules you are using.

Often, example Arduino code is provided by vendors of these modules. I guess this is where you got the code you are using. However, the code for the other module may be different in some way.

Also please post a schematic showing how you wired the modules to your Arduino. Some photos may also be useful, but only if they are clear, bright, and it is possible to see how everything is connected (this is not easy to show clearly in a photo, which is why a schematic is also needed).

I found the following schematic for the KY-013:

We can see that there is a 10K pull-down resistor.

I could not find a schematic for the WPSE320. However, the spec you posted says "pull-up resistor provided: 10K"

So, one module has a pull-up resistor and the other has a pull-down resistor. The same code cannot be used for both sensors.

KY-013 Analog Temperature Thermistor Module ENG.pdf (1,1 MB)
I have this one. I used this code for both sensors. But the WPSE320 only gives the correct output with this code. The KY is the negative one.
WPSE320

Try this change

Vo1 = 1023 - analogRead(thermistorPin1);

Thanks. The schematic in your link is different to the schematic I found. Let us assume the schematic in your link is the correct one.


Here we see a pull-up resistor, but it is 1K, not 10K.

Your code has this line:

float R1 = 10000; // value of the fixed resistor connected in series with the thermistor

This value is not correct for the KY-013. However, it may be correct for the WPSE320.

Ah you're correct! If I Switch the fixed resistor value in the code it gives me the indeed a positive temperature. Does the lower fixed resistor influences the precision of my measurements? The KY-013 manual also says something about using an additional resistor?

Probably not the value. The precision of that fixed resistor may affect the precision of your measurements, and also the precision of the thermistors themselves.

Fixed Code

const int thermistorPin1 = A0; // analog pin the first thermistor is connected to
const int thermistorPin2 = A1; // analog pin the second thermistor is connected to
int Vo1, Vo2;
float R1 = 10000; // value of the fixed resistor WPSE320
float R3 = 1000; // value of the fixed resistor KY-013
float logR2_1, R2_1, T1;
float logR2_2, R2_2, T2;
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; // Steinhart-Hart coefficients

void setup() {
    pinMode(thermistorPin1, INPUT); // set the first thermistor pin as input
    pinMode(thermistorPin2, INPUT); // set the second thermistor pin as input
    Serial.begin(9600);
}

void loop() {
    // Read the voltage on the first thermistor pin
    Vo1 = analogRead(thermistorPin1);
    // Calculate the resistance of the first thermistor
    R2_1 = R3 * (1023.0 / (float)Vo1 - 1.0);
    logR2_1 = log(R2_1);
    // Calculate the temperature of the first thermistor in Kelvin
    T1 = (1.0 / (c1 + c2*logR2_1 + c3*logR2_1*logR2_1*logR2_1));
    T1 = T1 - 273.15; // convert Kelvin to Celsius
    // T1 = (T1 * 9.0)/ 5.0 + 32.0; // convert Celsius to Fahrenheit
    

    // Read the voltage on the second thermistor pin
    Vo2 = analogRead(thermistorPin2);
    // Calculate the resistance of the second thermistor
    R2_2 = R1 * (1023.0 / (float)Vo2 - 1.0);
    logR2_2 = log(R2_2);
    // Calculate the temperature of the second thermistor in Kelvin
    T2 = (1.0 / (c1 + c2*logR2_2 + c3*logR2_2*logR2_2*logR2_2));
    T2 = T2 - 273.15; // convert Kelvin to Celsius
    // T2 = (T2 * 9.0)/ 5.0 + 32.0; // convert Celsius to Fahrenheit
    Serial.print(T1);
    Serial.print(", ");
    Serial.println(T2);

    delay(500);
}



Okay Thankyou!

No problem.

If you are getting readings from the two sensors that are a few degrees different, this is normal. Cheap components sold for use with Arduino are not individually calibrated at the factory, so there will always be differences in the readings between two "identical" sensors, and your sensors are not identical.

You could apply a calibration adjustment to one of the readings to make them give the same value at room temperature. But at higher or lower temperatures, they would probably begin to drift apart again.

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