Dear all,
after working some time with the classical OneWire and DallasTemperature Arduino lib the DS18B20 was not able to work poperly.
So I’ve installed the MaximWire lib at this repo:
https://github.com/adameat/MaximWire
and the sensor DS18B20 started to be seen immediately and the temperature measurements were correct. I’ve put a 4.7k resistor between the +3.3V and the data line of the sensor (yellow wire).
Here below my environment and setup.
Arduino IDE: 1.8.19
Board used: SEEED STUDIO XIAO nRF52840 sense
- pin D2 used for the DS18B20 sensor
- 4.7k resistor between +3.3V and pin D2
Boards definition files (use Board Manager):
- Seed nRF52 Boards: version 1.0.0
- Seed nRF52 mbed-enabled Boards: version 2.7.2
Library for manage OneWire (use Library Manager):
- MaximWire: version 1.0.3
Include at the top of the sketch:
#define MAXIMWIRE_EXTERNAL_PULLUP
#include <MaximWire.h>
MaximWire::Bus bus(2);
MaximWire::DS18B20 sensTemp;
MaximWire::Address sensorAddr[3]; // I can use max 3 external sensor in my design increase it if you need more sensor to detect
int sens_idx = 0; // Used to store the number of detected sensors
into the Setup function you can use following code to discover the sensors and store relative address for future reference:
MaximWire::Discovery discovery = bus.Discover();
do {
MaximWire::Address address;
if (discovery.FindNextDevice(address)) {
Serial.print("FOUND: ");
Serial.print(address.ToString());
sensorAddr[sens_idx++] = address;
if (address.IsValid()) {
Serial.print(" (VALID) ");
} else {
Serial.print(" (INVALID)");
}
if (address.GetModelCode() == MaximWire::DS18B20::MODEL_CODE) {
Serial.print(" (DS18B20)");
MaximWire::DS18B20 sensTemp(address);
if (sensTemp.IsParasitePowered(bus)) {
Serial.print(" (PARASITE POWER)");
}
float temp = sensTemp.GetTemperature<float>(bus);
Serial.print(" temp=");
Serial.print(temp);
Serial.println();
sensTemp.Update(bus);
} else {
Serial.println();
}
} else {
Serial.println("NOTHING FOUND");
}
} while (discovery.HaveMore());
Serial.println("DS18B20: Discovered " + String(sens_idx) + " temperature sensor(s)");
Into the main loop function, to read the sensors temperature:
for (int idx=0; idx<sens_idx; idx++){
MaximWire::Address address;
address = sensorAddr[idx];
if (address.GetModelCode() == MaximWire::DS18B20::MODEL_CODE) {
MaximWire::DS18B20 sensTemp(address);
float TempEstCelsius = sensTemp.GetTemperature<float>(bus);
if (!isnan(TempEstCelsius)) {
String strTempEst = "DS18B20 #" + String(idx)+ ": " + String(TempEstCelsius,3);
Serial.println(strTempEst);
}
sensTemp.Update(bus);
}
else
{
Serial.println("DS18B20 ERROR : Could not read temperature data");
}
}
such code was adapted from the examples at the GitHub repo listed above.
Hope it can help someone and save some time!
Best regards.
Fire