Nano 33 BLE and SPI stops loop running

Hi I have the following code

#include int ledState = LOW; const unsigned long Spi_Hz = 8000000;

void setup() {
Serial.begin(115200);
while ( !Serial ) {
delay(1);
Serial.write(".");
}
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

Serial.write("\nloop");
ledState = ledState ? LOW: HIGH;
digitalWrite(LED_BUILTIN, ledState);
delay(400);
SPI.beginTransaction(SPISettings( Spi_Hz, MSBFIRST, SPI_MODE1));
SPI.endTransaction();
}

If I comment out the SPI code, you have the normal Blink result. However if the SPI lines are added there is no blinking and the loop is not complete. I know that there should be the chip select toggle around the SPI tranmission statement however I have narrowed the problem down to the two SPI lines. My code works on a Nano Every however.

Here are a few issues I found

  • For code tags you need square brackets.
  • The code in your while( ! Serial ) does not make sense. If Serial is not connected in cannot print the dots.
  • Your forgot to call SPI.begin() in setup().
  • Try to avoid using the delay() function. On the BLE it can be used for power saving because it uses sleep but you should not use it to control timing. Have a look at the following example

File → Examples → 02.Digital → BlinkWithoutDelay

  • You used the BUILTIN LED which is connected to the SPI clock pin. On the Arduino Nano 33 BLE you can use the green power LED.
  • Your example did not send any data.

Here is your example modified. If you slow down the SPI to 800kHz you can see the SPI clock light the orange LED.

#include <SPI.h>
#define SPI_SPEED_HZ    800000
#define BLINK_INTERVAL  400

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

  SPI.begin();

  pinMode( LED_PWR, OUTPUT );
}


void loop()
{
  static int ledState = LOW;
  static   unsigned long previousMillis = 0;

  unsigned long currentMillis = millis();
  if ( currentMillis - previousMillis >= BLINK_INTERVAL )
  {
    previousMillis = currentMillis;
    ledState = ledState ? LOW : HIGH;
    digitalWrite( LED_PWR,  ledState );
  }

  SPI.beginTransaction( SPISettings( SPI_SPEED_HZ, MSBFIRST, SPI_MODE1 ) );
  SPI.transfer( 0x55 );
  SPI.endTransaction();
}

Hi Klaus

Thankyou for the reply, the delay function and the BUILTIN LED was a real gotcha on the Nano 33 BLE. the SPI.begin was my error in cutting down the code to make a simple example.

I am not to sure if on creating a serial interface the serial writes are buffered until the Serial class is enabled. I was using the code to identify how long the initalisation took by counting the number of dots and then setting the delay.