0
0
Arduinoprogramming~10 mins

Writing data to SD card in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the SD card.

Arduino
#include <SD.h>
const int chipSelect = 4;

void setup() {
  Serial.begin(9600);
  if (!SD.[1](chipSelect)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");
}

void loop() {}
Drag options to blanks, or click blank then click option'
Abegin
Binit
Cstart
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using SD.init() instead of SD.begin()
Forgetting to pass the chip select pin
Calling the function outside setup()
2fill in blank
medium

Complete the code to open a file named "data.txt" for writing.

Arduino
File dataFile = SD.[1]("data.txt", FILE_WRITE);
Drag options to blanks, or click blank then click option'
Awrite
Bcreate
Cread
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using SD.read() which is for reading files
Using SD.write() which is for writing data to files, not opening
Using SD.create() which does not exist
3fill in blank
hard

Fix the error in the code to write "Hello" to the file.

Arduino
if (dataFile) {
  dataFile.[1]("Hello");
  dataFile.close();
} else {
  Serial.println("Error opening file");
}
Drag options to blanks, or click blank then click option'
Aopen
Bprint
Cwrite
Dprintln
Attempts:
3 left
💡 Hint
Common Mistakes
Using write() which writes bytes, not strings
Using println() which adds a newline
Using open() which is for opening files
4fill in blank
hard

Fill both blanks to write a number and close the file properly.

Arduino
File dataFile = SD.open("numbers.txt", FILE_WRITE);
if (dataFile) {
  dataFile.[1](123);
  dataFile.[2]();
} else {
  Serial.println("Failed to open file");
}
Drag options to blanks, or click blank then click option'
Aprint
Bclose
Cwrite
Dflush
Attempts:
3 left
💡 Hint
Common Mistakes
Using print() for numbers without conversion
Forgetting to close the file
Using flush() instead of close()
5fill in blank
hard

Fill all three blanks to write multiple lines and close the file.

Arduino
File dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile) {
  dataFile.[1]("Line 1");
  dataFile.[2]("Line 2");
  dataFile.[3]();
} else {
  Serial.println("Cannot open file");
}
Drag options to blanks, or click blank then click option'
Aprint
Bprintln
Cclose
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using print() without newlines
Using write() which writes bytes
Forgetting to close the file