0

I'm trying to use the DS18B20 sensor with Arduino Nano RP2040. No matter which library I use, it seems to crash MbedOS as soon as the driver is initialized. I get 4 short and 4 long flashes of the orange LED, which as I found somewhere indicates a crash.

I was trying with DS18B20 library (https://github.com/matmunk/DS18B20) as well as the DallasTemperature one (https://github.com/milesburton/Arduino-Temperature-Control-Library).
At first I suspected that the issue is in my code as it interfaces with multiple different devices (EEPROM, SHT30, etc.) and this may interfere with 1-wire interface, but if I upload just the examples from these libraries the effect is the same.

I know that the Arduino is OK, as it can run other code absolutely fine, and the sensor is OK as well since I can read it with RaspberryPi.

Does anyone have similar problems? Is there some workaround?

5
  • It's difficult to say much without being able to recreate the problem or at least inspect the code and potentially wiring as well. For certain values of "similar" you may get responses that aren't not exactly answers as they may not have anything to do with the problem you're actually having. You can edit your question to include more details.
    – timemage
    Commented Dec 18, 2022 at 18:38
  • @timemage As I said, the problem emerges even with example code from linked libraries. The wiring is not particularly interesting, just a 4.7k resistor between Vcc and data lines. I know that wiring is OK since I can connect RPi instead of Arduino and have readouts. I also suspect that incorrect wiring would prevent communication with the sensor but not outright crash the OS.
    – daneos
    Commented Dec 18, 2022 at 19:03
  • Yeah, sorry, I did miss that. If I stumble cross a DS18B20 I'll at least try it with an RPI pico (the closest thing I have to what you're using). But, I don't know how likely I am to find one or when. I can at least cancel out the -1. Hopefully someone will recreate the problem and have something useful to say about it long before I'm able to.
    – timemage
    Commented Dec 18, 2022 at 19:10
  • add a lot of debugging code to your program ...determine where it hangs
    – jsotola
    Commented Dec 18, 2022 at 21:42
  • @jsotola I found out it hangs in DS18B20 constructor for the DS18B20 library and in begin() method for DallasTemperature.
    – daneos
    Commented Dec 19, 2022 at 9:15

1 Answer 1

2

Test Setup

I have wired an DS18B20+ to an RPI Pico and used a 4.7K pullup. The RPI Pico is a stand-in for your board. It uses the arduino:mbed_rp2040:pico core rather than the arduino:mbed_nano:nanorp2040connect that yours does. However, I'm guessing they're similar enough.

Test result

When I attempt the to use the matmonk DS18B20 library you mention, I see similar pattern on the RPI Pico's onboard LED. Just the attempt to construct the object was sufficient to crash. Construction fairly directly defers to the OneWire library. So, I tried the OneWire library's own example for reading the DS18B20 (with some appropriate modification) and it crashes in the same manner. This lead to searching for and finding issues for the RP2040 on the OneWire github page resulting in (among others) this find. The answer mentions OneWireNG.

The OneWireNG DallasTemperature example works

Or rather, it's the library that works. But, this is the relevant example for it that works for me. I made the following modifications.

  • All Serial usages were replaced with SerialUSB because I am using the USB connection for the Serial Monitor (well, a terminal emulator).

  • I changed the one wire object to use GPIO28 because it was conveniently close to the pins where I was picking up 3.3V and GND: OneWire ds(28);

It seems plausible that you could change the #include <DS18B20.h> usages in the two libraries you cited to #include "OneWireNg_CurrentPlatform.h" and find that they then work fine. I have not tried this yet. If that's of particular interest to you I will try it and update(Update below). However, this stock example from the OneWireNG library is not crashing and is printing temperatures that rise and fall expected when I manipulate the temperature of the sensor.

OneWireNg with Matmunk and DallasTemperature libraries

In general

It turns out that it is unnecessary to modify libraries that were using the original OneWire, not even just changing the #include. Despite the OneWireNg example using "OneWireNg_CurrentPlatform.h" it also provides a OneWire.h header. It is sufficient to have OneWireNg installed but the original OneWire library must uninstalled (or moved out of the libraries directory) so as not to confuse the build process into selecting the original library based on the header name over the OneWireNg library. The OneWireNg authors may have used the OneWireNg specific header in the examples to avoid this problem wit their examples when both libraries were installed. They've gone out of their way to make it compatible with the original, but below are reports of specific tests with the two libraries mentioned anyway.

Matmunk library with OneWireNg

With OneWireNg installed and the original OneWire not installed, the Multiple example form the matmunk library worked with the same minor modifications mentioned before (I used pin 28 rather than 2 and SerialUSB rather than Serial). In case your wondering the only reason why I switched from testing Single to Multiple is the latter didn't require that I first find and enter my part's address. That example is printing temperatures that rise and fall in accordance with manipulation of the sensor.

DallasTemperature with OneWireNg

Likewise I tested with the Multiple example from the DallasTemperature library. Same kind of modifications to the example. I changed ONE_WIRE_BUS to 28 for my setup. I changed Serial to SerialUSB. And runs normally and prints expected temperatures under manipulation of the sensor.

3
  • 1
    Thanks so much! Using OneWireNg instead of OneWire indeed solves the problem. I looked for problems in DallasTemperature and DS18B20 libraries, but somehow it did not occur to me to look into the OneWire itself.
    – daneos
    Commented Dec 21, 2022 at 22:48
  • It didn't work for me. I removed the OneWire library and installed OneWireNg. I installed Mathias Munk Hanson's DS18B20 library and loaded the sketch found at this address: github.com/matmunk/DS18B20/blob/1.0.0/examples/Multiple/… Arduino Nano RP2040 crashes as soon as the sketch is loaded. Commented Apr 25, 2023 at 16:40
  • @MauroPagano, I see your comment was converted from an answer. I point that out mostly for the benefit for everyone (most people) that can't see that. It seems like it's the same content that I'd put under the "Matmunk library with OneWireNg" header. So maybe that's why it was converted to a comment. If you were trying to address that part of my answer, then I'm not entirely sure what to say except perhaps if you found a difference it was in the version of OneWireNg, which for me was almost certainly the 0.12.2 I'd linked.
    – timemage
    Commented Apr 26, 2023 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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