I'm creating a solar water heater controller using a Raspberry Pi and a pair of DS18B20 thermal sensors.
Bench testing while building the system worked fine. I replaced my old failed controller with mine and things were going well until we had two consecutive days of sun and the storage tank temperature increased over 105 deg F.
The sensors began failing at high temperatures around 110 and would recover when they cooled.
The sensors I have are probably cheap implementations, so I'm ordering from a more reputable company, but in the meantime I decided to look into adding error handling to my python code.
What I found was that if I handled the errors, the code would keep running as opposed to locking up and even at higher temps, a successful reading would occur periodically so I could limp along during these events without the controller dying.
It's really a good idea to handle errors anyway, so here is a snippet of code for acquiring all sensors and then reading them.
I'm using a Raspberry Pi 5, so GPIZERO
This code is an example and will not run as is, but should get you on your way to gracefully handling errors on cheap DS18B20 sensors.
#GPIOZERO library
from gpiozero import LED
from time import sleep
# therm sensor library
from w1thermsensor import W1ThermSensor, SensorNotReadyError, NoSensorFoundError
from w1thermsensor.units import Unit
# Sensor instance, both sensors
sensor = W1ThermSensor()
# acuire 2 sensors
sensors = W1ThermSensor.get_available_sensors()
while(len(sensors) < 2):
sensors = W1ThermSensor.get_available_sensors()
print(f"Error: Missing sensors")
for sensor in sensors:
print("Found sensor ID:", sensor.id, "Type:", sensor.type)
print("Will try again, sleeping")
sleep(10)
# Read the sensors
def ReadSensors(self):
sensors = W1ThermSensor.get_available_sensors()
for sensor in sensors:
print("Sensor ID:", sensor.id, "Type:", sensor.type)
try:
self.t = sensor.get_temperature(Unit.DEGREES_F)
print (f"TEMPERATURE: {self.t:.2f}°F")
except SensorNotReadyError:
print("Error: The sensor is not ready. Please try again later.")
except NoSensorFoundError:
print("Error: No sensor was found. Ensure the sensor is properly connected.")
except Exception as e:
# Catch any other unexpected errors
print(f"Sensor An unexpected error occurred: {e}")
Bench testing while building the system worked fine. I replaced my old failed controller with mine and things were going well until we had two consecutive days of sun and the storage tank temperature increased over 105 deg F.
The sensors began failing at high temperatures around 110 and would recover when they cooled.
The sensors I have are probably cheap implementations, so I'm ordering from a more reputable company, but in the meantime I decided to look into adding error handling to my python code.
What I found was that if I handled the errors, the code would keep running as opposed to locking up and even at higher temps, a successful reading would occur periodically so I could limp along during these events without the controller dying.
It's really a good idea to handle errors anyway, so here is a snippet of code for acquiring all sensors and then reading them.
I'm using a Raspberry Pi 5, so GPIZERO
This code is an example and will not run as is, but should get you on your way to gracefully handling errors on cheap DS18B20 sensors.
#GPIOZERO library
from gpiozero import LED
from time import sleep
# therm sensor library
from w1thermsensor import W1ThermSensor, SensorNotReadyError, NoSensorFoundError
from w1thermsensor.units import Unit
# Sensor instance, both sensors
sensor = W1ThermSensor()
# acuire 2 sensors
sensors = W1ThermSensor.get_available_sensors()
while(len(sensors) < 2):
sensors = W1ThermSensor.get_available_sensors()
print(f"Error: Missing sensors")
for sensor in sensors:
print("Found sensor ID:", sensor.id, "Type:", sensor.type)
print("Will try again, sleeping")
sleep(10)
# Read the sensors
def ReadSensors(self):
sensors = W1ThermSensor.get_available_sensors()
for sensor in sensors:
print("Sensor ID:", sensor.id, "Type:", sensor.type)
try:
self.t = sensor.get_temperature(Unit.DEGREES_F)
print (f"TEMPERATURE: {self.t:.2f}°F")
except SensorNotReadyError:
print("Error: The sensor is not ready. Please try again later.")
except NoSensorFoundError:
print("Error: No sensor was found. Ensure the sensor is properly connected.")
except Exception as e:
# Catch any other unexpected errors
print(f"Sensor An unexpected error occurred: {e}")
Statistics: Posted by mocpi — Fri May 09, 2025 3:20 pm