Dec-29-2020, 07:37 PM
I'm trying to write a script which uses the Steinhart-Hart equation to calculate the temperature of a thermistor from its measured resistance.
The formula is:
In this case:
a = 1.068981*10**-4
b = 2.120700*10**-4
c = 9.019537*10**-8
In my script, I have the formula as:
My version (using **3), returns this:
Temperature = 436.09 K
Temperature = 162.94 °C
Whereas the other version, using pow, returns this:
Temperature = 418.07 K
Temperature = 144.92 °C
So, what is the difference?
My full script:
The formula is:
1/T = A + B(ln(R)) + C(ln(R))³Where T is the temperature in Kelvin and A, B and C are given constants.
In this case:
a = 1.068981*10**-4
b = 2.120700*10**-4
c = 9.019537*10**-8
In my script, I have the formula as:
tk = 1/(a + b*math.log(r) + (c*math.log(r))**3)But, when trying to fix another problem, I found someone has written it like this:
temp = 1 / (a + b * math.log(r) + c * pow(math.log(r), 3))I thought both are technically the same, but when I tried the above example, it returns different values.
My version (using **3), returns this:
Temperature = 436.09 K
Temperature = 162.94 °C
Whereas the other version, using pow, returns this:
Temperature = 418.07 K
Temperature = 144.92 °C
So, what is the difference?
My full script:
# Resistance to temperature convertor for Betatherm 30K6A1* thermistor print("Resistance to temperature convertor for Betatherm 30K6A1* thermistor") print("====================================================================") print("") def main(): import math # Constants a = 1.068981*10**-4 b = 2.120700*10**-4 c = 9.019537*10**-8 # Input measured resistance value while True: try: r = float(input("Enter the measured resistance value (Ω): ")) except ValueError: print("Must be a numeric value...") continue else: break # Calculate temperature # This calculator uses the Steinhart-Hart equation # Formual: 1/T = A + B(ln(R)) + C(ln(R))³ tk = 1/(a + b*math.log(r) + (c*math.log(r))**3) # Alternate version found on the internet # tk = 1/(a + b*math.log(r) + c*pow(math.log(r),3)) tk = round(tk,2) print(f"Temperature = {tk} K") # Convert to celcius tc = tk-273.15 print(f"Temperature = {tc} °C") print("Resistance at 25 °C should be 30 kΩ") print("") # Restart? yay or nay? restart = input("Do you want to convert another value? Y/N: ") print("") if restart.lower() == 'y': main() else: print("Goodbye") main()