0
0
Arduinoprogramming~20 mins

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

Choose your learning style9 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.