Arduino Nano Sense becomes unresponsive while connected via bluetooth

Hello,
I am trying to use the Arduino BLE Sense for my project, and while it is connected via Bluetooth, it can't handle a simple switch case with multiple analogwrites, and it becomes unresponsive. But it can just write multiple analogwrites, while it is not connected to Bluetooth. (EDIT: it can't write more than 4 in any case no matter connected or not connected to Bluetooth, 4 is maximum)
In the code below, it breaks after running analogWrite(M4, 255);
I have used two arduino ble 33 senses and both became responsive during writing multiple PWMs.

I can't understand what caused it to crash. The same code works on the Arduino IOT without issues.

#include <ArduinoBLE.h>

BLEService ledService("180A"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central

BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);

int M1 = 5;

int M2 = 6;

int M3 = 9;

int M4 = 17;

int M5 = 4;

int M6 = 7;

int M7 = 3;

int M8 = 2;

int M9 = 8;

int M10 = 10;

int M11 = 16;

int M12 = 14;

void setup() {

  Serial.begin(9600);

  //while (!Serial);

  // set LED's pin to output mode

  pinMode(LEDR, OUTPUT);

  pinMode(LEDG, OUTPUT);

  pinMode(LEDB, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(M1, OUTPUT);

  pinMode(M2, OUTPUT);

  pinMode(M3, OUTPUT);

  pinMode(M4, OUTPUT);

  pinMode(M5, OUTPUT);

  pinMode(M6, OUTPUT);

  pinMode(M7, OUTPUT);

  pinMode(M8, OUTPUT);

  pinMode(M9, OUTPUT);

  pinMode(M10, OUTPUT);

  pinMode(M11, OUTPUT);

  pinMode(M12, OUTPUT);

  digitalWrite(LED_BUILTIN, LOW);         // when the central disconnects, turn off the LED

  digitalWrite(LEDR, HIGH);               // will turn the LED off

  digitalWrite(LEDG, HIGH);               // will turn the LED off

  digitalWrite(LEDB, HIGH);                // will turn the LED off

  // begin initialization

  if (!BLE.begin()) {

    Serial.println("starting Bluetooth® Low Energy failed!");

    while (1);

  }

  // set advertised local name and service UUID:

  BLE.setLocalName("Nano 33 BLE Sense");

  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service

  ledService.addCharacteristic(switchCharacteristic);

  // add service

  BLE.addService(ledService);

  // set the initial value for the characteristic:

  switchCharacteristic.writeValue(0);

  // start advertising

  BLE.advertise();

 // Serial.println("BLE LED Peripheral");

}

void loop() {

  // listen for Bluetooth® Low Energy peripherals to connect:

  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:

  if (central) {

    Serial.print("Connected to central: ");

    // print the central's MAC address:

    Serial.println(central.address());

    digitalWrite(LED_BUILTIN, HIGH);            // turn on the LED to indicate the connection

    // while the central is still connected to peripheral:

    while (central.connected()) {

      // if the remote device wrote to the characteristic,

      // use the value to control the LED:

      if (switchCharacteristic.written()) {

        switch (switchCharacteristic.value()) {   // any value other than 0



          case 1:

            Serial.println("All on");

            analogWrite(M1, 255);

            analogWrite(M2, 255);

            analogWrite(M3, 255);

            analogWrite(M4, 255);

            analogWrite(M5, 255);

            analogWrite(M6, 255);

            analogWrite(M7, 255);

            analogWrite(M8, 255);

            analogWrite(M9, 255);

            analogWrite(M10, 255);

            analogWrite(M11, 255);

            analogWrite(M12, 255);

            digitalWrite(LEDR, LOW);         // will turn the LED off

            digitalWrite(LEDG, HIGH);       // will turn the LED off

            digitalWrite(LEDB, LOW);         // will turn the LED on

            delay (320);

            analogWrite(M1, 0);

            analogWrite(M2, 0);

            analogWrite(M3, 0);

            analogWrite(M4, 0);

            analogWrite(M5, 0);

            analogWrite(M6, 0);

            analogWrite(M7, 0);

            analogWrite(M8, 0);

            analogWrite(M9, 0);

            analogWrite(M10, 0);

            analogWrite(M11, 0);

            analogWrite(M12, 0);

            digitalWrite(LEDR, HIGH);               // will turn the LED off

            digitalWrite(LEDG, HIGH);               // will turn the LED off

            digitalWrite(LEDB, HIGH);                // will turn the LED off

            break;

          default:

            Serial.println(F("LEDs off"));

            digitalWrite(LEDR, HIGH);          // will turn the LED off

            digitalWrite(LEDG, HIGH);        // will turn the LED off

            digitalWrite(LEDB, HIGH);         // will turn the LED off

            break;

        }

      }

    }

    // when the central disconnects, print it out:

    //Serial.print(F("Disconnected from central: "));

    Serial.println(central.address());

    digitalWrite(LED_BUILTIN, LOW);         // when the central disconnects, turn off the LED

    digitalWrite(LEDR, HIGH);          // will turn the LED off

    digitalWrite(LEDG, HIGH);        // will turn the LED off

    digitalWrite(LEDB, HIGH);         // will turn the LED off

  }

}

I don't think this is correct. There is an undocumented limit on the number of pins you can use analogWrite on simultaneously on the Nano 33 BLE Sense. That limit applies regardless of whether or not you are using BLE.

More information here:

Yes, you're right. 4 PWM instances simultaneously seem like the maximum in any case. (I was testing with 4, not 5 or more pins :crazy_face:)

so, there is no way to fix this? and it is something implemented in the roots of MbedOS?

Unfortunately the references I shared in that post pretty much sum up what I know about the subject, and I had some difficulty even finding them.

I did a little bit more research on the subject a while back but didn't really get anywhere because I'm especially clueless when it comes to Mbed OS. My notes indicate I followed this chain down the rabbit hole before giving up:

Wow! Thank you very much for your efforts!

So, what I could do till now as a workaround is:
for every single switch, we should empty the pin right away and add a millisecond delay, which basically makes them not turn on at same time, and it may not be the best solution, but at least it doesn't make Arduino unresponsive :sweat_smile: here is an example:

            case 8:
            Serial.println("All on");
            for (int i = 0; i < 320; i++){ 
              analogWrite(M1, 120);
              analogWrite(M2, 120);
              analogWrite(M3, 120);
              analogWrite(M4, 120);
              
              delay (1);

              analogWrite(M1, -1);
              analogWrite(M2, -1);
              analogWrite(M3, -1);
              analogWrite(M4, -1);

              delay (1);

              analogWrite(M5, 120);
              analogWrite(M6, 120);
              analogWrite(M7, 120);
              analogWrite(M8, 120);
              
              delay (1);

              analogWrite(M5, -1);
              analogWrite(M6, -1);
              analogWrite(M7, -1);
              analogWrite(M8, -1);

              delay (1);

              analogWrite(M9, 120);
              analogWrite(M10, 120);
              analogWrite(M11, 120);
              analogWrite(M12, 120);
              
              delay (1);

              analogWrite(M9, -1);
              analogWrite(M10, -1);
              analogWrite(M11, -1);
              analogWrite(M12, -1);
            }
            break;
 

Yeah, I have taken a similar approach when I wanted to do PWM on an LED matrix with my Nano 33 BLE Sense.

It made the code a bit tricky to write, but once I had it set up to ensure that it always closed the previous channels before creating a new one I was able to use as many pins as I needed (just not all at the same time).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.