XIAO with DS18B20 sensor

Dears,
please support me with issue I am facing -
I try to connect DS18B20 to Xiao NR52840, using the code from library examples:

OneWire  ds(12);  // on pin 2 (a 4.7K resistor is already installed on Grove DS18B20 sensor, or not?)

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[9];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

After I upload the code, COM5 that i connected to the board disappears and XIAO needs to be rebooted (ERROR: Failed to upgrade target. Error is: No data received on serial port. Not able to proceed…). I do the wiring on XIAO Expansion board, data wire of DS18B20 connected to PIN2 of XIAO with jumper, power to 3V.
Pls support me to resolve the issue.

Does a simple blink app work?
It sounds like you are using an expansion board, if you take the Xiao off the expansion board does a simple blink app then work?

There have been instances reported where using the expansion board with a Xao has resulted in toasted expansion boards.
Rumor has it, though not confirmed, is that the 3.3v on the Xiao and the expansion board are tied together, that causes a problem.
Symptom, it suddenly stops working, the expansion board is noticeably warm to the touch.
A workaround is to not solder a pin on the Xiao before putting it into the expansion board.
I have two that are expansion boards that are bad apparently due to this, though I cannot absolutely confirm this was the cause of failure.

One can cut a trace on the expansion board to get the SWD pins back, but not so the remaining functionality, I guess, I’ve not tried other than SWD interface.
The Xiao boards seem to be unhurt.

Again, this is rumor has it.

For starters, disconnect the DS18B20, and anything else you happen to have connected to your XIAO BLE board and see what happens.

Here’s what happened when I ran your code:
The Blue LED on my XIAO BLE Sense blinked at a rate of four per second.

Here’s why:

Arduino pin 12 is actually CPU GPIO pin P0.06, which is connected to the cathode of the Blue part of the RGB LED.

The code in the loop sends a 480 microsecond “DS18B20 Reset” low pulse every 250 milliseconds. There’s nothing to respond (by pulling the DQ line low after the “Reset” pulse returns high), so ds.search() fails, and the code prints “No more addresses.” each time through the loop.

Now, if you want to see some action on The XIAO BLE Sense Pin 2, change your OneWire object instantiation to something like this:

// Note that a 4.7K Pullup resistor from DQ to 3.3V is recommended
OneWire  ds(2);  // DS18B20 DQ Line to XIAO Pin 2

Now, I don’t have a Grove DS18B20 sensor, but its Wiki page indicates that you need the external 4.7K Ohm pullup to Vdd. I guess it’s just a barefoot DS18B20 device with some waterproofing and a cable.

Anyhow…
With a pullup resistor on Pin 2 and no DS18B20, I see the Reset pulse.

Regards,

Dave

Footnote: If you can continue operating after going into boot mode, there’s a good chance that your XIAO is OK. I didn’t have your reboot problem with this sketch, but I have seen some strange behavior with (Other People’s) sketches that use “unusual” GPIO pins in unintended ways.

@davekw7x , yes after boot mode all comes back to the operation. I tried to change to pin 2 - same result.
with regards to 4,7k resistor - in the it says it is already inside of the item.
i will continue to dig then.

Arduino pin 12 is actually CPU GPIO pin P0.06, which is connected to the cathode of the Blue part of the RGB LED.
is there any comparison table Arduino Pin X = Pin Xiao=Pin Xiao expansion board?

@leroyle - yes i am using XIAO expansion board, will try to run the code on breadboard today, let’s see the result.
thank you all, will keep trying to find the solution

I recently posted tables that are in variants.cpp for board package 1.0.0 and 2.7.2 here:
Totally new in this and need a basic question answered… (How to map device pin with pin numbers in Arduino Code - Products & Technology / XIAO - Seeed Forum (seeedstudio.com)

Regards,

Dave
Footnote: I usually use solderless breadboards to connect CPUs and external circuity, but for various XIAO family modules I sometimes use the XIAO Expansion board. The test I mentioned in the previous post works either way. Have not had problems with BLE modules on the XIAO Expansion Board (or XIAO or XIAO rp2040 modules either).

I tried all code examples that i found and no luck.
Seems it is not possible to use one wire protocol on XIAO NRF52840, or at least it is not on the level of coding that i have.
there are some discussions about the topic
https://forum.arduino.cc/t/nano33-ble-onewire-issues/651767/4
I have almost gave up to use DS18B20 …

same here. I have had more than five (5) successful Arduino projects using One Wire DS18B20 and using it with XIAO nRF52840 did not work for me. I got stuck and gave up and reverted to using an i2c based temp sensor

Well…
I dug up my old DS18B20, in the TO-92 package, and connected it (on a solderless breadboard) to my XIAO BLE Sense module:

DS18B20 Pin 1 to Gnd from the XIAO BLE Sense

DS18B20 Pin 2 to Pin D2 of the XIAO BLE Sense, with a 4.7K Resistor pulled up to 3.3V

DS18B20 Pin 3 to 3.3V from the XIAO BLE Sense

I took your software and made the following changes:

1.
I put the following at the top of the file

 #include <OneWire.h>

2.
I changed your OneWire object instantiation to

OneWire  ds(2);  // On pin 2 ---A 4.7K pullup resistor is required

Then I loaded board package 1.0.0 from the Board Manager’s “Seeed nRF52 mbed-enabled Boards” package, and selected the “Seeed nRF52840” board. (Doesn’t matter whether you select “Seeed nRF52840” or “Seeed nRF52840 Sense”)

You can try one of the other board options if you want, but I’m telling you what works for me. (See Footnote.)

Anyhow, output is now consistent with temperature measured at the DS18B20 with my handy-dandy GM320 Infrared Thermometer.

ROM = 28 5F 1 A8 2 0 0 23
  Chip = DS18B20
  Data = 1 DB 1 4B 46 7F FF 5 10 27  CRC=27
  Temperature = 29.69 Celsius, 85.44 Fahrenheit
No more addresses.

ROM = 28 5F 1 A8 2 0 0 23
  Chip = DS18B20
  Data = 1 DB 1 4B 46 7F FF 5 10 27  CRC=27
  Temperature = 29.69 Celsius, 85.44 Fahrenheit
No more addresses.
.
.   Etc.
.

Regards,

Dave

Footnote:
[/Begin Editorial Rant]
The XIAO BLE boards are terrific hardware items. Have used on a couple of experimental BLE projects. Not bad.
However…
The software is VERRRRRRRY Frustrating. Several releases that are, in some unpredictable, inexplicable, and inexcusable ways, incompatible. I wish Seeed would start all over. Make another board with these capabilities but give it another name. And don’t release a blasted bloomin’ thing until and unless SOMEONE would actually test the software and would publish examples that actually work “out of the box.” Life is simply too short to for some of us to keep dicking around with different versions that more-or-less work (mostly less).
[/End Editorial Rant]

2 Likes

I agree with you 1000% They would have a corner on the market if their software was at minimum tested, updated and maintained. Wishful thinking or sage business advice. They can decide.
I have put 6 months or more in the Xiao boards BLE and BLE Sense, That Nordic chip is very good…nRF52840. Great Boards as you say.
They seem to have too many items in the fire and it is starting to show. Great for prototypes but I would NEVER stake my product on it. too easy to roll your own now days. love the fitment though :smile:

it sounds like this is yet another issue with the XIAO nRF52 board library

there is a long thread somewhere in this forum about the XIAO nRF52’s deep sleep mode and the guys from (working at) Seeed suggested to revert to 1.0.0 version of the board lib. Same approach here – and good to know that OneWire works with that version.

But, again… it’s just that… the version 1.0.0 of their Board software is where some of the stuff works, but other (like me) have to use the latest version coz that is where the other/newer Libraries will only work.

Example: I needed the nRF52’s deep sleep and ArduinoBLE to work together. so I gave up – choosing to find an external solution on the power conservation and continued with using ArduinoBLE and the 2.x version of their Board.

I had the same problem. XIAO nRF52840 jammed every time I tried to use OneWire.h library.
Found today MaximWire that doesn’t need OneWire library. Got it via Library Manager
Example DiscoveryDevices.ino worked right a way.
MaximWire can also be found here in Github with examples: GitHub - adameat/MaximWire: MaximWire library for Arduino 33 BLE and DS18B20 sensor
Adding this line to other examples, they also started to work for me:
#define MAXIMWIRE_EXTERNAL_PULLUP
Without the line it looked like the bus pin stayd in input status, as it wasn’t able to pull the line down. Adding the line, the pin was able to drive the one wire bus.
Many thanks to ADAMEAT.

1 Like

hi i can make working in non mbed code with adafruit aproach just adding this line to example
#include “Adafruit_TinyUSB.h”
apperars adfruit resolves this issues but the seed not update new core updates form adafruit, can be good for final users if they can work together but looks more rival companies cheers this line fix alots of problems wiht others librarys too
#include “Adafruit_TinyUSB.h”
PD; anyway i install al nrf52 boards