guild icon
Toit
#DS18b20 Using Multiple Devices on the same bus
Thread channel in help
Tyrone Smith
Tyrone Smith 08/20/2024 06:54 AM
I'm trying to use multiple DS18b20 temperature sensors:

import gpio // Sensors import ds18b20 show Ds18b20 // Temperatursensor GPIO-PIN-NUM1 ::= 2 // Outside Temperature GPIO GPIO-PIN-NUM2 ::= 5 // Inside top Temperature GPIO main: temp1 := gpio.Pin GPIO-PIN-NUM1 --input --pull-up=true //Temperature-sensor Outside ds1 := Ds18b20 temp1 temp2 := gpio.Pin GPIO-PIN-NUM2 --input --pull-up=true //Temperature-sensor Outside ds2 := Ds18b20 temp2 temperatur1 := ds1.read-temperature // Outside temperatur2 := ds2.read-temperature // Inside top print "Temperatur 1: $temperatur1" print "Temperatur 2: $temperatur2"


I found this comment in the ds18b20.toit lib but i honestly don't know how to make use of it.

Assumes there is only one sensor connected to the given $pin. If there are multiple sensors, then the driver misbehaves. In that case use $(Ds18b20.constructor --id --bus) instead.
(edited)
Tyrone SmithOPTyrone Smith
I'm trying to use multiple DS18b20 temperature sensors: ``` import gpio // Sensors import ds18b20 show Ds18b20 // Temperatursensor GPIO-PIN-NUM1 ::= 2 // Outside Temperature GPI...(edited)
floitsch
floitsch 08/20/2024 07:06 AM
Given that you use two different pins, the sensors aren't on the same bus.
floitsch
floitsch 08/20/2024 07:10 AM
If you want to use several ds18b20 on the same bus, then you would do a bus scan first.
Something like this: https://github.com/toitware/toit-ds18b20/blob/3bc3a4b3fa21dd7558cc53680aadae7f9ee9452c/examples/multi_bus.toit#L24
Contribute to toitware/toit-ds18b20 development by creating an account on GitHub.
Tyrone Smith
Tyrone Smith 08/20/2024 07:10 AM
If I want to use my approach, how can I fix this error?
floitsch
floitsch 08/20/2024 07:11 AM
On a standard esp32?
Tyrone Smith
Tyrone Smith 08/20/2024 07:12 AM
Yes
floitsch
floitsch 08/20/2024 07:13 AM
Esp32s3 or esp32?
Tyrone Smith
Tyrone Smith 08/20/2024 07:14 AM
casual esp32
floitsch
floitsch 08/20/2024 07:24 AM
The one-wire protocol (used by the ds18b20) uses the RMT peripheral underneath. There is only a limited number of them available on the device (4 iirc).
I thought that 1-wire takes two for each bus, but maybe it needs more. I will check later when I have a device at hand.
Tyrone Smith
Tyrone Smith 08/20/2024 07:24 AM
Okay, thank you!
lmk as soon as you have any updates
floitsch
floitsch 08/20/2024 08:26 AM
I can not reproduce so far.
floitsch
floitsch 08/20/2024 08:27 AM
Do you have any other program running that could use the RMT?
floitsch
floitsch 08/20/2024 08:27 AM
Wait
floitschfloitsch
Do you have any other program running that could use the RMT?
Tyrone Smith
Tyrone Smith 08/20/2024 08:31 AM
No, just the casual jaguar container is running
floitsch
floitsch 08/20/2024 08:31 AM
I can reproduce now. Was just a user error... Investigating
Tyrone Smith
Tyrone Smith 08/20/2024 08:32 AM
Thanks a lot!
floitsch
floitsch 08/20/2024 08:32 AM
While I'm looking: in the worst case you can just allocate and close the device inside the loop.
floitsch
floitsch 08/20/2024 08:33 AM
A bit heavier, but that would definitely allow you to read the two sensors.
Tyrone Smith
Tyrone Smith 08/20/2024 08:38 AM
import gpio // Sensors import ds18b20 show Ds18b20 // Temperatursensor GPIO-PIN-NUM1 ::= 2 // Outside Temperature GPIO GPIO-PIN-NUM2 ::= 5 // Inside top Temperature GPIO main: temp1 := gpio.Pin GPIO-PIN-NUM1 --input --pull-up=true //Temperature-sensor Outside ds1 := Ds18b20 temp1 temperatur1 := ds1.read-temperature // Outside print "Temperatur 1: $temperatur1" ds1.close temp1.close temp2 := gpio.Pin GPIO-PIN-NUM2 --input --pull-up=true //Temperature-sensor Outside ds2 := Ds18b20 temp2 temperatur2 := ds2.read-temperature // Inside top print "Temperatur 2: $temperatur2" ds2.close temp2.close


It works perfectly fine like this:
floitsch
floitsch 08/20/2024 08:38 AM
Exactly. But I'm still investigating why two buses aren't supported. My gut feeling was that two were fine and there weren't.
Tyrone Smith
Tyrone Smith 08/20/2024 08:39 AM
Okay, I'll just go with that solution for now, if you get the other one to work aswell i would prefer that.
Thank you for your help
👍1
floitsch
floitsch 08/20/2024 08:47 AM
Found it. It's a regression with recent releases. (A one character typo...).
The next (or maybe the one after) release will have this fixed.
Tyrone Smith
Tyrone Smith 08/20/2024 08:49 AM
Thank you for your support!
floitsch
floitsch 08/20/2024 08:51 AM
Thank you for letting us know. The bug could have lingered for a while without your report.
24 messages in total