JohnsUPS
Posts: 129
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Multiple DS18B20 temperature sensors

Thu Mar 21, 2019 12:06 am

I am not that much of a fan of using a GPIO pin as the data pin when talking to DS18B20 sensors. For better performance I'd advocate using a driver chip powered from 5 volts.

For your application, I'd recommend using the eight channel driver chip. This way you don't have to string one long wire around the house, but instead run up to eight circuits wherever you'd like without creating a star topology - Dallas DS2482-800.

If one long wire isn't a problem, then a single driver chip would do the trick.

For overcoming distance issues, you may want to look at:
viewtopic.php?t=36163
and
viewtopic.php?f=37&t=231164

heitjer
Posts: 7
Joined: Sat Jul 27, 2019 5:42 pm

Re: Multiple DS18B20 temperature sensors

Sat Jul 27, 2019 6:08 pm

I am new to the PI experience and I am currently building the weather station.
https://projects.raspberrypi.org/en/pro ... -station/3

So far so good. All works well. I reached the stage where they recommend to use additional sensors. I need to add multiple temperature sensors beyond the two described in the weather station setup.

I have now one temperature on the BME280 and one on the DS18B20. This will measure temp_air and temp_topsoil. I also want to measure temp_frost below the frost line, temp_water and temp_deepwater for my large pond.

The estimated cable length for the sensors:
BME280: temp_air = 5 inch max (~ 0.13m)
DS18B20: temp_topsoil = 6 ft (~ 1.8m)
DS18B20: temp_frost = 9 ft (~2.7m)
DS18B20: temp_water = 12 ft (~ 3.7m)
DS18B20: temp_deepwater = 50 ft (~ 15.2m)

I have read up on the linear, stubbed and star configurations. The most logical to me would be the star config and terminate all sensors back at the weather station. But the star is not the most reliable. I can not imagine right now how I would pull off a different configuration the star other then running 4 wires to each sensor and loop a wire through?

I am also concerned about the run lengths and the 3.3V.

Any help and suggestions out there?

JohnsUPS
Posts: 129
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Multiple DS18B20 temperature sensors

Fri Aug 02, 2019 11:45 am

heitjer wrote:
Sat Jul 27, 2019 6:08 pm

The estimated cable length for the sensors:
BME280: temp_air = 5 inch max (~ 0.13m)
DS18B20: temp_topsoil = 6 ft (~ 1.8m)
DS18B20: temp_frost = 9 ft (~2.7m)
DS18B20: temp_water = 12 ft (~ 3.7m)
DS18B20: temp_deepwater = 50 ft (~ 15.2m)

I have read up on the linear, stubbed and star configurations. The most logical to me would be the star config and terminate all sensors back at the weather station. But the star is not the most reliable. I can not imagine right now how I would pull off a different configuration the star other then running 4 wires to each sensor and loop a wire through?

I am also concerned about the run lengths and the 3.3V.

Any help and suggestions out there?
Heitjer,
Because it is apparently more convenient for you to run an individual cable for each sensor back to a central point instead of looping one long cable around to each sensor, I would advocate the use of the DS2482-800 driver chip. You will avoid the star topology because each line will come back to its own channel on the driver. The chip talks to the Pi via I2C, so if the chip is powered by 5V, you will need voltage level translation. Otherwise, if powered from 3V3 (data sheet states Vcc can be as low as 2V9), no voltage level translation would be necessary. Because your runs are fairly short, powering the driver chip from 3V3 should work.
I am not a fan of running the 1Wire signalling directly off of a GPIO pin unless it is for testing or proof of concept. This is mainly because doing it this way provides no buffer between the outside world and the Pi when it comes to the final installation. However, if the data line for each sensor were connected to a different GPIO pin with pullup resistor, you would also eliminate the star topology reflection problem, effectively creating a seperate channel for each sensor.

heitjer
Posts: 7
Joined: Sat Jul 27, 2019 5:42 pm

Re: Multiple DS18B20 temperature sensors

Wed Oct 23, 2019 3:27 am

Guys - a problem!

I had everything working on my desk with breadboards (Wind vane, direction, rain meter, BME280 and three DS18B20 sensors). Then I moved all to my workshop to start building everything. I must have shortened the BME280 and had to order a new one. That works again but now my three temp sensors are acting up. Here is the details:

In the boot/config.txt I have the following:

dtoverlay=w1-gpio,gpiopin=4
dtoverlay=w1-gpio,gpiopin=17
dtoverlay=w1-gpio,gpiopin=27

In the /sys/bus/w1/devices folder I see the following list of folders:

00-800000000000 (does not contain any other files)
28-020a92454a01
28-020d9177b3ec
w1_bus_master1
w1_bus_master2
w1_bus_master3

cat /sys/bus/w1/devices/28-*/w1_slave delivers now mostly 2 devices, sometimes only one and just now when i tested it all three.
Now it shows the third folder correctly instead of the 00-80....
28-020392458844
28-020a92454a01
28-020d9177b3ec
w1_bus_master1
w1_bus_master2
w1_bus_master3

So while typing my post it fixed itself - go figure.... :)

I assume this has to do with the fact that the temperature sensors are on a long wire. I have them now on a 5 wire direct bury cable.
Red carries the positive, black negative and yellow, white and orange carry a signal back. Each signal has its own resistor and gpio. The cable is about 50 ft long and has one sensor at the end (red-black-yellow), 20 ft up it has the second sensor (red-black-white) and another 20 ft up the third is tied in (red-black-orange). I assume that it has to do with the voltage drop over that length that sometimes not all of them are recognized during boot up.

Now my question -
1) would you also suspect the length and resulting voltage drop (cable is 18 gauge bare copper, sprinkler wire)
2) is there a way to give the raspberry time to recognize this at startup? It seems hit & miss.
3) So far I think this only fixed itself during a fresh boot up. Is a reboot required or will the sensor occur later as it is detected.

Unfortunately my python script stops when less then three sensors are detected.
Last edited by heitjer on Wed Oct 23, 2019 5:55 pm, edited 1 time in total.

jcyr
Posts: 504
Joined: Sun Apr 23, 2017 1:31 pm
Location: Atlanta

Re: Multiple DS18B20 temperature sensors

Wed Oct 23, 2019 4:20 am

A lot can go wrong with 1-wire, specially with long runs. I don't think voltage drop on 18 gauge copper would be significant when using low power devices. Lot's of good information here: https://www.maximintegrated.com/en/desi ... 1/148.html
It's um...uh...well it's kinda like...and it's got a bit of...

pcmanbob
Posts: 7306
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Multiple DS18B20 temperature sensors

Wed Oct 23, 2019 8:22 am

Are you DS18B20 temp sensors waterproof ones by any chance ?

There are a lot of cheap/factory seconds being sold on line and the symptoms you are describing are common for these sensors, you may find they work for a while then stop responding until your power off the pi and restart the system or they can fail all together.

If you got your sensors cheap on line I would recommend replacing them with some new ones from a better supplier such as one of the big electronics suppliers.

There are several threads on here about this type of sensor , talking about bad sensors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

heitjer
Posts: 7
Joined: Sat Jul 27, 2019 5:42 pm

Re: Multiple DS18B20 temperature sensors

Wed Oct 23, 2019 11:40 am

I guess I have to look into the 'cheap' sensors. I bought of Amamzon (HiLetgo 5pcs DS18B20 Temperature Sensor Temperature Probe Stainless Steel Package Waterproof).
5 pieces for $10.99.
Unfortunately the cost is in the "deployment system" as I have them glued in watertight into PVC pipe. Can someone recommend me a "good source" for my next purchase?

@jcyr - I read this article previously and decided to go to a three separate string temperature deployment instead of using the multiple sensor on one wire approach.

pcmanbob
Posts: 7306
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Multiple DS18B20 temperature sensors

Wed Oct 23, 2019 12:31 pm

Sorry cant recommend a good source for the waterproof DS18B20, in my case I just took the failed ones apart and replaced the sensor chip inside with some standard TO-92 DS18B20 sensors that I sourced from a trusted supplier in the UK, and then sealed them back up with hot glue and heat shrink tubing in the same way they were originally sealed.

This also allowed me to soak test the DS18B20's before sealing them in the waterproof heads.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

heitjer
Posts: 7
Joined: Sat Jul 27, 2019 5:42 pm

Re: Multiple DS18B20 temperature sensors

Thu Oct 24, 2019 1:07 am

Can someone look through my code and see why my script stops. I think it is related to the above - possible dropping of a sensor for a period of time. Can someone suggest how I can code it that it ignores the error for a time, possible repeats the old measure (or an error number like 99.99) and sends a flag. All I am interested in is to have this weather station script running and dont care too much about skipping one measurement.

Script stops with this error message:

Code: Select all

Traceback (most recent call last):
  File "/mnt/weather/WebSensor/wind.py", line 103, in <module>
    temp_deepwater = (x.tempC(2))
  File "/mnt/weather/WebSensor/multiple_temp.py", line 20, in tempC
    lines = self._read_temp(index)
  File "/mnt/weather/WebSensor/multiple_temp.py", line 14, in _read_temp
    f = open(self._device_file[index],'r')
IndexError: list index out of range
Here is the code:

Main Program - wind.py

Code: Select all

from gpiozero import Button
import time
import datetime
import math
import bme280_sensor
import wind_direction_byo
import statistics
from multiple_temp import DS18B20

import sqlite3
import sys
conn=sqlite3.connect('fishpond.db')
curs=conn.cursor()

store_speeds=[]
store_directions=[]


rain_count=0
wind_count=0
gust = 0
radius_cm=5.0       #radius from center to cup
wind_interval=5     #how often to record
interval=30         #time inbetween write out to database 
adjustment=1.18     #annometer inefficiency and friction
Bucket_Size=0.2794  #mm rainfall per bucket tip


def add_data (temp_air, temp_ground, temp_water, temp_deepwater, humidity, wind_average, wind_speed, wind_gust, pressure, rainfall):
    curs.execute("INSERT INTO FP_data values(datetime('now'), (?), (?),(?), (?), (?), (?), (?), (?), (?), (?))",(temp_air, temp_ground, temp_water, temp_deepwater, humidity, wind_average, wind_speed, wind_gust, pressure, rainfall))
    conn.commit()

    

# WIND SPEEDS
def spin():
    global wind_count
    wind_count= wind_count + 1

def calculate_speed(time_sec):
    global wind_count
    global gust
    circumference_cm = (2*math.pi) * radius_cm
    rotations = wind_count / 2
    dist_km = (circumference_cm * rotations) / 10000
    final_speed = dist_km*3600*adjustment/time_sec
    return final_speed

# RAINFALL MEASUREMENT
def bucket_tipped():
    global rain_count
    rain_count = rain_count + 1

def reset_rainfall():
    global rain_count
    rain_count = 0

def reset_wind():
    global wind_count
    wind_count=0

def reset_gust():
    global gust
    gust = 0



wind_speed_sensor=Button(5)
wind_speed_sensor.when_activated = spin


#db = database.weather_database()

while True:
    start_time=time.time()
    while time.time() - start_time <= interval:
        wind_start_time = time.time()
        reset_wind()
        while time.time() - wind_start_time <= wind_interval:
            store_directions.append(wind_direction_byo.get_value())
            
        final_speed = calculate_speed(wind_interval)
        store_speeds.append(final_speed)

    wind_average = wind_direction_byo.get_average(store_directions)
    wind_gust = max(store_speeds)
    wind_speed = statistics.mean(store_speeds)

    rainfall = rain_count * Bucket_Size
    reset_rainfall()

    store_speeds=[]
    store_directions=[]
    
    humidity, pressure, temp_air = bme280_sensor.read_all()

    x = DS18B20()
    temp_ground = (x.tempC(0))
    temp_water = (x.tempC(1))
    temp_deepwater = (x.tempC(2))


    temp_air=round(temp_air,2)
    temp_ground=round(temp_ground,2)
    temp_water=round(temp_water,2)
    temp_deepwater=round(temp_deepwater,2)
    humidity=round(humidity,2)
    wind_average=round(wind_average,2)
    pressure = round(pressure,2)
    wind_speed=round(wind_speed,2)
    wind_gust=round(wind_gust,2)
    rainfall=round(rainfall,2)

    
    add_data (temp_air, temp_ground, temp_water, temp_deepwater, humidity, wind_average, wind_speed, wind_gust, pressure, rainfall)

 
    datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

       
    print(' ')
    print('Temperature: ',temp_ground, 'degC')
    print('Water Temperature: ',temp_water, 'degC')
    print('Deep Water Temperature: ',temp_deepwater, 'degC')
    print('BME Temperature: ',temp_air, 'degC')
    print('Humidity: ',humidity, '%')
    print('Pressure: ',pressure, 'kPA')
    print('Direction: ',wind_average, 'deg')
    print('Rotation Count: ',wind_count, 'rotation')
    print('Wind Speed: ',wind_speed, 'km/hr')
    print('Wind Gust: ',wind_gust, 'km/hr')
    print('Rainfall: ',rainfall, 'mm')
    print(' ')
    print('----------------------------------------------')

    
From there the program calls multiple_temp.py

Code: Select all

import os, glob, time

class DS18B20:
    def __init__(self):
        base_dir = '/sys/bus/w1/devices/'
        device_folder = glob.glob(base_dir + '28*')
        self._num_devices = len(device_folder)
        self._device_file = list()
        i=0
        while i < self._num_devices:
            self._device_file.append(device_folder[i] + '/w1_slave')
            i +=1
    def _read_temp(self,index):
        f = open(self._device_file[index],'r')
        lines = f.readlines()
        f.close()
        return lines

    def tempC(self,index=0):
        lines = self._read_temp(index)
        retries = 10
        while (lines[0].strip()[-3:] !='YES') and (retries >0):
            time.sleep(0.1)
            lines=self._read_temp(index)
            retries -=1

        if retries ==0:
            return 998

        equals_pos = lines[1].find('t=')
        if equals_pos != -1:
            temp = lines[1][equals_pos +2:]
            return float(temp)/1000
        else:
            # error
            return 999

    def device_count(self):
        return self._num_devices
        
Last edited by heitjer on Thu Oct 24, 2019 11:31 am, edited 1 time in total.

JohnsUPS
Posts: 129
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Multiple DS18B20 temperature sensors

Thu Oct 24, 2019 1:24 am

I assume this has to do with the fact that the temperature sensors are on a long wire. I have them now on a 5 wire direct bury cable.
Red carries the positive, black negative and yellow, white and orange carry a signal back. Each signal has its own resistor and gpio. The cable is about 50 ft long and has one sensor at the end (red-black-yellow), 20 ft up it has the second sensor (red-black-white) and another 20 ft up the third is tied in (red-black-orange). I assume that it has to do with the voltage drop over that length that sometimes not all of them are recognized during boot up.

Now my question -
1) would you also suspect the length and resulting voltage drop (cable is 18 gauge bare copper, sprinkler wire)
2) is there a way to give the raspberry time to recognize this at startup? It seems hit & miss.
3) So far I think this only fixed itself during a fresh boot up. Is a reboot required or will the sensor occur later as it is detected.

Unfortunately my python script stops when less then three sensors are detected.
There could be several problems - the voltage that the sensors are running at (3V3), the value of the pull-up resistors and possibly the quality of the sensor at the end. As was pointed out by pcmanbob, there are a lot of fakes out there, and unfortunately they are all too common. Source from a reliable vendor such as Digi-Key, Jameco, Mouser or another big component house. Avoid Ebay and Banggood for these parts. Pay an extra buck per sensor and avoid the headaches. I did, and was glad to have reliable parts.

I see a couple of things that you can do to make your system more reliable/robust. The first thing that I would do is to run the sensors off of 5V, but still keep the pull-up resistors terminated to 3V3. Because the sensor output is an open drain configuration, the data line with a pull-up resistor to the Pi 3V3 connection will not present to the Pi a voltage higher than 3V3 although the sensors themselves are running from 5V. The 5V supplied to ground and VCC will allow for more of a noise margin on the connecting cable, lower current and a resulting lower voltage drop on the cable as a result. See this thread: https://www.raspberrypi.org/forums/view ... 37&t=30359
Add a small capacitor at each sensor. I used 10ufd tantalum between Vcc and ground. Keep in mind that at some point in time, the sensor has to drive the data line all the way back to the Pi, just like the Pi has to drive a signal to the sensor. The capacitor will provide that extra bit of power when needed to the sensor so that it can more easily drive the data line.
Because all of your sensors are along a single length of cable, you could also combine the data lines to ,say, the yellow wire. You're already using red and black for power, so one line for data. The remaining two wires (white and orange) can be doubled up for a better power connection (for example, black and white for ground, red and orange for VCC).

Questions:
1) No, I would not suspect the resistance of the wire to be a contributing factor at the lengths and gauge you are talking about. They type of wire you are using should work fine.
2) Once powered up, the sensors will come up faster than the Pi (probably within milliseconds). This should not be a problem.

A while back I installed five DS18B20 sensors along a 369 foot length of Cat6 cable. I just tapped the cable wherever I wanted a sensor (they are about 20-30 feet apart at the end, like yours). All of the sensors share a common data line and have capacitors at each sensor. They are running from 5V, but unlike your setup, the data line comes back to a driver chip. This has been running well for a while now.
https://www.raspberrypi.org/forums/view ... 44&t=36163

heitjer
Posts: 7
Joined: Sat Jul 27, 2019 5:42 pm

Re: Multiple DS18B20 temperature sensors

Thu Oct 24, 2019 1:44 am

@JohnsUPS - thanks for the fast reply. As a newb I am soaking in all the advice I can get and you are very detailed which I appreciate.

The reason why I did not run this in a single wire config was that people all over the internet were talking about the problems they have with it of sensors dropping out. I figured I go with a run each to avoid this. Little did I know...

I will certainly try the 5V supply. The way I understand your advice is as follows:
Image
So in my case this means that I only change red from 3V to 5V so all sensors get 5V supply, ground stays as is.
Yellow, white and orange will terminate as is (!!!) to the 3V pin with their current resistor.
This is a quick change and can be done without a major headache.

If the problem persists then I go to the next step and get new sensors and mess with my deployment PVC pipe (cut and redo). When I mess with new sensors I will also do the capacitors. At that point should I stay with the three returns or go single wire setup?

I do not understand your last comment from your setup..."the data line comes back to a driver chip". I understand the single wire setup but what is your driver chip? Do you mean the internal PI I2C?

JohnsUPS
Posts: 129
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Multiple DS18B20 temperature sensors

Fri Oct 25, 2019 1:01 pm

I do not understand your last comment from your setup..."the data line comes back to a driver chip". I understand the single wire setup but what is your driver chip? Do you mean the internal PI I2C?
In the temperature measurement setup that I have deployed, the data line does not come back to a GPIO pin on the Pi. Instead, it terminates to a connection on a driver chip (https://datasheets.maximintegrated.com/ ... 82-100.pdf) instead. This chip is powered from 5 volts, and therefore the I2C data signal output of the chip is 5 volts, necessitating the use of logic level converters (down to 3.3 volts) in order to safely connect it to the Pi. I did it this way for two main reasons - one, the driver chip is specifically meant to drive the data line for a 1 wire confiuration, and two, the driver chip will help protect the Pi (as no lines connect directly to the Pi) in the event a spike/transient comes down the line. Less of a strain on the GPIO pins as well.

JohnsUPS
Posts: 129
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Multiple DS18B20 temperature sensors

Fri Oct 25, 2019 1:37 pm

I assume this has to do with the fact that the temperature sensors are on a long wire. I have them now on a 5 wire direct bury cable.
Red carries the positive, black negative and yellow, white and orange carry a signal back. Each signal has its own resistor and gpio. The cable is about 50 ft long and has one sensor at the end (red-black-yellow), 20 ft up it has the second sensor (red-black-white) and another 20 ft up the third is tied in (red-black-orange). I assume that it has to do with the voltage drop over that length that sometimes not all of them are recognized during boot up.
This description does not seem consistent with the schematic diagram shown a couple of posts up, unless you have the yellow white and orange wires all connected together at the Pi with a single pull up resistor?? If so, you have created a star topology.

The above paragraph reads like you have put the yellow on one GPIO with a pull up resistor, the white on a different GPIO with its own pull up resistor, and the orange on yet another GPIO (not shared by either one of the other two) with yet a third pull up resistor, but the schematic shows a single line for the data connections to a single GPIO with one resistor.
Wiring it so that three GPIOs are used for three sensors won't be a star topology.

Otherwise, because you already have your sensors strung out along the wire in a line, the data terminal for each sensor can be connected to the same wire (with one pull up) and that would be consistent with the above schematic.
You may also want to put a small capacitor (about 10ufd or so) at each sensor.

Return to “Advanced users”