Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Convert resistance (0–50kΩ) to temperature in °C
- def res_to_celsius(resistance):
- # Ensure valid input range
- if resistance <= 0:
- resistance = 1e-6 # Avoid log(0)
- elif resistance > POT_RESISTANCE:
- resistance = POT_RESISTANCE
- # Beta equation: T = 1 / (1/T0 + (1/B) * ln(R/R0)) - 273.15
- steinhart = math.log(resistance / THERMISTOR_NOMINAL)
- steinhart /= B_COEFFICIENT_OUTPUT
- steinhart += 1.0 / (TEMPERATURE_NOMINAL + 273.15)
- steinhart = 1.0 / steinhart
- celsius = steinhart - 273.15
- # Clamp to physical range (-7.22°C to 100°C)
- return Tclamp(celsius, Tminn, Tmaxn)
Advertisement
Add Comment
Please, Sign In to add comment