DS18B20 Rpi SE Q&A Old Answer

DS18B20 Temperature Sensor – Rpi 3/4 Driver, Wiring, Detection, and Python Programming

Ask QuestionAsked 1 year, 5 months agoActive 3 months agoViewed 1k times41

I am new in raspberry pi. In my research project, I would like to monitor ground temperature at 60 points. My team decided to set a monitoring system using raspberry pi.

As a preliminary test, I connected 4 DS18B20 sensors to my pi; it can find sensors and read temperature correctly. Unfortunately, when I connected an additional DS18B20 to the pi (so total 5 DS18B20 were linked to the pi), it stopped monitoring and gave no value. (I tried to reboot the system; however the pi can’t read the temperature)

After this weird situation, I disconnected the last sensor (so subsequently total 4 sensors were connected to the pi); then the pi started to read the temperature. I have attached two photos of a breadboard with 4 and 5 sensors.

Breadboard with 4 sensors - work
Breadboard with 5 sensors - doesn't work

I really want someone can explain this to me.

Thank you for reading.


I updated config.txt and opend 4, 17, 27 pins. So now I am using 3 pins. As a first trial, I connected 4, 4, and 2 DS18B20s to 4, 17, and 27 pins, respectively. Interestingly, my RPi can find every sensors in this case. Followings are PCB and Terminal screen.

teminal with 4, 4, 2 connection
PCB with 4, 4, 2 connection

Then, I connected 5, 4, 1 DS18B20s to 4, 17, and 27 pins, respectively. In this time, my RPI can detect only 5 sensors which are connected to 17 and 27 pins. Followings are PCB and Terminal screen.

teminal with 5, 4, 1 connection
PCB with 5, 4, 1 connection

I am guessing that there is a restriction on the number of DS18B20 on a bus. Am I right?pi-3b+driverpi-4ds18b201-wireshareedit  follow  closeflagedited Oct 10 ’19 at 2:58tlfong013,41633 gold badges77 silver badges2222 bronze badgesasked Jul 2 ’19 at 7:32Sang Inn Woo4133 bronze badges

  • 2What is the electrical connection for these? Are you using powered sensors (3 wires) or parasitic power (2 wires only). Do you have a single pull-up for the one-wire bus (correct) or have you added a pull-up per sensor (incorrect)? Also provide a minimal code sample for inspection that demonstrates how you are reading the values. – patthoyts Jul 2 ’19 at 8:51
  • The datasheet suggests the following for better performance: (1) Use strong pullup 4k7, (2) Supply power at Vdd pin, not using parasite power at Dq. No harm trying. – tlfong01 Jul 2 ’19 at 8:53   
  • 1I am using powered sensors (3 wires) and a single pull-up. Basically I followed the circuit showed in (raspberrypi.org/forums/viewtopic.php?t=167896). I didn’t use any code currently as I have a trouble in recognizing sensors. I simply typed “ls” at /sys/bus/w1/devices/. – Sang Inn Woo Jul 2 ’19 at 14:54
  • 1I have attached photos of my breadboard setting. – Sang Inn Woo Jul 2 ’19 at 15:26
  • 1Try a stronger pull-up. – Brick Jul 2 ’19 at 17:33

add a commentstart a bounty

1 Answer



Rpi can handle at most 4 DS18B20s, how come so few? How can greedy me do as many as 60?

Short Answer

Update 2019nov20hkt1014

ds18b20 pullup

Update 2019jul26hkt2030

I have upgraded my Rpi3B+ stretch to Rpi4B buster. I test the same Rpi3B+ program for Rpi4B /buster and found everything work smoothly. The program v71 with updated documentation is listed in Appendix G. I have deleted the old program in Appendix F, because this answer is again getting too long, hitting the forum answer’s 32k words limit.

Update 2019jul17hkt1301

I connected 12 DS18B20 to GPIO2, and another 3 to GPIO3, and found measurements stable. I am using the I2C pins for now. I hope to test other pins after I finished testing Rpi4 and buster.

ds18b20 test
*** Begin testGetPrintSensorIdTempTimeList() **********************************************************

    Time Testing = 2019-07-17 12:49 tlfong01 

    Getting sensor temperatures, ...............

    Begin printSensorIdTempTimeList(), ...

      Sensor         Sensor    Temp                         Time
         Num             ID   deg C                             
           1   01161cd97fee   28.12   2019-07-17 12:49:22.365111
           2   01161cda20ee   28.12   2019-07-17 12:49:23.325028
           3   0316a2795c11   27.81   2019-07-17 12:49:24.284948
           4   021313a052aa   28.56   2019-07-17 12:49:25.244955
           5   011319f99467   28.38   2019-07-17 12:49:26.204966
           6   000001d8e47e   28.44   2019-07-17 12:49:27.164934
           7   0316a27926c3   28.25   2019-07-17 12:49:28.124953
           8   01131a39c3e9   28.56   2019-07-17 12:49:29.085026
           9   0316a279d285   28.25   2019-07-17 12:49:30.044939
          10   0316a27907c7   27.69   2019-07-17 12:49:31.004974
          11   02131261bcaa   28.38   2019-07-17 12:49:31.965575
          12   01131a2edfe9   28.50   2019-07-17 12:49:32.924948
          13   0316a27938eb   27.69   2019-07-17 12:49:34.845093
          14   01131bb23b09   28.19   2019-07-17 12:49:35.804940
          15   000001d8e502   28.44   2019-07-17 12:49:36.764982

    End   printSensorIdTempTimeList().

    Number of sensors tested = 15
    Seconds lapsed           = 16.69
    Seconds per sensor       = 1.11

*** End   testGetPrintSensorIdTempTimeList() **********************************************************

ds18b20 2019jul1402

Long Answer to be shortened or removed

Now I am testing 12 DS18B20 devices, all connected to one GPIO pin 2, configuring as star. Now I am finding that the syatem is not reliable, in that it takes longer to made one measurements, from about 1 second to up ta 1.5 seconds. I need to find if using daisy chain configuration can improve the performance.

Testing 11 sensors OK. All 11 sensors connected in parallel, power 3V3, all output pins connected to GPIO2 (Similarly to shown in the following post . Test program list and sample output shown in Appendix B.

wiring picture

Now I am going to test 9 sensor modules, suing 2 or 3 GPIO pins.

ds18b20 modules

Now I am testing 11 DS18B20 (7 with waterproof probes, two modules, two through hole chips), all on the same bus connected to GPIO2. I found measurements stable. See sample output and program listing in the appendix below.

ds18b20 modules

Long winded Research Notes to shorten or delete later

I guess, DS18B20, using the 1-Wire method, is like the trouble making I2C, cannot have wires too long (preferably less than 30cm), pull up not too strong, and not too weak (total around 4k7) capacitance less than 400pF etc.

For I2C, I had a very hard time and could achieve only placing 8 devices on the bus, but system still very unstable, often getting I/O error (due to impedance overloading, noise etc).

So for this 1-Wire DS18B20, I was humble and my goal was to get at most 8 devices on the 1-Wire bus. I started with onle one DS18B20, and added one after another, until I used up all my 7 devices. Surprisingly I found measurements very stable.

Now let me show off my setup, results, and program.



I think I confused everybody by using I2C data pin for DS18B20. Actually I can use any other pin. The reason that I choose I2C Data pin (GPIO 2) is that I know I2C CLK and DATA pins have a very strong pull up of 1k8. All other pins (only 90% sure) have pullups of 50k. In other words, I don’t need any extra 4k7.


  1. DS18B20 Programmable Resolution 1-Wire Digital Thermometer Datasheet – Maxim
  2. Guidelines for Reliable Long Line 1-Wire Networks, Tutorial 148 – Maxim
  3. w1thermsensor DS18B20 Python Module 1.1.2 – pypi
  4. Multiple DS18B20 1-Wire Temperature Sensors – rpi.org forum 2016dec09
  5. DS18B20 Wiring Diagram – raspberry pi forum discussion
  6. DS18B20 MAX DISTANCE -rpi forum geekinthesticks 2013maro6
  7. Joining Multiple DS18B20 Sensors – rpi forum Darranking 2019jan14
  8. 1-Wire – Wikipedia
  9. SunFounder DS18B20 Temperature Sensor Learning Kit
  10. Rpi3B+ Python DS2482 I2C to 1-Wire Bridge Connecting 10+ DS18B20 Temperature Sensors Using 30 Metres Long CAT5e Cables


Appendix A – Test Program 2019jul05

Appendix B – DS18B20 Test Program 2019jul11

Appendix C – DS18B20 12 devices testing notes

Appendix D/E – DS18B20 Test Program V50 2019jul14hkt1128

Listings too long hitting the StackExchange answer’s 32k word limit, therefore removed.

Appendix F – Program listing of version 62 2019jul17hkt1259

Appendix G – Version 71 Rpi4B Throny python 3.7.3

Update 2019nov20hkt1031

(1) DS18B20 test71 was written for Rpi3B+ stretch 9 and IDLE python 3.5.3. It is still compatible to Rpi4B buster python 3.7.3, without any modification.

(2) The program was developed in an almost pure functional programming and declarative programming paradigm, using lisp/scheme/LUA programming constructs, and OOP style without using OP constructs, only FP. It automatically scales up for any number of DS18B20 sensors. For example, there is no C++ like switch/case, nested if-then-else, numerical for loops. The same program can entertain more or less sensors without modifying any for loop range parameter, ie, “looping for list”, not for “range(length …) etc.

# DS18B20_test71 tlfong01 2019jul26hkt204
# Rpi3B+ Raspbian  9 stretch 2019jul15, IDLE   Python 3.5.3
# Rpi4B  Raspbian 10 buster  2019jul25, Throny Python 3.7.3
# python3-w1ThermSensor-1.0.5

# *** Setup Notes ***

# 1. "$ sudo nano /boot/config.txt" to config w1thermsensor GPIO dtoverlay 
# 2. GPIO 2/3 (I2C DATA/CLK) with built in 1k8 pull up is used.  For other
#    pins with weaker pullups, a 4k7 resistor is usually recommended.
# 3. dtoverlay=w1-gpio,gpiopin=2, dtoverlay=w1-gpio,gpiopin=3  
# 4. For two or more GPIO pins, w1thermsensor auto detects connected sensors,
#    but might hang if GPIO is specified but no senors are connected.
#    And remember to reboot after making changes to dtoverlay settings.
# 6. Terminal mode sensor detection command = "$ ls /sys/bus/w1/devices"

# *** Testing Results ***
# 1. Program V62 has been tested OK for 15 sensors (12 connected to GPIO2
#    and 3 to GPIO3.  All connecting cables are less than 1 metre long.
# 2. To test later - Rpi4, buster, long CAT5 cables, 5V power and logical
#    level shifter.

# Program Summary and Testing Notes
# 1. The three datetime functions in this sections are mainly for demo and testing.  They
#    can be removed after development is completed.
# 2. The data structure "sensorIdList" contains one member for each sensor, 
#    Each member consists of the id string of the sensor.
# 3. The get/print/getPrint SensorIdList is mainly used for detecting sensors which is
#      much quicker than the very slow sensor measurement process of around 1 second long.
# 4. The data structure "sensorIdTempTimeList", contains one member for each sensor.
#    Each member consists of the id, temperature read, time read of the sensor.
# 5. The getSensorIdTempTime fucntion is very slow, about 1 second to execute. 
# 6. The main tests can be executed all at once, or comment/uncomment to select lists.

from time          import sleep
from datetime      import datetime
from datetime      import timedelta
from w1thermsensor import W1ThermSensor

# *** Date Time Functions *** 

def printDateTimeNow():
    timeNowLong  = datetime.now()
    timeNowShort = str(timeNowLong)[0:16]
    print('\n    timeNowLong         =',  timeNowLong)
    print('    timeNowShort        =', timeNowShort)

def printElapsedTime():
    startTime = datetime.now()
    finishTime = datetime.now()

    timeElapsed    = finishTime - startTime
    secondsElapsed = timeElapsed.seconds
    microSecondsElapsed = (secondsElapsed * 1000000) + timeElapsed.microseconds
    milliSecondsElapsed = microSecondsElapsed / 1000

    print('\n    Start Time          =', startTime)
    print('    Finish Time         =', finishTime)
    print('\n    timeElapsed         =', timeElapsed)
    print('    secondsElapsed      =', secondsElapsed)
    print('    milliSecondsElapsed =', '%d' %milliSecondsElapsed)    
    print('    microSecondsElapsed =', microSecondsElapsed, '\n')

def testDateTime():
    print('\n*** Begin Test python datetime module, ... *******************************')
    print('*** End Testing python datetime module. **********************************')

# *** DS18B20 Functions ***

# *** SensorIdList Functions ***

def getSensorIdList():
    sensorIdList = []
    for sensor in W1ThermSensor.get_available_sensors():
    return sensorIdList

def printSensorIdList(sensorIdList):
    print('    ------------------------------------------------------------------------')
    print('    Sensor        ', '   Sensor')
    print('       Num        ', '       ID')
    print('    ------------------------------------------------------------------------')
    sensorNum = 1
    for sensorId in sensorIdList:
        if sensorNum < 10:
            print('        ', sensorNum, '    ', sensorId)
            print('       ', sensorNum, '    ', sensorId)    
        sensorNum = sensorNum + 1
    print('    ------------------------------------------------------------------------')
def getPrintSensorIdList():
    sensorIdList = getSensorIdList()

def testGetPrintSensorIdList():
    print('\n*** Begin testGetPrintSensorIdList() ******************************************************************\n')
    print('\n*** End   testGetPrintSensorIdList() ******************************************************************\n')

# *** SensorIdTempTimeList Functions ***

def getSensorIdTempTimeList():
    sensorIdTempTimeList = []
    print('    Getting sensor temperatures, ', end = '')
    for sensor in W1ThermSensor.get_available_sensors():
        idTempTimeList = (sensor.id, sensor.get_temperature(), datetime.now())
        print('.', end = '')
    return sensorIdTempTimeList

def printSensorIdTempTimeList(sensorIdTempTimeList):
    print('\n\n    Begin printSensorIdTempTimeList(), ...\n')
    print('      ----------------------------------------------------------')
    print('      Sensor', '        Sensor', '   Temp', '                        Time')
    print('         Num', '            ID', '  deg C', '                            ')
    print('      ----------------------------------------------------------')
    sensorNum = 1
    for idTempTimeList in sensorIdTempTimeList:
        if sensorNum < 10:
            print('          ', sensorNum, end = '')
            print('         ',  sensorNum, end = '')  
        print('  ', idTempTimeList[0], end = '  ')
        print('', '%.2f' %(idTempTimeList[1]), end = '  ')
        print('', idTempTimeList[2])
        sensorNum = sensorNum + 1
    print('      ----------------------------------------------------------')
    print('\n    End   printSensorIdTempTimeList().')

def testGetPrintSensorIdTempTimeList():
    print('\n*** Begin testGetPrintSensorIdTempTimeList() **********************************************************\n')
    startTime = datetime.now()
    print('    Time Testing =', str(startTime)[0:16], 'tlfong01 \n')

    sensorIdTempTimeList = getSensorIdTempTimeList()

    finishTime = datetime.now()
    timeElapsed    = finishTime - startTime
    secondsElapsed = timeElapsed.seconds
    microSecondsElapsed = (secondsElapsed * 1000000) + timeElapsed.microseconds

    sensorNum = len(sensorIdTempTimeList)
    print('\n    Number of sensors tested =', sensorNum)
    print('    Seconds lapsed           =', '%.2f' %(microSecondsElapsed / 1000000))
    print('    Seconds per sensor       =', '%.2f' %((microSecondsElapsed / 1000000) / sensorNum))
    print('\n*** End   testGetPrintSensorIdTempTimeList() **********************************************************\n')


# *** Main Test ***


Python 3.7.3 (/usr/bin/python3)
>>> %cd /home/pi/Python_Programs/test1321/ds18b20
>>> %Run ds18b20_test70_2019jul2501.py

*** Begin Test python datetime module, ... *******************************

    timeNowLong         = 2019-07-26 20:01:22.402122
    timeNowShort        = 2019-07-26 20:01

    Start Time          = 2019-07-26 20:01:22.402724
    Finish Time         = 2019-07-26 20:01:24.404806

    timeElapsed         = 0:00:02.002082
    secondsElapsed      = 2
    milliSecondsElapsed = 2002
    microSecondsElapsed = 2002082 

*** End Testing python datetime module. **********************************

*** Begin testGetPrintSensorIdList() ******************************************************************

    Sensor            Sensor
       Num                ID
         1      01161cd97fee
         2      01161cda20ee
         3      0316a2795c11
         4      021313a052aa
         5      011319f99467
         6      000001d8e47e
         7      0316a27926c3
         8      01131a39c3e9
         9      0316a279d285
        10      0316a27907c7
        11      02131261bcaa
        12      01131a2edfe9
        13      0316a27938eb
        14      01131bb23b09
        15      000001d8e502

*** End   testGetPrintSensorIdList() ******************************************************************

*** Begin testGetPrintSensorIdTempTimeList() **********************************************************

    Time Testing = 2019-07-26 20:01 tlfong01 

    Getting sensor temperatures, ...............

    Begin printSensorIdTempTimeList(), ...

      Sensor         Sensor    Temp                         Time
         Num             ID   deg C                             
           1   01161cd97fee   29.81   2019-07-26 20:01:25.373759
           2   01161cda20ee   29.75   2019-07-26 20:01:26.253930
           3   0316a2795c11   29.38   2019-07-26 20:01:27.133707
           4   021313a052aa   30.00   2019-07-26 20:01:28.014356
           5   011319f99467   29.88   2019-07-26 20:01:28.903601
           6   000001d8e47e   29.81   2019-07-26 20:01:29.853169
           7   0316a27926c3   29.88   2019-07-26 20:01:30.733717
           8   01131a39c3e9   29.94   2019-07-26 20:01:31.613679
           9   0316a279d285   29.62   2019-07-26 20:01:32.493157
          10   0316a27907c7   29.12   2019-07-26 20:01:33.373696
          11   02131261bcaa   29.88   2019-07-26 20:01:34.313676
          12   01131a2edfe9   29.62   2019-07-26 20:01:35.213715
          13   0316a27938eb   29.50   2019-07-26 20:01:36.093166
          14   01131bb23b09   29.44   2019-07-26 20:01:36.973689
          15   000001d8e502   29.94   2019-07-26 20:01:37.853752

    End   printSensorIdTempTimeList().

    Number of sensors tested = 15
    Seconds lapsed           = 13.48
    Seconds per sensor       = 0.90

*** End   testGetPrintSensorIdTempTimeList() **********************************************************


# *** End of Sample Output ***

# *** End of Program Documentaton ***

shareeditdeleteflagedited 26 secs agoanswered Jul 3 ’19 at 9:34tlfong013,41633 gold badges77 silver badges2222 bronze badges

  • 2Thanks; Do you think that I should rely on I2C to use multiple sensors? – Sang Inn Woo Jul 4 ’19 at 6:24
  • 1@Sang Inn Woo, Ah I guess you misunderstood my comments on I2C. I said I have bad experience using I2C bus and could not put more than 8 devices on the bus and performance is unstable. So I guessed DS18B20’s 1-Wire bus should be as bad. However, I started testing from 1 sensor and adding one by by one. Surprisingly I can add 7 (I have only 7 on hand!) and found everything stable. So I guess there is hope to add to 60! I am using the python3-w1ThermSensor-1.0.5. and I found it easy to setup and test. – tlfong01 Jul 4 ’19 at 6:33   
  • You might like to read my update, clarifying that I am NOT using I2C. I am still using 1-Wire bus. I can use any GPIO pin. I choose I2C Data pin because it has a built in strong pull up of 1k8. In other words, you can try any other pins. BTW, I don’t follow your terminal commands listing 1-Wire devices. Please show me the link for those commands. Thanks. – tlfong01 Jul 4 ’19 at 6:44   
  • I have found two more DS18B20 modules in my junk box. So I will test them soon. My rooftop garden need 64 sensors. So I also need to buy more. I also need to test long wires, because my garden is some 15 metres baove my flat. – tlfong01 Jul 5 ’19 at 2:42   
  • @Sang Inn Woo, I just finished testing 11 DS18B20s, all on the same bus connected to GPIO2. So far so good. I now think that you can use, say 6 GPIO pins for 6 1-Wire buses, each of which connects 10 DS18B20, with a total of 60. My testing this phase is coming to an end. Se you later. Cheers. – tlfong01 Jul 5 ’19 at 8:21    
  • 1@ tlfong01 Thank you very much for replying. I am just typing “cd /sys/bus/w1/devices” then “ls” to list DS18B20 sensors that I wired. Is my issue related to powering? In today’s trial, when I used 5V 3A USB adapter, I can find four DS18B20 sensors per one bus. However, when I changed USB adapter with 5V 1A, I can find only two sensors. BTW, it would be grealty helpful if you can share details of wiring for DS18B20. – Sang Inn Woo Jul 5 ’19 at 13:02 
  • Ah, what you are doing: “typing “cd /sys/bus/w1/devices” then “ls” to list DS18B20 sensors that I wired.” is the most basic way which is tedious. I would recommend you to use “python3-w1ThermSensor-1.0.5” which is what I am using. Yes, you need to use 5V 2.5A for Rpi3. I guess DS18B20 takes little power, but not very sure, need to check later. You seems a bit confused. Perhaps I can draw you a schematic during the weekend, or Monday. I hope I can find a couple of more DS18B20s from my junk box, then I can test a more comprehensive setup. – tlfong01 Jul 5 ’19 at 13:55   
  • 1Just as @tlfong01 did, I can now connect 10+ DS18B20 sensors to GPIO 2 or GPIO 3 (which are I2C buses) with stable reading. For GPIO 4, 17, 22, …, I tried an 1k resister instead of 4.7k for pull-up following the recommendation from @Brick; then I can connect 10+ DS18B20 sensors to those buses. I believe that the problem is now solved with your help. I will try 60 sensors wired to a RPi in early next week and update the post. – Sang Inn Woo Jul 17 ’19 at 11:03 
  • @Sang Inn Woo, yes, I agree that problem solved, at least for short cables. Wish you luck with 60 sensors. Cheers. – tlfong01 Jul 19 ’19 at 4:45   
  • @Sang Inn Woo, I have upgraded to Rpi4B and Throny python 3.7.3. I ran the old program and surprisingly found it ran smoothly. In other words the same old w1ThermoSensor driver works in Rpi4 python 3.7.3 without any changes. Now I am going to take a long break in this project, because I need to check if my other projects need any changes for my upgrade to Rpi4B. Wish you luck on your DS18B20 project. Cheers! – tlfong01 Jul 26 ’19 at 12:40   

add a comment


Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.