analogWrite Crashes NANO 33BLE Sense

/*

  • If you uncomment more than 4 calls in the void loop or do a "for loop" that enables
  • more that 4 pins it freezes the NANO 33 BLE Sense at that line of code. It does
  • not seem to make any difference if anything is attached to the board or not. If you
  • run the same test with digitalWrite there is no issue. It also does not matter which 4 Digital
  • pins.
    */

#include <Arduino_HTS221.h>
#include <Arduino_APDS9960.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_APDS9960.h>
#include <PDM.h>
#include <DS3231.h> //RTC

//Globals

//Digital PINS
int pin_actIS=2; //Actuator Motor Current Alarm
int pin_actEN=3; //Actuator Motor Arm
int pin_actRPWM=4; //Actuator Motor Forward PWM
int pin_actLPWM=5; //Actuator Motor Reverse PWM
int pin_fan1=6; //Fan 1 MOSFET
int pin_fan2=7; //Fan 2 MOSFET
int pin_heat=8; //Heater MOSFET

void setup() {

//Pinmode
pinMode(pin_actIS,OUTPUT);
pinMode(pin_actEN,OUTPUT);
pinMode(pin_actRPWM,OUTPUT);
pinMode(pin_actLPWM,OUTPUT);
pinMode(pin_fan1,OUTPUT);
pinMode(pin_fan2,OUTPUT);
pinMode(pin_heat,OUTPUT);

//Serial Startup
Serial.begin(9600);
while (!Serial) {}
for (int i = 0; i<5; i++){
Serial.print("*");
delay (1000); //Delay running program for 5 seconds
}
Serial.println(" Initiate Program *****");
Serial.println("");

}

void loop() {

test2 ();
test3 ();
test4 ();
test5 ();
test6 ();
test7 ();
test8 ();

}

void test2 (){

analogWrite(pin_actIS,255);
delay (1000);
analogWrite(pin_actIS,0);
delay (1000);

Serial.println("pin_actIS");

}

void test3 (){

analogWrite(pin_actEN,255);
delay (1000);
analogWrite(pin_actEN,0);
delay (1000);

Serial.println("pin_actEN");

}

void test4 (){

analogWrite(pin_actRPWM,255);
delay (1000);
analogWrite(pin_actRPWM,0);
delay (1000);

Serial.println("pin_actRPWM");

}

void test5 (){

analogWrite(pin_actLPWM,255);
delay (1000);
analogWrite(pin_actLPWM,0);
delay (1000);

Serial.println("pin_actLPWM");

}

void test6 (){

analogWrite(pin_fan1,255);
delay (1000);
analogWrite(pin_fan1,0);
delay (1000);

Serial.println("pin_fan1");

}

void test7 (){

analogWrite(pin_fan2,255);
delay (1000);
analogWrite(pin_fan2,0);
delay (1000);

Serial.println("pin_fan2");

}

void test8 (){

analogWrite(pin_heat,255);
delay (1000);
analogWrite(pin_heat,0);
delay (1000);

Serial.println("pin_heat");

}

Hi @jpvanier. As you discovered, the Nano 33 BLE has an undocumented limitation that you can only use analogWrite() on a maximum of four pins at a time.

Also undocumented is that you can set the value to -1 to free that object so that analogWrite() can be used on another pin. So you can write a sketch to do analogWrite() on all the pins of the board just so long as you never go over four at a time and make sure to free up a slot before starting it on a new pin.

1 Like