Adafruit BusIO/Adafruit_SPIDevice.h:1:17: fatal error: SPI.h: No such file or directory

Hello, I am trying to run a simple motor test with my Arduino board. I have installed the Adafruit BusIO library but I don’t know why I’m getting this error:

> Executing task in folder my_Motor: C:\Users\DDS\.platformio\penv\Scripts\platformio.exe run <

Processing megaatmega2560 (platform: atmelavr; framework: arduino; board: megaatmega2560)
-------------------------------------------------------------------------------------------------------------------------------------------------------------

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/megaatmega2560.html
PLATFORM: Atmel AVR 2.2.0 > Arduino Mega or Mega 2560 ATmega2560 (Mega 2560)
HARDWARE: ATMEGA2560 16MHz, 8KB RAM, 248KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES:
 - framework-arduino-avr 5.0.0
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 15 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Adafruit Motor Shield V2 Library> 1.0.9
|   |-- <Adafruit ILI9341> 1.5.6
|   |   |-- <Adafruit GFX Library> 1.9.0
|   |   |   |-- <Adafruit BusIO> 1.3.3
|   |   |   |   |-- <Wire> 1.0
|   |   |   |-- <Wire> 1.0
|   |   |-- <Adafruit STMPE610> 1.1.2
|   |   |   |-- <Wire> 1.0
|   |   |-- <Adafruit TouchScreen> 1.1.0
|   |-- <Adafruit GFX Library> 1.9.0
|   |   |-- <Adafruit BusIO> 1.3.3
|   |   |   |-- <Wire> 1.0
|   |   |-- <Wire> 1.0
|   |-- <Adafruit SSD1306> 2.3.1
|   |   |-- <Adafruit GFX Library> 1.9.0
|   |   |   |-- <Adafruit BusIO> 1.3.3
|   |   |   |   |-- <Wire> 1.0
|   |   |   |-- <Wire> 1.0
|   |   |-- <Wire> 1.0
|   |-- <AccelStepper> 1.59
|   |-- <Servo> 1.1.6
|   |-- <Wire> 1.0
|-- <Wire> 1.0
Building in release mode
Compiling .pio\build\megaatmega2560\libd39\Adafruit BusIO\Adafruit_BusIO_Register.cpp.o
Compiling .pio\build\megaatmega2560\libd39\Adafruit BusIO\Adafruit_SPIDevice.cpp.o
Compiling .pio\build\megaatmega2560\lib4ca\Adafruit GFX Library\Adafruit_GFX.cpp.o
Compiling .pio\build\megaatmega2560\lib4ca\Adafruit GFX Library\Adafruit_MonoOLED.cpp.o
In file included from C:\Users\DDS\.platformio\lib\Adafruit BusIO\Adafruit_SPIDevice.cpp:1:0:
C:\Users\DDS\.platformio\lib\Adafruit BusIO/Adafruit_SPIDevice.h:1:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

compilation terminated.
In file included from C:\Users\DDS\.platformio\lib\Adafruit BusIO/Adafruit_BusIO_Register.h:2:0,
                 from C:\Users\DDS\.platformio\lib\Adafruit BusIO\Adafruit_BusIO_Register.cpp:1:
C:\Users\DDS\.platformio\lib\Adafruit BusIO/Adafruit_SPIDevice.h:1:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

compilation terminated.
In file included from C:\Users\DDS\.platformio\lib\Adafruit GFX Library\Adafruit_MonoOLED.h:31:0,
                 from C:\Users\DDS\.platformio\lib\Adafruit GFX Library\Adafruit_MonoOLED.cpp:20:
C:\Users\DDS\.platformio\lib\Adafruit BusIO/Adafruit_SPIDevice.h:1:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

compilation terminated.
*** [.pio\build\megaatmega2560\libd39\Adafruit BusIO\Adafruit_SPIDevice.cpp.o] Error 1
*** [.pio\build\megaatmega2560\libd39\Adafruit BusIO\Adafruit_BusIO_Register.cpp.o] Error 1
*** [.pio\build\megaatmega2560\lib4ca\Adafruit GFX Library\Adafruit_MonoOLED.cpp.o] Error 1
================================================================ [FAILED] Took 3.18 seconds ================================================================
The terminal process "C:\Users\DDS\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Here is my simple code:

#include <Arduino.h>

#include <Wire.h>

#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// Or, create it with a different I2C address (say for stacking)

// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

// You can also make another motor on port M2

//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz

  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

  

  // Set the speed to start, from 0 (off) to 255 (max speed)

  myMotor->setSpeed(150);

  myMotor->run(FORWARD);

  // turn on motor

  myMotor->run(RELEASE);

}

void loop()

 {

  uint8_t i;

  

  Serial.print("tick");

  myMotor->run(FORWARD);

  for (i=0; i<255; i++)

   {

    myMotor->setSpeed(i);  

    delay(10);

  }

  for (i=255; i!=0; i--) 

  {

    myMotor->setSpeed(i);  

    delay(10);

  }

 }

How can get ride of this error?

Thank you
Joe

1 Like

Adding lib_deps = SPI to the platformio.ini doesn’t help? Or equally writing #include <SPI.h> as the first line of the sketch?

1 Like

I added #include <SPI.h> to the top and what do you know? It worked!

Thank you very much.
Joe

2 Likes

This solution doesnt work for me.

I have used lib_ldf_mode = deep+, lib_deps = SPI <others> and added #include <SPI.h> to the start of main.cpp, yet I still get the error.

Please state your full platformio.ini and code that you’re using.

Unfortunately, I cant do that.

I have worked around the issue by making a fork of the Radiohead library (the one causing issues) and adding a library.json file with SPI as a dependancy.

If you can find a minimal example that reproduces your issue (maybe basic platformio.ini with same platform, board and framework as your real project), let us know.

Adding #include <SPI.h> solved the issue. Could you please explain why it worked? Thank you!

Because this works around a platformio dependency scanning bug. When you are using a library that depends on the Adafruit BusIO library, the pio dependency scanner is confused by a C preprocessor guard that guards an #include <SPI.h> there:

#if !defined(SPI_INTERFACES_COUNT) ||                                          \
    (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))

#include <SPI.h>

During compilation, when targeting a standard Arduino board, the guard evaluates to true such that the compiler wants to include that header - but platformio hasn’t provided it.

So adding a plain #include <SPI.h> in your own code works around this issue because the pio dependency scanner picks up the dependency that way then. Alternatively, and arguably a cleaner work around is to simply add an SPI line to the lib_deps directive in your platformio.ini.

2 Likes

cool , i added the #include <SPI.h> then solved my problem too, thankyou

Thank you.
Its worked.