https://toitlang.org/ logo
DS18b20 Using Multiple Devices on the same bus
# help
t
I'm trying to use multiple DS18b20 temperature sensors:
Copy code
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.
Copy code
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.
f
Given that you use two different pins, the sensors aren't on the same bus.
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
f
On a standard esp32?
t
Yes
f
Esp32s3 or esp32?
t
casual esp32
f
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.
t
Okay, thank you! lmk as soon as you have any updates
f
I can not reproduce so far.
Do you have any other program running that could use the RMT?
Wait
t
No, just the casual jaguar container is running
f
I can reproduce now. Was just a user error... Investigating
t
Thanks a lot!
f
While I'm looking: in the worst case you can just allocate and close the device inside the loop.
A bit heavier, but that would definitely allow you to read the two sensors.
t
Copy code
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:
f
Exactly. But I'm still investigating why two buses aren't supported. My gut feeling was that two were fine and there weren't.
t
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
f
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.
t
Thank you for your support!
f
Thank you for letting us know. The bug could have lingered for a while without your report.
2 Views