luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Trouble reading DS18B20 one wire through 16 relay board on RPi

Wed Oct 09, 2019 3:47 pm

Hi, I am a rookie with programming. Currently working on a project where I am planning to collect data using 16 temperature probes DS18B20 one wire connecting to 16 relay board that connect with Raspberry Pi 3 B+.
I have 12V adapter powering the relay, both 5V connected to 5V on RPi, and both ground on relay connected to GND on RPi.

So far I am only connecting one temperature probe on channel 1 on the relay to the GPIO 20 (RPi), but when I tried to read the temperature on RPi, it will not detect the probe.

On the terminal I did:

Code: Select all

sudo nano /boot/config.txt
then added this at the bottom:

Code: Select all

dtoverlay=w1-gpio


Saved and exit, then:

Code: Select all

sudo reboot
Open the terminal and typed:

Code: Select all

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls
The serial number of the probe is supposed to show up, but it didn't.
It worked find when I connected the probe directly to the RPi and I could get the temperature reading and all.

Image
Image

And I also tried this python code:

Code: Select all

import os
import glob
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
relay=20
GPIO.setup(20,GPIO.OUT)
GPIO.output(20, GPIO.HIGH)
print "Relay should be off"




base_dir = "/sys/bus/w1/devices/"
device_folder = glob.glob(base_dir + "28*")[0]
device_file = device_folder + "/w1_slave"

def read_temp_raw():
    f = open(device_file, "r")
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != "YES":
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find("t=")
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c,


while True:

    result = (read_temp())
    
    if (result <= 24):
        GPIO.output(relay, GPIO.HIGH)
        Print "Relay should be off"
    else:
        GPIO.output(relay, GPIO.LOW)
        Print "Relay should be on"        
    time.sleep(5)
But it didn't work, it only turns on the light on the relay board, and error on line 13 "device_folder = glob.glob(base_dir + "28*")[0]" the error was: list index out of range. (note: I have put the serial number of the probe 28-*****)

Can you all help me check what's wrong? I am suspecting maybe I connected wrong on the probe and relay, or I need to somehow call RPi to read the device on channel 1 of the relay (GPIO 20) but I don't know how.

Any advice would be appreciated!

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Wed Oct 09, 2019 5:19 pm

Hi.

So first off why are you trying to use relays to switch the DS18B20 sensors ?

you can connect a number of sensors to the gpio pin and read each using there unique serial number.

you can also set up several gpio pins to act as a 1-wire interface so you could spread your sensors over 2 or more pins if need be.

Secondly a realy is just an electrically operated switch you all you have done is connect the 3 wires from the sensor to the common, normally open & normally closed connections on the switch like this

Image

which will do absolutely nothing and not connect the sensor to the pi in anyway.

may be this will help with connecting your sensors
http://www.reuk.co.uk/wordpress/raspber ... pberry-pi/

and this will explain how to set-up more than one 1-wire interface on the pi.
https://blog.oddbit.com/post/2018-03-27 ... s-on-the-/

and lastly relays
http://www.circuitstoday.com/working-of-relays
https://www.build-electronic-circuits.c ... lay-works/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Thu Oct 10, 2019 2:49 pm

pcmanbob wrote:
Wed Oct 09, 2019 5:19 pm
Hi.

So first off why are you trying to use relays to switch the DS18B20 sensors ?

you can connect a number of sensors to the gpio pin and read each using there unique serial number.

you can also set up several gpio pins to act as a 1-wire interface so you could spread your sensors over 2 or more pins if need be.

Secondly a realy is just an electrically operated switch you all you have done is connect the 3 wires from the sensor to the common, normally open & normally closed connections on the switch like this

Image

which will do absolutely nothing and not connect the sensor to the pi in anyway.

may be this will help with connecting your sensors
http://www.reuk.co.uk/wordpress/raspber ... pberry-pi/

and this will explain how to set-up more than one 1-wire interface on the pi.
https://blog.oddbit.com/post/2018-03-27 ... s-on-the-/

and lastly relays
http://www.circuitstoday.com/working-of-relays
https://www.build-electronic-circuits.c ... lay-works/
Thank you for your reply, I tried using the relay because I wanted to control which sensor I want to power instead of powering all with the RPi.
And since I need to work with many sensors (possibly 32) I don't know how many sensor can the Pi operate, that's why I chose the 16 relay board for one Pi .

If I use Pi to connect with all the sensors directly, do you know how many one wire sensors can it withstand? Will it damage the Pi in the long term?

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Thu Oct 10, 2019 3:03 pm

If you want to run 32 sensors then you may have to split them over 3 or 4 gpio pins set up as 1-wire interfaces,

you may also need to add a separate 3.3v regulated power rail fed from the 5v on the pi.

or you could power the sensors from 5v but use 3.3v for the pull up ( will provide diagram if you need one ) , you may have to do this if you have 32 sensors anyway as I suspect you might have long cable runs.

Either way you should have no problem running 32 sensors.

You cant switch sensors in and out because there will be up to 90 seconds delay for a sensor be be detected when connecting or disconnecting from the pi gpio pin, and relay contacts with there inherent resistance may affect the reliable operation of the sensors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Thu Oct 10, 2019 9:38 pm

pcmanbob wrote:
Thu Oct 10, 2019 3:03 pm
If you want to run 32 sensors then you may have to split them over 3 or 4 gpio pins set up as 1-wire interfaces,

you may also need to add a separate 3.3v regulated power rail fed from the 5v on the pi.

or you could power the sensors from 5v but use 3.3v for the pull up ( will provide diagram if you need one ) , you may have to do this if you have 32 sensors anyway as I suspect you might have long cable runs.

Either way you should have no problem running 32 sensors.

You cant switch sensors in and out because there will be up to 90 seconds delay for a sensor be be detected when connecting or disconnecting from the pi gpio pin, and relay contacts with there inherent resistance may affect the reliable operation of the sensors.
Thank you for the quick reply again!

- If I split the sensors over different gpio pins, how do I activate it on the pi? Or is the same steps as the one I did? What is I use different gpio pin for each sensor?

- And how do I regulate 5v on the pi? (sorry, I am bad with electrical concept)

- It would be great if you can also show me how to power the sensors from 5v, please?

- The 32 sensors will spread over 50ft of area, so the possibility of having long cable runs is high (is there something that I can add to help that?)

- The relay you mentioned, is it only when connecting or disconnecting? Or you meant relay for sending the data all the time?

I really appreciate your help. ^_^

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Thu Oct 10, 2019 10:37 pm

You probably shouldn't attempt to switch the sensors on and off. They will be perfectly happy running all the time. Besides, if the sensor is off, you will miss any temperature swings that may have happened. The relay board isn't needed.

If you want to run 32 sensors, no problem.
The way I would tackle this is through the use of a driver chip, in this case I'd use the Dallas DS2482-800. This is an eight channel chip, but not limited to just eight sensors. More about that in a minute.
Because the chip communicates with the Pi via I2C, and is addressable, you can connect up to eight of these chips, Each chip has three addressing lines for a possibility of eight chip addresses, providing a total of 64 possible channels.

Now also consider the fact that multiple sensors can be connected to each channel, and you can see that a lot of sensors can be accommodated.

Because you alluded to the probability that the sensors will be mounted some distance from the Pi, I would say that a driver chip would be well advised. If you haven't done so already, you need to plan out the sensor locations and try to identify which sensors can be strung together on a single cable run. The 1-wire protocol does no play nicely with a star topology, so if your design necessitates multiple single sensor runs back to the Pi (driver chip), then each can be connected to an individual channel of the chip. You want to avoid a "star" topology.

I would further recommend that the driver chip be powered from 5 volts, and to use some sort of voltage level translation on the I2C lines back to the Pi, as the Pi won't like more than 3.3 volts. By using a higher voltage for the driver chip, there will be a much better noise margin on the data line to each sensor resulting in improved data integrity. A while ago I implemented multiple temperature sensors in this manner, and things have been very reliable. Run of over 300 feet. I posted some of the installation and connection details in this forum.

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 8:30 am

luisa88 wrote:
Thu Oct 10, 2019 9:38 pm

- If I split the sensors over different gpio pins, how do I activate it on the pi? Or is the same steps as the one I did? What is I use different gpio pin for each sensor?
No you can have a number of sensors on each gpio pin but as you have so many by splitting them up you will help reduce the length of the wiring runs, because you can wires running in different direction from each gpio pin.

To set up a number of gpio pins as the 1-wire interface just read the instructions in the second link I posted.
and this will explain how to set-up more than one 1-wire interface on the pi.
https://blog.oddbit.com/post/2018-03-27 ... s-on-the-/

- It would be great if you can also show me how to power the sensors from 5v, please?
Image

So the only change you need to make is to connect the VDD pin to 5v instead of 3.3v , but you must leave the pull up resistor connected to 3.3v and the DQ pin as shown , the 4.7K resistor should be mounted at the pi end of the circuit.
- The relay you mentioned, is it only when connecting or disconnecting? Or you meant relay for sending the data all the time?
I presume you mean delay not relay , yes the delay is for the connection/disconnection from the pi , its the time it take the pi to notice a sensor has been removed or added to the 1-wire interface.
You can read a connected sensor once a second without any problems.

Edited to correct miss information.
Last edited by pcmanbob on Fri Oct 11, 2019 9:21 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
rpdom
Posts: 15608
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 8:39 am

pcmanbob wrote:
Fri Oct 11, 2019 8:30 am
You can read a connected sensor several time a second without any problems.
Not quite. The DS18B20 takes a bit under a second to return a fresh reading. I'm getting a time of about 0.825 seconds to read it using the cat /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave method.

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 1:54 pm

JohnsUPS wrote:
Thu Oct 10, 2019 10:37 pm
You probably shouldn't attempt to switch the sensors on and off. They will be perfectly happy running all the time. Besides, if the sensor is off, you will miss any temperature swings that may have happened. The relay board isn't needed.

If you want to run 32 sensors, no problem.
The way I would tackle this is through the use of a driver chip, in this case I'd use the Dallas DS2482-800. This is an eight channel chip, but not limited to just eight sensors. More about that in a minute.
Because the chip communicates with the Pi via I2C, and is addressable, you can connect up to eight of these chips, Each chip has three addressing lines for a possibility of eight chip addresses, providing a total of 64 possible channels.

Now also consider the fact that multiple sensors can be connected to each channel, and you can see that a lot of sensors can be accommodated.

Because you alluded to the probability that the sensors will be mounted some distance from the Pi, I would say that a driver chip would be well advised. If you haven't done so already, you need to plan out the sensor locations and try to identify which sensors can be strung together on a single cable run. The 1-wire protocol does no play nicely with a star topology, so if your design necessitates multiple single sensor runs back to the Pi (driver chip), then each can be connected to an individual channel of the chip. You want to avoid a "star" topology.

I would further recommend that the driver chip be powered from 5 volts, and to use some sort of voltage level translation on the I2C lines back to the Pi, as the Pi won't like more than 3.3 volts. By using a higher voltage for the driver chip, there will be a much better noise margin on the data line to each sensor resulting in improved data integrity. A while ago I implemented multiple temperature sensors in this manner, and things have been very reliable. Run of over 300 feet. I posted some of the installation and connection details in this forum.
Thanks for the post, I have six pieces of ULN2803A chips, will that work? Or I have to get the Dallas DS2482-800 ? For either, would you mind to show me how do I connect it with the one wire sensor and the pi?

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 2:02 pm

pcmanbob wrote:
Fri Oct 11, 2019 8:30 am
luisa88 wrote:
Thu Oct 10, 2019 9:38 pm

- If I split the sensors over different gpio pins, how do I activate it on the pi? Or is the same steps as the one I did? What is I use different gpio pin for each sensor?
No you can have a number of sensors on each gpio pin but as you have so many by splitting them up you will help reduce the length of the wiring runs, because you can wires running in different direction from each gpio pin.

To set up a number of gpio pins as the 1-wire interface just read the instructions in the second link I posted.
and this will explain how to set-up more than one 1-wire interface on the pi.
https://blog.oddbit.com/post/2018-03-27 ... s-on-the-/

- It would be great if you can also show me how to power the sensors from 5v, please?
Image

So the only change you need to make is to connect the VDD pin to 5v instead of 3.3v , but you must leave the pull up resistor connected to 3.3v and the DQ pin as shown , the 4.7K resistor should be mounted at the pi end of the circuit.
- The relay you mentioned, is it only when connecting or disconnecting? Or you meant relay for sending the data all the time?
I presume you mean delay not relay , yes the delay is for the connection/disconnection from the pi , its the time it take the pi to notice a sensor has been removed or added to the 1-wire interface.
You can read a connected sensor once a second without any problems.

Edited to correct miss information.
Thank you for the detailed diagram, do I only need 4.7k resistor no matter how many sensors I use?

User avatar
rpdom
Posts: 15608
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 2:33 pm

luisa88 wrote:
Fri Oct 11, 2019 2:02 pm
Thank you for the detailed diagram, do I only need 4.7k resistor no matter how many sensors I use?
One resistor is all you need for each GPIO data pin you use. I wouldn't put more than 10-12 sensors on a single GPIO data pin, but you can use more than one pin.

For 32 sensors I would use 3 data pins and 3 resistors.

All 32 sensors can share the same 5V and GND/0V pins. The resistors can all be connected to the same 3.3V pin.

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 5:01 pm

rpdom wrote:
Fri Oct 11, 2019 2:33 pm
luisa88 wrote:
Fri Oct 11, 2019 2:02 pm
Thank you for the detailed diagram, do I only need 4.7k resistor no matter how many sensors I use?
One resistor is all you need for each GPIO data pin you use. I wouldn't put more than 10-12 sensors on a single GPIO data pin, but you can use more than one pin.

For 32 sensors I would use 3 data pins and 3 resistors.

All 32 sensors can share the same 5V and GND/0V pins. The resistors can all be connected to the same 3.3V pin.
Thank you for the reply!

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Fri Oct 11, 2019 5:01 pm

I will try the above solutions once the sensors are delivered!

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Sat Oct 12, 2019 1:26 am

Thanks for the post, I have six pieces of ULN2803A chips, will that work? Or I have to get the Dallas DS2482-800 ? For either, would you mind to show me how do I connect it with the one wire sensor and the pi?
The ULN2803A driver and the DS2482-800 driver are chips with completely different target applications. The DS2482-800 is specifically designed for interfacing 1-wire devices. This chip contains much more logic than the ULN2803, which is just a darlington transistor array. It cannot be used because it isn't bidirectional.

This thread has presented a couple of different ways of accomplishing your goal - one is to use the GPIO lines directly, and use as many as necessary, the other is to use one or more driver chips. With careful attention paid to how things are wired, each would probably work, however I would prefer using a driver. Longer wire lengths are possible, and the driver chip will help to isolate the sensor wires that come in from the outside world from the Pi in the event a line transient happens on the line. Better to replace a driver than the Pi.

In either case, I would also recommend putting a small capacitor (10ufd or less) in close proximity to each sensor. This will help the sensor when it drives the data line to send data back to the Pi.

This is how I wired five sensors using a single cable back to a DS2482-100 (single channel). I used CAT5e cable with RJ45 receptacles tapped into the line wherever I needed a sensor. Note the capacitors at each temp sensor:
https://i.pinimg.com/originals/7f/37/49 ... 5d2811.jpg

Here is a detail of one RJ45 receptacle being tapped into the run, which was (if I recall correctly) ~369 ft or so:
https://i.pinimg.com/originals/93/0e/df ... 40c5c2.jpg

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Tue Oct 15, 2019 3:45 pm

JohnsUPS wrote:
Sat Oct 12, 2019 1:26 am
Thanks for the post, I have six pieces of ULN2803A chips, will that work? Or I have to get the Dallas DS2482-800 ? For either, would you mind to show me how do I connect it with the one wire sensor and the pi?
The ULN2803A driver and the DS2482-800 driver are chips with completely different target applications. The DS2482-800 is specifically designed for interfacing 1-wire devices. This chip contains much more logic than the ULN2803, which is just a darlington transistor array. It cannot be used because it isn't bidirectional.

This thread has presented a couple of different ways of accomplishing your goal - one is to use the GPIO lines directly, and use as many as necessary, the other is to use one or more driver chips. With careful attention paid to how things are wired, each would probably work, however I would prefer using a driver. Longer wire lengths are possible, and the driver chip will help to isolate the sensor wires that come in from the outside world from the Pi in the event a line transient happens on the line. Better to replace a driver than the Pi.

In either case, I would also recommend putting a small capacitor (10ufd or less) in close proximity to each sensor. This will help the sensor when it drives the data line to send data back to the Pi.

This is how I wired five sensors using a single cable back to a DS2482-100 (single channel). I used CAT5e cable with RJ45 receptacles tapped into the line wherever I needed a sensor. Note the capacitors at each temp sensor:
https://i.pinimg.com/originals/7f/37/49 ... 5d2811.jpg

Here is a detail of one RJ45 receptacle being tapped into the run, which was (if I recall correctly) ~369 ft or so:
https://i.pinimg.com/originals/93/0e/df ... 40c5c2.jpg
Thank you for your explanation, I will try the other method first because I have limited budget for this project. I still have to get the resistor 4.7k, but I am not sure if I should the capacitors like you mentioned.

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Tue Oct 29, 2019 8:21 pm

pcmanbob wrote:
Wed Oct 09, 2019 5:19 pm


may be this will help with connecting your sensors
http://www.reuk.co.uk/wordpress/raspber ... pberry-pi/
Hi pcmanbob,

I tried connecting 5 sensors as described in the website you recommended, at first it worked but then it restart itself and I could no longer see the serial number but instead show random 000* numbers.

I noticed that the connection is a bit different than what you suggested on the post, do you know why or what makes the difference?

Thanks.

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Tue Oct 29, 2019 8:35 pm

The only difference I can see is that the web site is powering the sensors with 3.3V and I suggested using 5V , I only suggested that as you said you had long cable runs.

If the sensors were working and now they are not then there are only so many possibilities ,

1. Sensor/sensors are cheap / rejects and have failed

2. Wiring problems or bad connections

3. Powering sensors from 5V and using the same 5V for the pull up resistor damaging the gpio
( unlikely if you followed the web site )
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Tue Oct 29, 2019 9:24 pm

pcmanbob wrote:
Tue Oct 29, 2019 8:35 pm
The only difference I can see is that the web site is powering the sensors with 3.3V and I suggested using 5V , I only suggested that as you said you had long cable runs.

If the sensors were working and now they are not then there are only so many possibilities ,

1. Sensor/sensors are cheap / rejects and have failed

2. Wiring problems or bad connections

3. Powering sensors from 5V and using the same 5V for the pull up resistor damaging the gpio
( unlikely if you followed the web site )
Thank you for the quick reply, I will need to check the connection one by one and see where it has failed.

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Mon Nov 25, 2019 2:51 pm

I have connected 17 sensors, but it wouldn't power up although I connected it to the 5v, and a resistor connected with 3.3v and data pin as suggested in the diagram above.

Does anyone know why?

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

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Mon Nov 25, 2019 3:49 pm

luisa88 wrote:
Mon Nov 25, 2019 2:51 pm
I have connected 17 sensors, but it wouldn't power up although I connected it to the 5v, and a resistor connected with 3.3v and data pin as suggested in the diagram above.

Does anyone know why?
Not without seeing pictures of your wiring showing both the DS18B20 ends and the pi end , at sufficient quality and size so we can see your wiring clearly.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

luisa88
Posts: 13
Joined: Wed Oct 09, 2019 2:26 pm

Re: Trouble reading DS18B20 one wire through 16 relay board on RPi

Tue Nov 26, 2019 4:51 pm

I got it working! I connected as the website suggested, and separated it into 2 batches. So I have connected to pin 4 and pin17.

Thank you everyone who helped!

Return to “Beginners”