j0h

Steinhart for temp probe

j0h
Oct 29th, 2025
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. # Convert resistance (0–50kΩ) to temperature in °C
  2. def res_to_celsius(resistance):
  3.     # Ensure valid input range
  4.     if resistance <= 0:
  5.         resistance = 1e-6  # Avoid log(0)
  6.     elif resistance > POT_RESISTANCE:
  7.         resistance = POT_RESISTANCE
  8.  
  9.     # Beta equation: T = 1 / (1/T0 + (1/B) * ln(R/R0)) - 273.15
  10.     steinhart = math.log(resistance / THERMISTOR_NOMINAL)
  11.     steinhart /= B_COEFFICIENT_OUTPUT
  12.     steinhart += 1.0 / (TEMPERATURE_NOMINAL + 273.15)
  13.     steinhart = 1.0 / steinhart
  14.     celsius = steinhart - 273.15
  15.  
  16.     # Clamp to physical range (-7.22°C to 100°C)
  17.     return Tclamp(celsius, Tminn, Tmaxn)
Advertisement
Add Comment
Please, Sign In to add comment