Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created November 15, 2020 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/2537daecaab01fa60d4194bc6fe3d9b8 to your computer and use it in GitHub Desktop.
Save devilholk/2537daecaab01fa60d4194bc6fe3d9b8 to your computer and use it in GitHub Desktop.
Steinhart-Hart utilities for calculating the relationship between temperature and resistance of thermistors.
from math import log, exp, sqrt
cbrt = lambda x: x**(1/3)
invert = lambda x: 1/x
def solve_steinhart_hart_eq(R1, T1, R2, T2, R3, T3):
#Solving the Steinhart-Hart equation
#Source: https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation#Steinhart%E2%80%93Hart_coefficients
L1, L2, L3 = map(log, (R1, R2, R3))
Y1, Y2, Y3 = map(invert, (T1, T2, T3))
γ2 = (Y2 - Y1) / (L2 - L1)
γ3 = (Y3 - Y1) / (L3 - L1)
C = (γ3 - γ2) / (L3 - L2) / (L1 + L2 + L3)
B = γ2 - C * (L1**2 + L1*L2 + L2**2)
A = Y1 - (B + L1**2 * C) * L1
return A, B, C
def calculate_temperature_for_resistance(A, B, C, R):
return 1 / (A + B * log(R) + C * log(R) ** 3)
def calculate_resistance_for_temperature(A, B, C, T):
x = 1/(2*C) * (A - 1/T)
y = sqrt((B/(3*C))**3 + x**2)
return exp(cbrt(y - x) - cbrt(y + x))
def c_to_k(T):
return T + 273.15
def k_to_c(T):
return T - 273.15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment