Nano 33 BLE hangs with Peripheral Explorer example

Hi all,

I just bought my Nano 33 BLE board, and testing with the Peripheral Explorer example. I just modified the code a little bit to print text on serial monitor in Setup and Loop function only. However, I found that the module hangs after 4 main loops on the line "BLE.available()".

/*
  Peripheral Explorer

  This example scans for BLE peripherals until one with a particular name ("LED")
  is found. Then connects, and discovers + prints all the peripheral's attributes.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use it with another board that is compatible with this library and the
  Peripherals -> LED example.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

int counter = 1;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  Serial.println("BLE Central - Peripheral Explorer");

  // start scanning for peripherals
  BLE.scan();
}

void loop() {

  Serial.println("Main Loop()");
  
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  Serial.println("Before IF");
  
  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    // see if peripheral is a LED
    if (peripheral.localName() == "LED") {
      // stop scanning
      BLE.stopScan();

      explorerPeripheral(peripheral);

      // peripheral disconnected, we are done
      while (1) {
        // do nothing
      }
    }
  }

  Serial.print("End of Main Loop, ");
  Serial.println(counter);
  ++ counter;
  delay(1000);
}

void explorerPeripheral(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // read and print device name of peripheral
  Serial.println();
  Serial.print("Device name: ");
  Serial.println(peripheral.deviceName());
  Serial.print("Appearance: 0x");
  Serial.println(peripheral.appearance(), HEX);
  Serial.println();

  // loop the services of the peripheral and explore each
  for (int i = 0; i < peripheral.serviceCount(); i++) {
    BLEService service = peripheral.service(i);

    exploreService(service);
  }

  Serial.println();

  // we are done exploring, disconnect
  Serial.println("Disconnecting ...");
  peripheral.disconnect();
  Serial.println("Disconnected");
}

void exploreService(BLEService service) {
  // print the UUID of the service
  Serial.print("Service ");
  Serial.println(service.uuid());

  // loop the characteristics of the service and explore each
  for (int i = 0; i < service.characteristicCount(); i++) {
    BLECharacteristic characteristic = service.characteristic(i);

    exploreCharacteristic(characteristic);
  }
}

void exploreCharacteristic(BLECharacteristic characteristic) {
  // print the UUID and properties of the characteristic
  Serial.print("\tCharacteristic ");
  Serial.print(characteristic.uuid());
  Serial.print(", properties 0x");
  Serial.print(characteristic.properties(), HEX);

  // check if the characteristic is readable
  if (characteristic.canRead()) {
    // read the characteristic value
    characteristic.read();

    if (characteristic.valueLength() > 0) {
      // print out the value of the characteristic
      Serial.print(", value 0x");
      printData(characteristic.value(), characteristic.valueLength());
    }
  }
  Serial.println();

  // loop the descriptors of the characteristic and explore each
  for (int i = 0; i < characteristic.descriptorCount(); i++) {
    BLEDescriptor descriptor = characteristic.descriptor(i);

    exploreDescriptor(descriptor);
  }
}

void exploreDescriptor(BLEDescriptor descriptor) {
  // print the UUID of the descriptor
  Serial.print("\t\tDescriptor ");
  Serial.print(descriptor.uuid());

  // read the descriptor value
  descriptor.read();

  // print out the value of the descriptor
  Serial.print(", value 0x");
  printData(descriptor.value(), descriptor.valueLength());

  Serial.println();
}

void printData(const unsigned char data[], int length) {
  for (int i = 0; i < length; i++) {
    unsigned char b = data[i];

    if (b < 16) {
      Serial.print("0");
    }

    Serial.print(b, HEX);
  }
}

From the serial monitor, I only see this:

BLE Central - Peripheral Explorer
Main Loop()
Before IF
End of Main Loop, 1
Main Loop()
Before IF
Found 41:2d:26:33:a3:0a ''
End of Main Loop, 2
Main Loop()
Before IF
Found 3c:6c:3f:2a:0d:ae ''
End of Main Loop, 3
Main Loop()
Before IF
Found 10:40:53:5d:69:ad ''
End of Main Loop, 4
Main Loop()

After that, no more text displayed and the board likes hanged, Green LED is ON, but then Orange LED flashes 4 long and 4 short pattern, so I guess the mbedOS somehow crashed. Why did my board hang like this? I just use the example code only. Is my board damaged?

FYI, I am using IDE 1.8.13 on Windows 10 and using USB 2.0 port. I tried FlashLED example, and it worked fine.

Thanks in advance.

I followed the guide from Arduino Nano 33 BLE damaged? - Nano 33 BLE - Arduino Forum, and tried to read the Mbed OS log from TX1, and I got this:

++ MbedOS Error Info ++
Error Status: 0x80010133
Code: 307 Module: 1
Error Message:
Mutex: 0x2001015C, Not allowed in ISR context
Location: 0x4784F
Error Value: 0x2001015C
Current Thread: main
Id: 0x20004364
Entry: 0x4777D
StackSize: 0x8000
StackMem: 0x20008128
SP: 0x2003FE54
For more info, visit: mbedos-error
-- MbedOS Error Info --

Your sketch seems to be running fine on my Arduino Nano 33 BLE (20 minutes+). I have tried with different delays as well.

However, I am not sure the delay in your sketch is a good idea. You expect the radio to scan for other BLE devices nearby and then sleep most of the time.

I have used the latest version of the ArduinoBLE library 1.1.3. Which version did you use?

Klaus_K:
Your sketch seems to be running fine on my Arduino Nano 33 BLE (20 minutes+). I have tried with different delays as well.

However, I am not sure the delay in your sketch is a good idea. You expect the radio to scan for other BLE devices nearby and then sleep most of the time.

I have used the latest version of the ArduinoBLE library 1.1.3. Which version did you use?

Interesting, I followed your idea, and simply removed the "delay", and there was no more problem at all. You are right, that's a bit wired there is a sleep after scan called, but at least the OS shouldn't hang, and seems to be a limitation. So, I removed the delay and there is no more problem now.