Uncategorized

DS18B20 References

***Question***

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

***Short Answer***

***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][1]][1]

*** 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][2]][2]

***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][3] . Test program list and sample output shown in Appendix B.

[![wiring picture][4]][4]

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

[![ds18b20 modules][5]][5]

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][6]][6]

***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][7]][7]

> 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.

***References***

1. [DS18B20 Programmable Resolution 1-Wire Digital Thermometer Datasheet – Maxim][8]

2. [Guidelines for Reliable Long Line 1-Wire Networks, Tutorial 148 – Maxim][9]

3. [w1thermsensor DS18B20 Python Module 1.1.2 – pypi][10]

4. [Multiple DS18B20 1-Wire Temperature Sensors – rpi.org forum 2016dec09][11]

5. [DS18B20 Wiring Diagram – raspberry pi forum discussion][3]

6. [DS18B20 MAX DISTANCE -rpi forum geekinthesticks 2013maro6][12]

7. [Joining Multiple DS18B20 Sensors – rpi forum Darranking 2019jan14][13]

8. [1-Wire – Wikipedia][14]

*(9) SunFounder Sensor Learning Kit including Ds18B20 and others*

https://tlfong01.blog/2019/09/05/sunfounder-sensors/

***Appendices***

*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*

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

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 ***

# *** SensorIdList Functions ***

def getSensorIdList():
sensorIdList = []
for sensor in W1ThermSensor.get_available_sensors():
sensorIdList.append(sensor.id)
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)
else:
print(‘ ‘, sensorNum, ‘ ‘, sensorId)
sensorNum = sensorNum + 1
print(‘ ————————————————————————‘)

def getPrintSensorIdList():
sensorIdList = getSensorIdList()
printSensorIdList(sensorIdList)
return

def testGetPrintSensorIdList():
print(‘\n*** Begin testGetPrintSensorIdList() ******************************************************************\n’)
getPrintSensorIdList()
print(‘\n*** End testGetPrintSensorIdList() ******************************************************************\n’)
return

# *** 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())
sensorIdTempTimeList.append(idTempTimeList)
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 = ”) else: print(‘ ‘, sensorNum, end = ”) print(‘ ‘, idTempTimeList[0], end = ‘ ‘) print(”, ‘%.2f’ %(idTempTimeList[1]), end = ‘ ‘) print(”, idTempTimeList[2]) sensorNum = sensorNum + 1 print(‘ ———————————————————-‘) print(‘\n End printSensorIdTempTimeList().’) return def testGetPrintSensorIdTempTimeList(): print(‘\n*** Begin testGetPrintSensorIdTempTimeList() **********************************************************\n’) startTime = datetime.now() print(‘ Time Testing =’, str(startTime)[0:16], ‘tlfong01 \n’) sensorIdTempTimeList = getSensorIdTempTimeList() printSensorIdTempTimeList(sensorIdTempTimeList) 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’) return # *** Main Test *** testDateTime() testGetPrintSensorIdList() testGetPrintSensorIdTempTimeList() ”’ 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 ***

/ to continue, …

[1]: https://i.stack.imgur.com/G2NXC.jpg
[2]: https://i.stack.imgur.com/5SgVs.jpg
[3]: https://www.raspberrypi.org/forums/viewtopic.php?t=167896#p1079565
[4]: https://i.stack.imgur.com/QABQC.jpg
[5]: https://i.stack.imgur.com/RucSh.jpg
[6]: https://i.stack.imgur.com/0bGA6.jpg
[7]: https://i.stack.imgur.com/JdaAw.jpg
[8]: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
[9]: https://www.maximintegrated.com/en/app-notes/index.mvp/id/148
[10]: https://pypi.org/project/w1thermsensor/
[11]: https://www.raspberrypi.org/forums/viewtopic.php?t=167896
[12]: https://www.raspberrypi.org/forums/viewtopic.php?t=36163#p303717
[13]: https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=231164#p1415739
[14]: https://en.wikipedia.org/wiki/1-Wire

.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