ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Problem with temperature sensor ds18b20

Mon Apr 13, 2020 2:26 pm

Hi,
I have followed the next web to do my project https://pimylifeup.com/raspberry-pi-temperature-sensor/
1º electrical connections:
I connected:
-black wire to ground
-red wire to +5 volts as I had the other 2 pins of 3.3volts busy with other sensors: dht11 and pir (both working well)
-yellow wire to pin gpio 27
-I placed a resistance of 10Kohms between red and yellow wire (a simple pull-up)
but the ds18b20 started to get hot so I disconnected

I made a new connection, this time red wire to +3.3volts with another new ds18b20 and the ds18b20 was ok (not get hot)

2º I activated one.wire support in the raspberry pi

3º I wrote:
-

Code: Select all

sudo modprobe w1-gpio
sudo modprobe w1-therm
then

Code: Select all

cd /sys/bus/w1/devices
ls
but I am obtaining strange and changing values all the time and should not be in that way.
You can see this in the imageImage

So, I can not continue with my proyect as I can not point to any fix value at https://mydevices.com to my ds18b20 sensor to see the temperature

What am I doing bad?

Thanks
Attachments
photo.jpg
photo.jpg (204.8 KiB) Viewed 836 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Problem with temperature sensor ds18b20

Mon Apr 13, 2020 2:48 pm

First off you should not be powering sensors with 5V unless they specifically state when powered by 5v output will be 3.3v or you will damage your pi as the GPIO is not 5v tolerant.

So you only need to enable one wire support using sudo raspi-config and selecting interfaces > 1-wire > enable

you should connect you DS18B20 like this

Image


You can then use this code to return the current temperate for all connected DS18B20 on gpio4

Code: Select all

# Import Libraries

import glob
import time
  
# Finds the correct device file that holds the temperature data
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
# A function that reads the sensors data
def read_temp_raw():
  f = open(device_file, 'r') # Opens the temperature device file
  lines = f.readlines() # Returns the text
  f.close()
  return lines
 
# Convert the value of the sensor into a temperature
def read_temp():
  lines = read_temp_raw() # Read the temperature 'device file'
 
  # While the first line does not contain 'YES', wait for 0.2s
  # and then read the device file again.
  while lines[0].strip()[-3:] != 'YES':
    time.sleep(0.2)
    lines = read_temp_raw()
 
  # Look for the position of the '=' in the second line of the
  # device file.
  equals_pos = lines[1].find('t=')
 
  # If the '=' is found, convert the rest of the line after the
  # '=' into degrees Celsius, then degrees Fahrenheit
  if equals_pos != -1:
    temp_string = lines[1][equals_pos+2:]
    temp_c = float(temp_string) / 1000.0
    temp_f = temp_c * 9.0 / 5.0 + 32.0
    return temp_c, temp_f
 
# Print out the temperature until the program is stopped.
while True:
  print(read_temp())
  time.sleep(3)
  
And you can connect more than one sensor of any type to a 3.3v pin, you don't need a separate pin for each one.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Problem with temperature sensor ds18b20

Mon Apr 13, 2020 2:53 pm

You should use the 3.3V connection for power. It can be shared with the other sensors.

If the DS18B20 gets hot is is usually because you have wired it the wrong way round.

Some of the very cheap sensors have been known to have the wires connected wrongly. It may be worth swapping the red and black wires.
Unreadable squiggle

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Mon Apr 13, 2020 7:20 pm

Hi pcmanbob, hi rpdom
thanks both
according with the datashhet this device should work between +3.0 and 5.5 volts
https://datasheets.maximintegrated.com/ ... S18B20.pdf

1º but I follow your advice and I connected to +3.0volts

2º I have already activated 1-wire support (see photo1)

3º I copied your code and executed

I had an error (see photo2)

Please help
Attachments
photo1.jpg
photo1.jpg (77.46 KiB) Viewed 797 times
photo2.jpg
photo2.jpg (79.56 KiB) Viewed 797 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

trejan
Posts: 2118
Joined: Tue Jul 02, 2019 2:28 pm

Re: Problem with temperature sensor ds18b20

Mon Apr 13, 2020 8:59 pm

ancris00 wrote:
Mon Apr 13, 2020 7:20 pm
according with the datashhet this device should work between +3.0 and 5.5 volts
https://datasheets.maximintegrated.com/ ... S18B20.pdf
The DS18B20 is okay with 5V. The Pi GPIOs will get damaged by 5V though.

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

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 11:48 am

So you can power the DS18B20 with 5V but you would still need to use a 3.3v pull-up on the data pin.

like this

Image

Or you would damage / kill your pi.

The error suggests no sensor is connected.
So with your DS18B20 connected , if you run these commands what do you get as a result ?

Code: Select all

cd /sys/bus/w1/devices
ls
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
bensimmo
Posts: 4621
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 12:50 pm

just for the info, you don't need to do the mod-probe stuff. that's enbled when you enable 1-wire
which Pi are you using?

- you are using gpio27 (physical pin13)
if so type
sudo dtoverlay w1-gpio gpiopin=27 pullup=0
does it work now?


edit and check config.txt e.g.
sudo nano /boot/config.txt

and change the 1-wire line to read
dtoverlay=w1-gpio,gpiopin=27
if not already and reboot.

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 4:38 pm

hi pcmanbob, hi bensimmo, thanks both

pcmanbob, thanks now I grasp. The very first time I did the pullup with 5Volts. Lucky me I have checked today that pin(27) with other sensor and it is not damaged.
I executed your commands and I have obtained nothing as you can see in photo3

I desconnected the sensor and I obtained the extrange numbers as at the beguining of this post (see photo4). I do not now if to have other sensors connected, as I have, it is influencing this result..

bensimmo, I did what you said and nothing. A curiosity, just to mention that part of the line was already written (see photo5)

Shall I conclude that both sensors have been wrongly wired (china doggy manufacturer), and by being connected for me, as suppose that they must have been wired, they have been damaged?
Attachments
photo5.jpg
photo5.jpg (50.69 KiB) Viewed 716 times
photo4.jpg
photo4.jpg (88.11 KiB) Viewed 716 times
photo3.jpg
photo3.jpg (90.45 KiB) Viewed 716 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 5:00 pm

Hot sensor means the wiring is the wrong way round. Fortunately most of the time the DS18B20 will survive and work fine once cooled down and connected correctly.
Unreadable squiggle

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

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 5:31 pm

I assume you are using water proof sensors that have 3 wires , the problem is if they don't work using the wiring colours , then it becomes a problem as you have no way to know which wires are connected to which pin.

It may well be that it not the red/black that have been switched any 2 or all 3 may have been switched.

I would suggest you start by getting some of the standard TO-92 packaged devices and test your set-up with them.

I have in the past removed the actual sensor from a waterproof sensor and reused the casing/wiring with a TO-92 packaged device to end up with a working waterproof sensor.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
bensimmo
Posts: 4621
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem with temperature sensor ds18b20

Tue Apr 14, 2020 6:18 pm

If you only have the default line with no pin assignment, then it is using GPIO4 (physical pin 7). it will not see DS18's on any other pin.
Can you take a picture of you setup/


EDIT just a thought, is this a typo in you first post ?
" -yellow wire to pin gpio 27 "
Is that a non-UK keyboard ? so # is on number 2 and you mean #7
....



Do what I wrote and assign it to GPIO27, move the data wire to physical pin 13 ( see https://pinout.xyz/pinout/pin13_gpio27 )
then reboot and see what it says.
If this is a Pi4, then is may have some trouble on GPIO4 for unknown reasons, which is a shame as it is the default.

fingers crossed that is it.
(ref. viewtopic.php?f=112&t=269252 )

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Sun Apr 19, 2020 5:10 pm

Hi bensimmo, pcmanbob and rpdom, thanks for your quick reply

just a summary: gpio4 (connected sensor DHT11), gpio 17 (connected sensor PIR) and in gpio 27 (connected sensor DS18b20)

rpdom, probably you are right (probably the sensor is wrongly wired from the manufacturer). I test the sensor changing all the possibilities with the cables...but nothing...no response...I think is too late and my two ds18b20 are damaged now

pcmanbob, I will do what you said. I will drill one ds18b20 and I will use the inox-casing/housing. I will buy a TO-92 packaged and introduce it insided the inox-casing. I suppose that have used thermal paste. What have you used?

bensimmo, I have Spanish-keyboard. I used GPIO 27(physical pin 13)
What I have learn is that, please tell me if something is wrong:
-by defect only GPIO4 support 1-wire communication (is 1-wire a exclusive Raspberry protocol of comunication?)
-if you write:

Code: Select all

sudo dtoverlay w1-gpio gpiopin=27 pullup=0 
:
+ you enable 1-wire communication also on GPIO27 ?
+ So, now both pins support 1-wire communication
+ if you reboot the Pi you lost this configuration. For this reason, if you want to keep it you have to write/modifiy the config file

At the beguining probably I was obtaining extrange values because I have connected sensor DHT11 on GPIO4.

The last thing that I am thinking is happening is that because I am using a 10k resistance (I only have this, in Spain there is a heavy lockdown due to coronavirus) instead of 4k7 resistance (the recommended one) this is making the proyect not working
Please could any of you move this to an Pi-engineer?
Attachments
6.jpg
6.jpg (27.21 KiB) Viewed 618 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Problem with temperature sensor ds18b20

Sun Apr 19, 2020 6:06 pm

ancris00 wrote:
Sun Apr 19, 2020 5:10 pm

pcmanbob, I will do what you said. I will drill one ds18b20 and I will use the inox-casing/housing. I will buy a TO-92 packaged and introduce it insided the inox-casing. I suppose that have used thermal paste. What have you used?

The last thing that I am thinking is happening is that because I am using a 10k resistance (I only have this, in Spain there is a heavy lockdown due to coronavirus) instead of 4k7 resistance (the recommended one) this is making the proyect not working
I would start by using the DS18B20 TO-92 packaged device on your breadboard and test it like that, once you have it working then think about using it as a replacement in your waterproof sensor.

I used heat sink compound namely a high thermal conductivity zinc oxide filled silicone compound.

but any PC CPU thermal compound should be ok just don't use any with metal in and insulated all soldered joints with heat shrink sleeving.

As for your resistor problem putting 2 x 10K resistors in parallel will give you a 5K resistor which is closer to the 4.7K you require.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
bensimmo
Posts: 4621
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem with temperature sensor ds18b20

Mon Apr 20, 2020 9:13 am

something is wrong:
-by defect only GPIO4 support 1-wire communication (is 1-wire a exclusive Raspberry protocol of comunication?)
-if you write:
...... etc

only if you have
dtoverlay=w1-gpio
in config.txt is 1-wire enabled on gpio4
if it is not there, there is no 1-wire on gpio4
that line is the same as
dtoverlay=w1-gpio,gpiopin=4
Gpio 4 is just the *default* if no other pin is assigned.

if you have *only* the line
dtoverlay=w1-gpio,gpiopin=27
it will only have 1-wire on gpio27, it will not be on gpio4

if you have a DHT on gpio4, *do not enable* 1-wire on gpio4, DHTs do not use 1-wire.


So with your setup, the only line in config.txt should be
dtoverlay=w1-gpio,gpiopin=27

no other reference to w1-gpio !


Also given that is black, red and yellow wires. it will probably be wired correctly.

Hans.KK
Posts: 7
Joined: Thu May 31, 2018 3:36 pm

Re: Problem with temperature sensor ds18b20

Mon Apr 20, 2020 4:38 pm

ancris00 wrote:
Sun Apr 19, 2020 5:10 pm
just a summary: gpio4 (connected sensor DHT11), gpio 17 (connected sensor PIR) and in gpio 27 (connected sensor DS18b20)
I would suggest that you change your setup to:
gpio4 (connected sensor DS18b20),
gpio 17 (connected sensor PIR)
gpio 27 (connected sensor DHT11)
This setup will be more easy for you, you do not need to change anything in config.txt.
The last thing that I am thinking is happening is that because I am using a 10k resistance instead of 4k7 resistance
10 k is not a problem, but you could place two 10k in parallel, that will give you 5k.

I had had same problem with sensor DS18b20 as you show with screenshots, some strange sensors showed up with strange names like 00-a00000000000 in directory /sys/bus/w1/devices, this appears to be a failure during powerup of the sensors, they go into a unknown mode, the only fix for me so far is to remove the power (the +3.3Volt wire) to the sensor (and the pulleup resistor) after power on, wait a few seconds and then reconnect it, after this no problem. I had try just to reboot, but if there is a problem with the contains in directory /sys/bus/w1/devices then it is persistent until a power off/on of the sensors.

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

Re: Problem with temperature sensor ds18b20

Mon Apr 20, 2020 5:32 pm

Hans.KK wrote:
Mon Apr 20, 2020 4:38 pm
10 k is not a problem, but you could place two 10k in parallel, that will give you 5k.
I have done this when I didn't have a 4K7 resistor to hand. Actually, the datasheet for the DS18B20 says that the ideal resistance is 5K, but 4K7 is the closest standard value and completely acceptable.
Unreadable squiggle

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Wed Apr 22, 2020 2:52 pm

Thanks rpdom, Hans.KK, bensimmo, pcmanbob
I have good news...it is working :D :D

What I did...
1º Turn off the Raspberry Pi
2º I removed the DHT11 sensor for gpio 4
3º Turn on the Raspberry Pi
4º Execute ls /sys/bus/w1/devices and I obtain the value that identifie my sensor (see photo) YES :lol:

Later, just for cheching:
1º Modified the file config.txt and I wrote dtoverlay=w1-gpio,gpiopin=27
2º Turn off the Raspberry Pi
3º I moved the data wire from gpio4 to gpio27
4º Turn on the Raspberry Pi
5º Execute ls /sys/bus/w1/devices and I obtain the value that identifie my sensor. Again success YES

Where was the problem? Probably is what Hans.KK said...In the past I connect the sensor without turn off the Raspberry Pi and the sensor enter into a some kind of error. Sometime we forguet that inside the sensor DS18b20 are a complety computer: conversor a/d, comunications, sensor...

But I still have a doub:
Is the same a) modify the file config.txt and I wrote dtoverlay=w1-gpio,gpiopin=27 than b) write: sudo dtoverlay w1-gpio gpiopin=27 pullup=0 ??????

*Just an aclaration I use only one resistor 10K as I only have one
Attachments
7.jpg
7.jpg (51.96 KiB) Viewed 530 times
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Mon May 18, 2020 4:28 pm

please,
any help with the last question?
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Problem with temperature sensor ds18b20

Mon May 18, 2020 4:46 pm

The problem was that you had a DHT11 device connected to the 1-wire interface pin at the same time as the DS18B20,

The DHT11 is not a true 1-wire device unlike the DS18B20 so it would cause corruption on the interface causing the not to recognise devices.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Problem with temperature sensor ds18b20

Mon May 18, 2020 4:47 pm

Yes, modifying the config.txt file to add a dtoverlay option is (almost) the same as the sudo dtoverlay command.

Modifying the file is preferable.
Unreadable squiggle

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Mon May 18, 2020 4:52 pm

Thank you rpdom and pcmanbob,
and the last question 1-wire comunication could be in more than one pin at the same time?
Let's say 1-wire comunication on pin4 and on pin 27 at the same time?
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

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

Re: Problem with temperature sensor ds18b20

Mon May 18, 2020 5:41 pm

Yes, you can have more than one 1-wire bus on the Pi. You just add extra lines to config.txt

But you may not need that. Each one-wire bus connection is capable of supporting several devices.

As pcmanbob said, the DHT11 isn't a 1-wire device. It uses its own communication format.
Unreadable squiggle

ancris00
Posts: 52
Joined: Sun Jan 19, 2020 8:56 pm
Location: Miranda de Ebro, Spain

Re: Problem with temperature sensor ds18b20

Tue May 19, 2020 10:11 am

thank you rpdom
:lol: :lol: Pi 4 Model B :lol: :lol:
Raspbian Buster with desktop 4.19

Hans.KK
Posts: 7
Joined: Thu May 31, 2018 3:36 pm

Re: Problem with temperature sensor ds18b20

Tue May 19, 2020 8:39 pm

ancris00 wrote:
Mon May 18, 2020 4:52 pm
and the last question 1-wire comunication could be in more than one pin at the same time?
Let's say 1-wire comunication on pin4 and on pin 27 at the same time?
Yes to both, but it is not that easy to get it to work, I manage in this way:
In config.txt I have "#dtoverlay=w1-gpio" and in my crontab I have "@reboot sudo dtoverlay w1-gpio gpiopin=27"
That gives me two w1_bus_masters in "/sys/bus/w1/devices/" and all 1-wire devices are listed the same place.
The two 1-wire pins are then: 4 and 27.

Do you have your DHT11 activated as an overlay in config.txt, because if so then the default pin will be 4, same as the default pin for 1-wire, so they can't coexist at the same time without one of then (or both) with the pin set to an alternative than 4.

if config.txt looks like:
dtoverlay=dht11
dtoverlay=w1-gpio

It will not work, and most likely it will be dht11 that will not work, 1-wire may work.

dtoverlay=dht11
dtoverlay=w1-gpio,gpiopin=27

Should work ok for both dht11 and 1-wire.

User avatar
bensimmo
Posts: 4621
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Problem with temperature sensor ds18b20

Wed May 20, 2020 6:52 am

for more than one pin you don't need to use the Cron tab to schedule to schedule (you can of course :-))
just having
dtoverlay=w1-gpio,gpiopin=17
dtoverlay=w1-gpio,gpiopin=22
dtoverlay=w1-gpio,gpiopin=28

in config.txt

so enable 1-wire on the on the three pins.

Return to “Troubleshooting”