Skip to content

Ard Spi End

DodaTech 1 min read

In this tutorial, you'll learn about Arduino SPI.end Causes Communication Failure After Restart. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Calling SPI.end() and then SPI.begin() again results in communication failure.

Quick Fix

Wrong

SPI.end();  // Disables SPI peripheral
SPI peripheral is disabled. Subsequent transfers have no effect.
#include <SPI.h>

const int csPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(csPin, OUTPUT);
  digitalWrite(csPin, HIGH);
}

void restartSPI() {
  SPI.end();  // Disable SPI
  delay(10);
  SPI.begin();  // Re-enable SPI
  // Reconfigure settings
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  Serial.println("SPI restarted");
}

void loop() {
  digitalWrite(csPin, LOW);
  SPI.transfer(0x42);
  digitalWrite(csPin, HIGH);
  delay(1000);
  restartSPI();  // Works correctly
  delay(5000);
}
SPI restarted — communication continues after restart.

Prevention

SPI.end() disables the SPI peripheral and releases the pins. After SPI.begin(), re-apply settings with SPI.beginTransaction(). SPI.end() is rarely needed — use it when entering sleep mode or when reconfiguring the SPI bus for a different device.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### When should I use SPI.end()?

Before entering deep sleep to save power. When switching between different SPI configurations on the same bus. Between SPI transactions from different libraries.

Does SPI.end() affect pin states?

Yes. The SPI pins become regular GPIO after end(). You must reinitialize with begin() before next use.

Can I call SPI.end() in an ISR?

No. SPI.end() may take significant time and should not be called from an ISR.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro