Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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()
✗ Incorrect
The SD library uses SD.begin() to initialize the card with the chip select pin.
2fill in blank
mediumComplete 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'
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
✗ Incorrect
The SD library uses SD.open() to open files. FILE_WRITE mode opens the file for writing.
3fill in blank
hardFix 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'
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
✗ Incorrect
The File object uses print() to write text data to the file.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using print() for numbers without conversion
Forgetting to close the file
Using flush() instead of close()
✗ Incorrect
Use write() to write the number as bytes and close() to close the file.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using print() without newlines
Using write() which writes bytes
Forgetting to close the file
✗ Incorrect
Use println() to write lines with newlines and close() to close the file.