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
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.
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).
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.
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);
}
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.