Uncategorized

DS18b20 testing notes

2
1

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 - workBreadboard with 5 sensors - doesn't work

I really want someone can explain this to me.

Thank you for reading.

UPDATE

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?

  • 1
    What 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 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 at 8:53   
  • I 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 at 14:54
  • I have attached photos of my breadboard setting. – Sang Inn Woo Jul 2 at 15:26
  • Try a stronger pull-up. – Brick Jul 2 at 17:33

0

Question

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

Short Answer

Update 2019jul14hkt1358

*DS18B20 Test Program 5.3 – listing not shown because the post word limit of 30,000 words limit has reached.*

ds18b20 2019jul1402

Update 2019jul14hkt1131

Higher level DS18B20 functions debugged. Full program listing in Appendix D.

def testTempSensor04():
    sensorResultsList = getSensorResults()
    printSensorResults(sensorResultsList)
    return

# *** Main Test ***

testTempSensor04()

# *** Sample Output ***

    Begin getSensorReults(), ...
      ......
    End   getSensorReults().

    Begin printSensorResults(), ...
        id = 021313a052aa  temperature = 28.00  time = 2019-07-14 11:15:30.219736
        id = 011319f99467  temperature = 27.81  time = 2019-07-14 11:15:31.179577
        id = 01131a39c3e9  temperature = 28.00  time = 2019-07-14 11:15:32.139501
        id = 02131261bcaa  temperature = 27.81  time = 2019-07-14 11:15:33.099509
        id = 01131a2edfe9  temperature = 27.94  time = 2019-07-14 11:15:34.059503
        id = 01131bb23b09  temperature = 27.56  time = 2019-07-14 11:15:35.019804
    End   printSensorResults().

Long Answer to be shortened or removed

Update 2019jul13hkt1621

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.

Time Testing = 2019-07-13 16:10
Sensor Num / Sensor ID / Temperature / Time

 1      01161cd97fee          27.25       2019-07-13 16:10:52.077123
 2      01161cda20ee          27.12       2019-07-13 16:10:53.169058
 3      0213120955aa          27.56       2019-07-13 16:10:54.208432
 4      021313a052aa          27.50       2019-07-13 16:10:56.139755
 5      011319f99467          27.25       2019-07-13 16:10:57.169933
 6      000001d8e47e          27.44       2019-07-13 16:10:58.233655
 7      01131a39c3e9          28.25       2019-07-13 16:11:01.011485
 8      0316a27907c7          27.00       2019-07-13 16:11:02.048253
 9      02131261bcaa          28.06       2019-07-13 16:11:03.088226
10      01131a2edfe9          27.94       2019-07-13 16:11:05.935576
11      01131bb23b09          27.69       2019-07-13 16:11:07.809340
12      000001d8e502          27.50       2019-07-13 16:11:08.862291

Number of sensors tested = 12 Seconds lapsed = 17.96 Seconds per sensor = 1.50

Update 2019jul11hkt1518

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

Update 2019jul50hkt1043

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

ds18b20 modules

Update 2019jul05hkt1610

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. A sample output is displayed below. A full program listing is in the appendix below.

>>> 
 RESTART: /home/pi/Python Programs/test1308/ds18b20/ds18b20_test34_2019jul0301.py 
  Begin Testing Ds18B20, ... 
     1   Sensor ID   = 01161cd97fee        Temperature = 30.31
     2   Sensor ID   = 01161cda20ee        Temperature = 30.00
     3   Sensor ID   = 0213120955aa        Temperature = 30.19
     4   Sensor ID   = 021313a052aa        Temperature = 30.12
     5   Sensor ID   = 011319f99467        Temperature = 29.81
     6   Sensor ID   = 000001d8e47e        Temperature = 30.56
     7   Sensor ID   = 01131a39c3e9        Temperature = 30.00
     8   Sensor ID   = 02131261bcaa        Temperature = 30.06
     9   Sensor ID   = 01131a2edfe9        Temperature = 29.94
    10   Sensor ID   = 01131bb23b09        Temperature = 29.50
    11   Sensor ID   = 000001d8e502        Temperature = 30.62
  End   Testing DS18B20, ...    
>>>

ds18b20 test

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.

Setup

setup

Update 2019jul04hkt1435

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.

Sample Output Results

'''
>>> 
 RESTART: /home/pi/Python Programs/test1306/ds18b20/ds18b20_test33_2019jul0301.py 
  Begin Testing Ds18B20, ... 
    1   Sensor ID   = 0213120955aa        Temperature = 29.94 
    2   Sensor ID   = 021313a052aa        Temperature = 28.56 
    3   Sensor ID   = 011319f99467        Temperature = 29.75 
    4   Sensor ID   = 01131a39c3e9        Temperature = 29.38 
    5   Sensor ID   = 02131261bcaa        Temperature = 29.62 
    6   Sensor ID   = 01131a2edfe9        Temperature = 29.38 
    7   Sensor ID   = 01131bb23b09        Temperature = 29.25 
  End   Testing DS18B20, ...
>>> 
'''

The Test Program

# DS18B20_test330 tlfong01 2019jul03hkt1658 ***
# Raspbian stretch9 2019jun20, Python 3.5.3
# python3-w1ThermSensor-1.0.5

# $ sudo nano /boot/config.txt
# dtoverlay GPIO pin assignment = 23, 19, 24, 26
# dtoverlay=w1-gpio,gpiopin=23 (SPI CLK)
# dtoverlay=w1-gpio,gpiopin=19 (SPI MOSI)
# dtoverlay=w1-gpio,gpiopin=24 (SPI CE0)
# dtoverlay=w1-gpio,gpiopin=26 (SPI CE1)
# dtoverlay=w1-gpio,gpiopin=2  (I2C DATA) - 2019jul02hkt2310

from w1thermsensor import W1ThermSensor

def printIdAndTemperatureOfSensors():
    sensorNum = 1
    for sensor in W1ThermSensor.get_available_sensors():
        print('   ', sensorNum, '  Sensor ID   =', sensor.id, end = '') 
        print('        Temperature = %.2f' %(sensor.get_temperature()), '\n')
        sensorNum = sensorNum + 1
    return

# *** Main Test ***

print('\n  Begin Testing Ds18B20, ... \n')
printIdAndTemperatureOfSensors()
print('\n  End   Testing DS18B20, ...\n')

# *** End of program ***

References

  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

Appendices

Appendix A – Test Program 2019jul05

    # DS18B20_test36 tlfong01 2019jul05hkt1601 ***
    # Rpi3B+ Stretch 9 2019jun20, Python 3.5.3
    # python3-w1ThermSensor-1.0.5

    # $ sudo nano /boot/config.txt
    # dtoverlay GPIO pin assignment = 23, 19, 24, 26
    # dtoverlay=w1-gpio,gpiopin=23 (SPI CLK)
    # dtoverlay=w1-gpio,gpiopin=19 (SPI MOSI)
    # dtoverlay=w1-gpio,gpiopin=24 (SPI CE0)
    # dtoverlay=w1-gpio,gpiopin=26 (SPI CE1)
    # dtoverlay=w1-gpio,gpiopin=2  (I2C DATA) - 2019jul02hkt2310

    from w1thermsensor import W1ThermSensor

    def printIdAndTemperatureOfSensors():
        sensorNum = 1
        for sensor in W1ThermSensor.get_available_sensors():
            if sensorNum < 10:
                print('    ', sensorNum, '  Sensor ID   =', sensor.id, end = '')
            else:
                print('   ', sensorNum, '  Sensor ID   =', sensor.id, end = '')    
            print('        Temperature = %.2f' %(sensor.get_temperature()), )
            sensorNum = sensorNum + 1
        return

    # *** Main Test ***

    print('\n  Begin Testing Ds18B20, ... \n')
    printIdAndTemperatureOfSensors()
    print('\n  End   Testing DS18B20, ...\n')

    # *** End of program ***

    # *** Sample Output 2019jul05hkt1602 ***
    '''
    >>> 
     RESTART: /home/pi/Python Programs/test1308/ds18b20/ds18b20_test34_2019jul0301.py 

      Begin Testing Ds18B20, ... 

         1   Sensor ID   = 01161cd97fee        Temperature = 30.31
         2   Sensor ID   = 01161cda20ee        Temperature = 30.00
         3   Sensor ID   = 0213120955aa        Temperature = 30.19
         4   Sensor ID   = 021313a052aa        Temperature = 30.12
         5   Sensor ID   = 011319f99467        Temperature = 29.81
         6   Sensor ID   = 000001d8e47e        Temperature = 30.56
         7   Sensor ID   = 01131a39c3e9        Temperature = 30.00
         8   Sensor ID   = 02131261bcaa        Temperature = 30.06
         9   Sensor ID   = 01131a2edfe9        Temperature = 29.94
        10   Sensor ID   = 01131bb23b09        Temperature = 29.50
        11   Sensor ID   = 000001d8e502        Temperature = 30.62

      End   Testing DS18B20, ...

    >>>
    '''
# *** End of Sample Output ***

Appendix B – DS18B20 Test Program 2019jul11

# DS18B20_test39 tlfong01 2019jul11hkt1352 ***
# Rpi3B+ Stretch 9 2019jun20, Python 3.5.3
# python3-w1ThermSensor-1.0.5

# $ sudo nano /boot/config.txt
# dtoverlay GPIO pin assignment = 23, 19, 24, 26
# dtoverlay=w1-gpio,gpiopin=23 (SPI CLK)
# dtoverlay=w1-gpio,gpiopin=19 (SPI MOSI)
# dtoverlay=w1-gpio,gpiopin=24 (SPI CE0)
# dtoverlay=w1-gpio,gpiopin=26 (SPI CE1)
# dtoverlay=w1-gpio,gpiopin=2  (I2C DATA) - 2019jul02hkt2310
# dtoverlay=w1-gpio,gpiopin=3  (I2C SCLK) - 2019jul10hkt1334

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)
    return

# *** Print Time/Seconds Elapsed ***

def printElapsedSeconds():
    time1 = datetime.now()
    sleep(2)
    time2 = datetime.now()

    timeElapsed    = time2 - time1
    secondsElapsed = timeElapsed.seconds 

    print('\n  time 1           =', time1)
    print('  time 2           =', time2)
    print('\n  timeElapsed      =', timeElapsed)
    print('  secondsElapsed   =', secondsElapsed, '\n')

def testDateTime():
    printDateTimeNow()
    printElapsedSeconds()
    return    

# *** DS18B20 Functions ***

def printIdAndTemperatureOfSensors():
    print('    --------------------------------------------------------------')
    print('    Sensor        ', 'Sensor ID   ', 'Temperature      ', '            Time')
    print('       Num        ', '            ', '    (deg C)      ', '    ')
    print('    --------------------------------------------------------------')
    sensorNum = 1
    for sensor in W1ThermSensor.get_available_sensors():
        if sensorNum < 10:
            print('        ', sensorNum, '    ', sensor.id, end = '')
        else:
            print('       ', sensorNum, '    ', sensor.id, end = '')    
        print('         ', '%.2f' %(sensor.get_temperature()), end = '')
        print('      ', str(datetime.now())[0:16])
        sensorNum = sensorNum + 1
    print('    --------------------------------------------------------------')
    return

def testDs18b20():
    printIdAndTemperatureOfSensors()
    return

def testTempSensor01():
    print('\nTest DS18B20 - ', str(datetime.now())[0:16])
    print('\n  Begin Testing Ds18B20, ... \n')
    testDs18b20()
    print('\n  End   Testing DS18B20.\n')
    return

# *** Main Test ***

testTempSensor01()

# *** End of program ***

# *** Sample Output 2019jul05hkt1602 ***
'''
>>> 
 RESTART: /home/pi/Python Programs/test1311/ds18b20/ds18b20_test38_2019jul1101.py 

Test DS18B20 -  2019-07-11 14:32

  Begin Testing Ds18B20, ... 

    --------------------------------------------------------------
    Sensor         Sensor ID    Temperature                   Time
       Num                          (deg C)           
    --------------------------------------------------------------
         1      01161cd97fee          29.50       2019-07-11 14:32
         2      01161cda20ee          29.44       2019-07-11 14:32
         3      0213120955aa          29.75       2019-07-11 14:32
         4      021313a052aa          29.69       2019-07-11 14:32
         5      011319f99467          29.50       2019-07-11 14:32
         6      000001d8e47e          29.56       2019-07-11 14:32
         7      01131a39c3e9          29.62       2019-07-11 14:32
         8      02131261bcaa          29.56       2019-07-11 14:32
         9      01131a2edfe9          29.38       2019-07-11 14:32
        10      01131bb23b09          29.12       2019-07-11 14:32
        11      000001d8e502          29.69       2019-07-11 14:32
    --------------------------------------------------------------

  End   Testing DS18B20.

>>> 
'''

# *** End of Sample Output ***

Appendix C – DS18B20 12 devices testing notes

# DS18B20_test46 tlfong01 2019jul13hkt16125 ***
# Rpi3B+ Stretch 9 2019jun20, Python 3.5.3
# python3-w1ThermSensor-1.0.5

# dtoverlay w1thermsensor 1-Wire GPIO pin assignment 
# $ sudo nano /boot/config.txt
# dtoverlay=w1-gpio,gpiopin=2  (I2C DATA)  # GPIO2 builtin pullup = 1k8
# dtoverlay=w1-gpio,gpiopin=3  (I2C SCLK) 
# dtoverlay=w1-gpio,gpiopin=23 (SPI CLK)
# dtoverlay=w1-gpio,gpiopin=19 (SPI MOSI)
# dtoverlay=w1-gpio,gpiopin=24 (SPI CE0)
# dtoverlay=w1-gpio,gpiopin=26 (SPI CE1)

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)
    return

# *** Print Time/Seconds Elapsed ***

def printElapsedTime():
    time1 = datetime.now()
    sleep(2)
    time2 = datetime.now()

    timeElapsed    = time2 - time1
    secondsElapsed = timeElapsed.seconds
    microSecondsElapsed = (secondsElapsed * 1000000) + timeElapsed.microseconds

    print('\n    Start Time          =', time1)
    print('    Finish Time         =', time2)
    print('\n    timeElapsed         =', timeElapsed)
    print('    secondsElapsed      =', secondsElapsed)
    print('    microSecondsElapsed =', microSecondsElapsed, '\n')

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

# *** DS18B20 Functions ***

def printIdAndTemperatureOfSensors():
    print('    ------------------------------------------------------------------------')
    print('    Sensor        ', 'Sensor ID   ', 'Temperature      ', '                      Time')
    print('       Num        ', '            ', '    (deg C)      ', '    ')
    print('    ------------------------------------------------------------------------')
    sensorNum = 1
    for sensor in W1ThermSensor.get_available_sensors():
        if sensorNum < 10:
            print('        ', sensorNum, '    ', sensor.id, end = '')
        else:
            print('       ', sensorNum, '    ', sensor.id, end = '')    
        print('         ', '%.2f' %(sensor.get_temperature()), end = '')
        print('      ', str(datetime.now()))
        sensorNum = sensorNum + 1
    print('    ------------------------------------------------------------------------')
    return sensorNum - 1

def testDs18b20():
    sensorNum = printIdAndTemperatureOfSensors()
    return sensorNum

def testTempSensor01():
    print('\n*** Begin Testing DS18B20  , ... *******************************************\n')
    startTime = datetime.now()
    print('    Time Testing =', str(startTime)[0:16], 'tlfong01 \n')
    sensorNum = testDs18b20()
    finishTime = datetime.now()
    timeElapsed    = finishTime - startTime
    secondsElapsed = timeElapsed.seconds
    microSecondsElapsed = (secondsElapsed * 1000000) + timeElapsed.microseconds
    print('\n    Number of sensors tested =', sensorNum)
    print('    Seconds lapsed           =', '%.2f' %(microSecondsElapsed / 1000000))
    print('    Seconds per sensor       =', '%.2f' %((microSecondsElapsed / 1000000) / sensorNum))
    print('\n*** End Testing DS18B20.**************************************************\n')

    return

# *** Main Test ***

# testDateTime()

testTempSensor01()

# *** End of program ***

# *** Sample Output 2019jul05hkt1602 ***
'''
>>> 
 RESTART: /home/pi/Python Programs/test1314/ds18b20/ds18b20_test45_2019jul1101.py 

*** Begin Testing DS18B20  , ... *******************************************

    Time Testing = 2019-07-13 16:10 tlfong01 

    ------------------------------------------------------------------------
    Sensor         Sensor ID    Temperature                             Time
       Num                          (deg C)           
    ------------------------------------------------------------------------
         1      01161cd97fee          27.25       2019-07-13 16:10:52.077123
         2      01161cda20ee          27.12       2019-07-13 16:10:53.169058
         3      0213120955aa          27.56       2019-07-13 16:10:54.208432
         4      021313a052aa          27.50       2019-07-13 16:10:56.139755
         5      011319f99467          27.25       2019-07-13 16:10:57.169933
         6      000001d8e47e          27.44       2019-07-13 16:10:58.233655
         7      01131a39c3e9          28.25       2019-07-13 16:11:01.011485
         8      0316a27907c7          27.00       2019-07-13 16:11:02.048253
         9      02131261bcaa          28.06       2019-07-13 16:11:03.088226
        10      01131a2edfe9          27.94       2019-07-13 16:11:05.935576
        11      01131bb23b09          27.69       2019-07-13 16:11:07.809340
        12      000001d8e502          27.50       2019-07-13 16:11:08.862291
    ------------------------------------------------------------------------

    Number of sensors tested = 12
    Seconds lapsed           = 17.96
    Seconds per sensor       = 1.50

*** End Testing DS18B20.**************************************************

>>> 
'''

# *** End of Sample Output ***

Appendix D – DS18B20 Test Program V50 2019jul14hkt1128

# DS18B20_test50 tlfong01 2019jul14hkt1112 ***
# Rpi3B+ Stretch 9 2019jun20, Python 3.5.3
# python3-w1ThermSensor-1.0.5

# dtoverlay w1thermsensor 1-Wire GPIO pin assignment 
# $ sudo nano /boot/config.txt
# dtoverlay=w1-gpio,gpiopin=  2 (I2C DATA)  # GPIO2 builtin pullup = 1k8
# dtoverlay=w1-gpio,gpiopin=  3 (I2C SCLK) 
# dtoverlay=w1-gpio,gpiopin= 23 (SPI CLK)
# dtoverlay=w1-gpio,gpiopin= 19 (SPI MOSI)
# dtoverlay=w1-gpio,gpiopin= xx (SPI MISO)
# dtoverlay=w1-gpio,gpiopin= 24 (SPI CE0)
# dtoverlay=w1-gpio,gpiopin= 26 (SPI CE1)

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)
    return

# *** Print Elapsed Time Functions ***

def printElapsedTime():
    startTime = datetime.now()
    sleep(2)
    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, ... *******************************')
    printDateTimeNow()
    printElapsedTime()
    print('*** End Testing python datetime module. **********************************')
    return    

# *** DS18B20 Functions ***

# *** TestTempSensor01 ***

def testTempSensor01():
    print('    ------------------------------------------------------------------------')
    print('    Sensor        ', 'Sensor ID   ', 'Temperature      ', '                      Time')
    print('       Num        ', '            ', '    (deg C)      ', '    ')
    print('    ------------------------------------------------------------------------')
    sensorNum = 1
    for sensor in W1ThermSensor.get_available_sensors():
        if sensorNum < 10:
            print('        ', sensorNum, '    ', sensor.id, end = '')
        else:
            print('       ', sensorNum, '    ', sensor.id, end = '')    
        print('         ', '%.2f' %(sensor.get_temperature()), end = '')
        print('      ', str(datetime.now()))
        sensorNum = sensorNum + 1
    print('    ------------------------------------------------------------------------')
    return sensorNum - 1

#def testDs18b20():
#    sensorNum = printIdAndTemperatureOfSensors()
#    return sensorNum

def testTempSensor02():
    print('\n*** Begin testTempSensor02 *******************************************\n')
    startTime = datetime.now()
    print('    Time Testing =', str(startTime)[0:16], 'tlfong01 \n')
    sensorNum = testTempSensor01()
    finishTime = datetime.now()
    timeElapsed    = finishTime - startTime
    secondsElapsed = timeElapsed.seconds
    microSecondsElapsed = (secondsElapsed * 1000000) + timeElapsed.microseconds
    print('\n    Number of sensors tested =', sensorNum)
    print('    Seconds lapsed           =', '%.2f' %(microSecondsElapsed / 1000000))
    print('    Seconds per sensor       =', '%.2f' %((microSecondsElapsed / 1000000) / sensorNum))
    print('\n*** End testTempSensor02 **************************************************\n')  
    return

def testTempSensor03():
    print('\n*** Begin testTempSensor03 **********************************************************\n')

    sensorResultsList = []

    for sensor in W1ThermSensor.get_available_sensors():
        idResultsList = (sensor.id, sensor.get_temperature())
        sensorResultsList.append(idResultsList)

        print('id =', idResultsList[0], end = '  ')
        print('temperature =', '%.2f' %(idResultsList[1]))

    print('')
    for idResultsList in sensorResultsList:
        print('id =', idResultsList[0], end = '  ')
        print('temperature =', '%.2f' %(idResultsList[1]))

    print('\n*** End   testTempSensor03 **********************************************************\n')
    return

def getSensorResults():
    print('    Begin getSensorReults(), ...\n', '     ', end = '')
    sensorResultsList = []
    for sensor in W1ThermSensor.get_available_sensors():
        idResultsTimeList = (sensor.id, sensor.get_temperature(), datetime.now())
        sensorResultsList.append(idResultsTimeList)
        print('.', end = '')
    print('\n    End   getSensorReults().')
    return sensorResultsList

def printSensorResults(sensorResultsList):
    print('\n    Begin printSensorResults(), ...')
    for idResultsTimeList in sensorResultsList:
        print('        id =', idResultsTimeList[0], end = '  ')
        print('temperature =', '%.2f' %(idResultsTimeList[1]), end = '  ')
        print('time =', idResultsTimeList[2])
    print('    End   printSensorResults().')
    return

def testTempSensor04():
    print('\n*** Begin testTempSensor04 **********************************************************\n')
    sensorResultsList = getSensorResults()
    printSensorResults(sensorResultsList)
    print('\n*** End   testTempSensor04 **********************************************************\n')
    return

# *** Main Test ***

# testDateTime()
# testTempSensor01()
# testTempSensor02()
# testTempSensor03()
testTempSensor04()

# *** End of program ***

# *** Sample Output ***

'''
>>> 
 RESTART: /home/pi/Python Programs/test1315/ds18b20/ds18b20_test49_2019jul1401.py 

*** Begin testTempSensor04 **********************************************************

    Begin getSensorReults(), ...
      ......
    End   getSensorReults().

    Begin printSensorResults(), ...
        id = 021313a052aa  temperature = 28.00  time = 2019-07-14 11:15:30.219736
        id = 011319f99467  temperature = 27.81  time = 2019-07-14 11:15:31.179577
        id = 01131a39c3e9  temperature = 28.00  time = 2019-07-14 11:15:32.139501
        id = 02131261bcaa  temperature = 27.81  time = 2019-07-14 11:15:33.099509
        id = 01131a2edfe9  temperature = 27.94  time = 2019-07-14 11:15:34.059503
        id = 01131bb23b09  temperature = 27.56  time = 2019-07-14 11:15:35.019804
    End   printSensorResults().

*** End   testTempSensor04 **********************************************************

>>> 
'''

# *** End of Sample Output ***

/ to continue, …

  • 1
    Thanks; Do you think that I should rely on I2C to use multiple sensors? – Sang Inn Woo Jul 4 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. – tlfong01Jul 4 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 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 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 at 8:21    
  • @ 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 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 at 13:55   

Not the answer you’re looking for? Browse other questions tagged   or ask your own question.

.END

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

%d bloggers like this: