Bird
0
0
Arduinoprogramming~20 mins

SPI with SD card module in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SPI SD Card Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this SPI initialization code?

Consider the following Arduino code snippet initializing SPI for an SD card module. What will be printed on the Serial Monitor?

Arduino
void setup() {
  Serial.begin(9600);
  if (!SD.begin(10)) {
    Serial.println("Initialization failed!");
  } else {
    Serial.println("Initialization done.");
  }
}

void loop() {}
AInitialization done.
BInitialization failed!
CNo output, program stuck.
DCompilation error.
Attempts:
2 left
💡 Hint

Check if the SD card module is connected correctly and the chip select pin matches.

🧠 Conceptual
intermediate
1:30remaining
Which SPI mode is typically used by SD card modules?

SPI communication has four modes (0 to 3) defined by clock polarity and phase. Which mode is standard for SD card modules?

AMode 0 (CPOL=0, CPHA=0)
BMode 1 (CPOL=0, CPHA=1)
CMode 2 (CPOL=1, CPHA=0)
DMode 3 (CPOL=1, CPHA=1)
Attempts:
2 left
💡 Hint

SD cards use SPI mode where clock idles low and data is sampled on the rising edge.

🔧 Debug
advanced
2:30remaining
Why does this SD card write code fail to save data?

Examine the code below that attempts to write "Hello" to a file on the SD card. Why does it fail to save the data?

Arduino
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  Serial.begin(9600);
  if (!SD.begin(10)) {
    Serial.println("SD init failed");
    return;
  }
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.print("Hello");
    Serial.println("Write done");
    myFile.close();
  } else {
    Serial.println("Error opening file");
  }
}

void loop() {}
AThe SPI library is missing initialization.
BThe chip select pin 10 is incorrect for SD.begin().
CThe file mode FILE_WRITE is invalid.
DThe file is not closed after writing, so data is not saved.
Attempts:
2 left
💡 Hint

Think about what happens after writing to a file on SD cards.

📝 Syntax
advanced
1:30remaining
What error does this SPI transfer code produce?

Look at the code snippet below. What error will it cause when compiled?

Arduino
byte data = SPI.transfer();
ARuntime error: null pointer exception
BNo error, returns 0 by default
CCompilation error: missing argument for SPI.transfer()
DCompilation error: SPI library not included
Attempts:
2 left
💡 Hint

Check the required parameters for SPI.transfer().

🚀 Application
expert
3:00remaining
How many files are created by this SD card code?

Given the code below, how many files will be created on the SD card after running setup() once?

Arduino
#include <SPI.h>
#include <SD.h>

void setup() {
  Serial.begin(9600);
  SD.begin(10);
  for (int i = 0; i < 3; i++) {
    String filename = "file" + String(i) + ".txt";
    File f = SD.open(filename.c_str(), FILE_WRITE);
    if (f) {
      f.println("Data " + String(i));
      f.close();
    }
  }
}

void loop() {}
A3 files named file0.txt, file1.txt, file2.txt
BNo files created due to missing SD card check
C1 file named file2.txt only
DInfinite files created due to loop error
Attempts:
2 left
💡 Hint

Look at the loop and how filenames are generated.