Asking on Mega 2560 - simple code for writing a file in SD card

Hi friends,

I have one simple question. I have been staring to this project since last week and unfortunately the output/result is frustrating (got error and it couldn’t be compiled even). It is basically to write up a file type of .txt in the SD card. I followed exactly what had been asked in Adafruit learn website pertaining to Mega and Leo users for SD card tutorial (Older Datalogger Shield Leonardo & Mega Library | Adafruit Data Logger Shield | Adafruit Learning System).

The code is like this (https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial/library):

#include <SD.h>
 
File myFile;
 
void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
 
  pinMode(7, OUTPUT);
 
    // see if the card is present and can be initialized:
  if (!SD.begin(53, 51, 50, 52)) {

    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
 
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}
 
void loop()
{
	// nothing happens after setup
}

And the error I got after compiled:

Arduino: 1.6.1 (Windows 7), Board: “Arduino Uno”

In file included from test_SDstoreDataFn3.ino:1:0:

C:\Program Files (x86)\Arduino\libraries\SD/SD.h:24:17: fatal error: SPI.h: No such file or directory

#include <SPI.h>

^

compilation terminated.

Error compiling.

In file included from test_SDstoreDataFn3.ino:1:0:

C:\Program Files (x86)\Arduino\libraries\SD/SD.h:24:17: fatal error: SPI.h: No such file or directory

#include <SPI.h>

^

compilation terminated.

Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

I don’t know why is it happened like that as it just compiles the simple code. :sob: Hopefully you guys can help me out. Thanks!

Which part of the error message didn't you understand ?

The compiler cannot find SPI.h, which is the header file for the functions which implement SPI, the communication protocol used between the arduino and the SD card device.

SPI.h is one of the standard Arduino libraries.

You have not installed the Arduino environment properly, somehow.

Is this syntax correct?

if (!SD.begin(53, 51, 50, 52)) {

I can't get past that compiling, even with SPI.h added, SPI.begin() called out, and Mega selected for a board.

michinyon:
Which part of the error message didn't you understand ?

The compiler cannot find SPI.h, which is the header file for the functions which implement SPI, the communication protocol used between the arduino and the SD card device.

SPI.h is one of the standard Arduino libraries.

You have not installed the Arduino environment properly, somehow.

I did exactly what was written in Adafruit lear website (Older Datalogger Shield Leonardo & Mega Library | Adafruit Data Logger Shield | Adafruit Learning System) .

I tried it few times, I could not upload the code since it has error in compilation state.

CrossRoads:
Is this syntax correct?

if (!SD.begin(53, 51, 50, 52)) {

I can’t get past that compiling, even with SPI.h added, SPI.begin() called out, and Mega selected for a board.

I got the code from Adafruit learn website (Older Datalogger Shield Leonardo & Mega Library | Adafruit Data Logger Shield | Adafruit Learning System). It says in the website that for Mega users, there are few codes need to change same goes to the library in Arduino IDE which contains SD folder need to replace for a new one.

It doesn't matter what code or library you got from adafruit, if your compiler cannot find SPI.h, it means the Arduino software has not installed properly on your computer, or it was installed properly and is now messed up.

You need to look for SPI.h on your computer and make sure it is in the correct place for the compiler to find it.

I can't find SPI.h on my computer either. The compiler can find it, but I can't. Where is it buried now ?