DS18B20, W1ThermSensor, Raspberry pi Zero W, and Python3.9 - Does not consistently read sensor

617 Views Asked by At

Before I start, I have browsed the "similar questions" section before writing this and could not see one that matched a situation like mine. If one is found, please let me know and I will mark it as "answered" if it is in fact similar. I am a .net full stack developer by profession, i only recently started dabbling in Python and Electrical Engineering as a hobby.

I am creating an Automated Aquaponics Control system, a part of the project reads the temp of the grow bed media and with the input of various other sensors, recalculates the frequency at which the pump cycles to flood the bed. I am using a DS18B20 with Python3.9 and the W1ThermSensor v2.0.0a2 library. Here is the init and first of several functions for the sensor. I have the w1thermsensor as a property of the class instead of inheritance just during the initial testing, since it is easier to manipulate the code this way for me.

#!/usr/bin/env python3

from w1thermsensor import W1ThermSensor, Sensor, Unit
from datetime import datetime
import os
import numpy
import traceback


class DS18B20:

    def __init__(self, min_temp=18, max_temp=26):
        self.sensor = W1ThermSensor()
        self.temp_string = "{dt} : Sensor: {id} :: {temp_c}C - {temp_f}F"
        self.temp_c = 0.00
        self.temp_f = 0.00
        self.is_active = False
        self.is_alert = False
        self.min_temp = min_temp
        self.max_temp = max_temp
        self.values = [0.00, 0.00, 0.00, 0.00, 0.00]
        self.value = 0.00

    def start(self):
        if self.sensor is None:
            return False
        os.system('modprobe w1-gpio')
        os.system('modprobe w1-therm')
        # Set baseline for values Average
        self.is_active = True
        self.monitor()
        self.values = [self.temp_c, self.temp_c, self.temp_c, self.temp_c, self.temp_c]
        self.value = numpy.average(self.values)

This issue that I am running into is that it will have one of 3 issues:

  1. Raises w1thermsensor.errors.NoSensorFoundError
  2. Raises w1thermsensor.errors.SensorNotReadyError
  3. Returns no value in the temp_c property after calling get_temperature()

I looked into this a bit more, If i load up the IDLE in Terminal using the 'sudo python3' command I can enter the following commands and it works no problem:

sudo python3
>>> from w1thermsensor import W1ThermSensor, Sensor
>>> import time
>>> temp_sensor = W1ThermSensor(Sensor.DS18B20)
>>> while True:
...    print(str(round(temp_sensor.get_temperature()))
...    time.sleep(2)

and it works without issue. I also try the 'cat' command

cd /sys/bus/w1/devices
cd 28-3c01d607414b
cat w1_slave
94 01 55 05 7f a5 81 66 5b : crc=5b YES
94 01 55 05 7f a5 81 66 5b t=25250

The stacktrace shows that it is throwing the Errors when it is calling the W1Termsensor() function in "init()". My question is, is it my code or implementation that is causing the issue, or is it something else. My sleep is set to 2 seconds in the hope that I am just catching it in the middle of an update. Any help would be a big help.

Addtional Info:

  • the DS18B20 is wired to a separate 5v power source, the capacitor it to stableize the voltage since there is a 5v relay and a LED array on the same 5v power rail of the power supply.
    5v+ -------------+---------VCC------
                     |                  |
                   4.7 Kohm             |
                     |                  |
    GPIO4 ---------------------DQ       = 1uf polCap
                                        |
                                        |
                                        |
    GND ----------------------GND-------
  • I have double-checked that I have 1-wire enabled.
0

There are 0 best solutions below