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?
1 Answer 1
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 withSerialUSB
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 (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.#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 (削除ここまで)
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.
-
1Thanks 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– daneos2022年12月21日 22:48:40 +00:00Commented 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.Mauro Pagano– Mauro Pagano2023年04月25日 16:40:13 +00:00Commented 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– timemage2023年04月26日 14:56:45 +00:00Commented Apr 26, 2023 at 14:56
begin()
method for DallasTemperature.