Arduino nano freezes when calling SPI.begin()

Hi there,

I am using an Arduino Nano, ATmega328P with the old bootloader. When I call SPI.begin() anywhere in the program (either in a seperate callable function, the setup or inside main) the program freezes and the Arduino doesn't seem to budge. Any chance I am doing anything wrong?

It does not matter if I have periphery hooked up or if I don't.

Other libraries which use SPI like the nRFLite library work just fine. I know this because I can successfully communicate with the nRF chip through the library and the example sketches work.

Seems likely.

Code is this.

#include <SPI.h>

int CEpin = 9;
int CSNpin = 10;

//the blinky function is used for debugging and just flashes the on-board LED
void blinky(int repeats, int pauseLength){
  for(int i = 1; i <= repeats; i++){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(pauseLength);
    digitalWrite(LED_BUILTIN, LOW);
    delay(pauseLength);
  }
}

void setup() {

  pinMode(CEpin, OUTPUT);
  pinMode(CSNpin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);


  blinky(5, 100);
  
  
  //de-select slave and set chip-enable of slave to 0
  digitalWrite(CEpin, LOW);
  digitalWrite(CSNpin, HIGH);
  
  //begin SPI with normal settings
  SPI.begin();


  blinky(25, 50);

}

void loop(){
}

Is there anything I am not seeing?

EDIT: Wanted to apologize for not posting the code in the first place.

D13, used for LED_BUILTIN, is also used by SPI, so you cannot use both.

1 Like

This is a really great hint already, thank you. I'll try disabling it and report back.

HALELUJAH IT WORKED!!!

Now, look into the following diagram (Fig-1) to understand that in SPI Mode, DPin-13 carries SCK signal.


Figure-13:

1 Like