Bird
Raised Fist0
Arduinoprogramming~20 mins

Writing data to SD card in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SD Card Writing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output on the Serial Monitor?

Consider this Arduino code that writes to an SD card and prints a message to the Serial Monitor. What will be printed on the Serial Monitor?

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

  const int chipSelect = 10;

  void setup() {
    Serial.begin(9600);
    while (!Serial) {}

    if (!SD.begin(chipSelect)) {
      Serial.println("Initialization failed!");
      return;
    }

    File dataFile = SD.open("log.txt", FILE_WRITE);

    if (dataFile) {
      dataFile.println("Test data");
      dataFile.close();
      Serial.println("Write successful");
    } else {
      Serial.println("Error opening file");
    }
  }

  void loop() {}
AError opening file
BWrite successful
CInitialization failed!
DNo output
Attempts:
2 left
💡 Hint

Think about what happens if the SD card initializes correctly and the file opens successfully.

🧠 Conceptual
intermediate
1:00remaining
Which pin is commonly used as chip select for SD cards on Arduino Uno?

When connecting an SD card module to an Arduino Uno, which pin is typically used as the chip select (CS) pin?

APin A0
BPin 13
CPin 10
DPin 2
Attempts:
2 left
💡 Hint

Look for the pin that SPI devices commonly use as chip select on Arduino Uno.

🔧 Debug
advanced
2:00remaining
Why does this code fail to write to the SD card?

Examine the code below. It compiles but does not write data to the SD card. What is the most likely reason?

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

  const int chipSelect = 10;

  void setup() {
    Serial.begin(9600);
    if (!SD.begin()) {
      Serial.println("SD init failed");
      return;
    }

    File file = SD.open("data.txt", FILE_WRITE);
    if (file) {
      file.println("Hello");
      file.close();
      Serial.println("Data written");
    } else {
      Serial.println("Failed to open file");
    }
  }

  void loop() {}
AThe file name is invalid
BFile is not closed after writing
CSerial.begin() is missing
DSD.begin() is called without chip select pin, so initialization fails
Attempts:
2 left
💡 Hint

Check the SD.begin() function call and its parameters.

📝 Syntax
advanced
1:30remaining
Which option causes a compilation error?

Which of the following code snippets will cause a compilation error when trying to write "Data" to a file on the SD card?

AFile file = SD.open("file.txt", WRITE); file.println("Data"); file.close();
BFile file = SD.open("file.txt", FILE_WRITE); file.print("Data"); file.close();
CFile file = SD.open("file.txt", FILE_WRITE); file.println("Data"); file.close();
DFile file = SD.open("file.txt", FILE_WRITE); file.write("Data"); file.close();
Attempts:
2 left
💡 Hint

Check the constants used for file modes in the SD library.

🚀 Application
expert
2:30remaining
How many lines will be in the file after running this code?

Given the code below runs on an Arduino with a working SD card, how many lines will the file "log.txt" contain after setup() finishes?

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

  const int chipSelect = 10;

  void setup() {
    Serial.begin(9600);
    while (!Serial) {}

    if (!SD.begin(chipSelect)) {
      Serial.println("SD init failed");
      return;
    }

    for (int i = 0; i < 3; i++) {
      File file = SD.open("log.txt", FILE_WRITE);
      if (file) {
        file.println(i);
        file.close();
      }
    }
  }

  void loop() {}
A3
B1
C0
D6
Attempts:
2 left
💡 Hint

Consider how many times the file is opened and written to in the loop.

Practice

(1/5)
1. What is the correct way to open a file for writing data on an SD card in Arduino?
easy
A. Use SD.close(filename)
B. Use SD.read(filename)
C. Use SD.open(filename, FILE_WRITE)
D. Use SD.format()

Solution

  1. Step 1: Understand file opening modes

    To write data, the file must be opened in write mode, which is done using FILE_WRITE.
  2. Step 2: Identify the correct function call

    The function SD.open(filename, FILE_WRITE) opens the file for writing. Other options do not open files for writing.
  3. Final Answer:

    Use SD.open(filename, FILE_WRITE) -> Option C
  4. Quick Check:

    Open file for writing = SD.open(filename, FILE_WRITE) [OK]
Hint: Use FILE_WRITE with SD.open() to write files [OK]
Common Mistakes:
  • Using SD.read() to open file for writing
  • Forgetting FILE_WRITE mode
  • Trying to write without opening file
2. Which of the following is the correct syntax to write the string "Hello" followed by a newline to a file named "log.txt" on the SD card?
easy
A. file.write("Hello");
B. file.println("Hello");
C. file.print("Hello");
D. file.read("Hello");

Solution

  1. Step 1: Identify the write functions

    To write text with a newline, file.println() is used. file.print() writes without newline, file.write() writes bytes.
  2. Step 2: Choose the function that writes string with newline

    file.println("Hello") writes "Hello" followed by a newline, which is common for logging.
  3. Final Answer:

    file.println("Hello"); -> Option B
  4. Quick Check:

    Write string with newline = file.println() [OK]
Hint: Use file.println() to write string with newline [OK]
Common Mistakes:
  • Using file.read() to write data
  • Confusing file.write() with file.print()
  • Forgetting to add newline if needed
3. What will be the output on the SD card file after running this code snippet?
File file = SD.open("data.txt", FILE_WRITE);
if (file) {
  file.println("Line 1");
  file.println("Line 2");
  file.close();
}
medium
A. No output because file.close() is missing
B. Line 1\nLine 2\n
C. Line 1Line 2
D. Line 1 Line 2

Solution

  1. Step 1: Understand file writing with println()

    Each call to file.println() writes the string plus a newline character.
  2. Step 2: Check file closing

    The file is closed properly with file.close(), so data is saved.
  3. Final Answer:

    Line 1 Line 2 -> Option D
  4. Quick Check:

    file.println() adds newline, file.close() saves data [OK]
Hint: file.println() adds newline; always close file to save [OK]
Common Mistakes:
  • Thinking file.close() is missing
  • Assuming println() does not add newline
  • Confusing escape sequences with actual newlines
4. Identify the error in this Arduino code snippet for writing to an SD card:
if (!SD.begin()) {
  Serial.println("Initialization failed!");
}
File file = SD.open("test.txt", FILE_WRITE);
file.println("Data");
// Missing file.close()
medium
A. File is not closed after writing, risking data loss
B. Serial.println() cannot be used with SD cards
C. FILE_WRITE is not a valid mode for SD.open()
D. SD.begin() should be called after opening the file

Solution

  1. Step 1: Check SD initialization

    SD.begin() is correctly called before opening the file.
  2. Step 2: Identify missing file.close()

    The file is opened and written to, but not closed, which can cause data not to be saved properly.
  3. Final Answer:

    File is not closed after writing, risking data loss -> Option A
  4. Quick Check:

    Always close file after writing to save data [OK]
Hint: Always close files after writing to save data [OK]
Common Mistakes:
  • Forgetting to close the file
  • Calling SD.begin() after opening file
  • Misunderstanding FILE_WRITE mode
5. You want to write multiple sensor readings to a file on the SD card, but only if the SD card initializes successfully. Which of the following code snippets correctly implements this logic?
hard
A. if (SD.begin()) { File dataFile = SD.open("readings.txt", FILE_WRITE); if (dataFile) { dataFile.println(sensorValue); dataFile.close(); } } else { Serial.println("SD init failed"); }
B. File dataFile = SD.open("readings.txt", FILE_WRITE); if (dataFile) { if (SD.begin()) { dataFile.println(sensorValue); } dataFile.close(); }
C. if (!SD.begin()) { Serial.println("SD init failed"); } else { File dataFile = SD.open("readings.txt"); dataFile.println(sensorValue); dataFile.close(); }
D. SD.begin(); File dataFile = SD.open("readings.txt", FILE_WRITE); dataFile.println(sensorValue); dataFile.close();

Solution

  1. Step 1: Check SD initialization before file operations

    The correct code first calls and checks SD.begin() before attempting to open the file.
  2. Step 2: Proper file open, write, close, and error handling

    Open with SD.open("readings.txt", FILE_WRITE), verify with if (dataFile), write dataFile.println(sensorValue), dataFile.close(), and print error if init fails.
  3. Final Answer:

    if (SD.begin()) { File dataFile = SD.open("readings.txt", FILE_WRITE); if (dataFile) { dataFile.println(sensorValue); dataFile.close(); } } else { Serial.println("SD init failed"); } -> Option A
  4. Quick Check:

    Check SD.begin() first, then open, write, close file [OK]
Hint: Always check SD.begin() before file operations [OK]
Common Mistakes:
  • Opening file before SD.begin()
  • Not closing file after writing
  • Ignoring SD initialization failure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard