0
0
Arduinoprogramming~20 mins

Reading data from SD card in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SD Card Reading 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 SD card read code?

Consider this Arduino code snippet that reads a line from a file on the SD card and prints it:

File dataFile = SD.open("test.txt");
if (dataFile) {
  while (dataFile.available()) {
    Serial.write(dataFile.read());
  }
  dataFile.close();
} else {
  Serial.println("Error opening file");
}

If the file test.txt contains the text Hello SD Card!, what will be printed on the Serial Monitor?

Arduino
File dataFile = SD.open("test.txt");
if (dataFile) {
  while (dataFile.available()) {
    Serial.write(dataFile.read());
  }
  dataFile.close();
} else {
  Serial.println("Error opening file");
}
AHello SD Card!\n
BError opening file
CHello SD Card!
DHELLO SD CARD!
Attempts:
2 left
💡 Hint

Think about what Serial.write() does compared to Serial.println().

🧠 Conceptual
intermediate
1:00remaining
Which Arduino library is required to read from an SD card?

When programming an Arduino to read data from an SD card, which library must you include to access SD card functions?

A#include <SD.h>
B#include <Wire.h>
C#include <SPI.h>
D#include <EEPROM.h>
Attempts:
2 left
💡 Hint

Think about the library that provides file system access on SD cards.

🔧 Debug
advanced
2:30remaining
Why does this SD card read code fail to open the file?

Look at this Arduino code snippet:

if (!SD.begin(4)) {
  Serial.println("Initialization failed!");
  return;
}
File myFile = SD.open("data.txt");
if (!myFile) {
  Serial.println("Error opening file");
}

The SD card is connected correctly, but the output is always "Error opening file". What is the most likely cause?

Arduino
if (!SD.begin(4)) {
  Serial.println("Initialization failed!");
  return;
}
File myFile = SD.open("data.txt");
if (!myFile) {
  Serial.println("Error opening file");
}
AThe SD.open() function requires a second argument specifying read mode
BThe file name "data.txt" is too long for SD cards
CThe SD card is not formatted as FAT32
DThe chip select pin number 4 is incorrect for the hardware setup
Attempts:
2 left
💡 Hint

Check the hardware connection and pin numbers.

📝 Syntax
advanced
2:30remaining
Which option correctly reads the first 10 bytes from an SD card file?

Choose the code snippet that correctly reads and prints the first 10 bytes from a file named "log.txt" on an SD card.

A
File file = SD.open("log.txt");
for (int i = 0; i < 10; i++) {
  if (file.available()) {
    Serial.print((char)file.read());
  }
}
file.close();
B
File file = SD.open("log.txt");
for (int i = 0; i <= 10; i++) {
  Serial.print(file.read());
}
file.close();
C
File file = SD.open("log.txt");
int count = 0;
while (count < 10) {
  Serial.print(file.read());
  count++;
}
file.close();
D
File file = SD.open("log.txt");
for (int i = 0; i < 10; i++) {
  Serial.print(file.read());
}
file.close();
Attempts:
2 left
💡 Hint

Consider checking if data is available before reading.

🚀 Application
expert
3:00remaining
How many bytes are read and printed by this code?

Analyze this Arduino code reading from an SD card file:

File file = SD.open("data.bin");
int bytesRead = 0;
while (file.available() && bytesRead < 15) {
  char c = file.read();
  if (c == '\n') {
    break;
  }
  Serial.print(c);
  bytesRead++;
}
file.close();
Serial.println(bytesRead);

If the file data.bin contains the text 12345\n67890abcdef, what will be the final number printed by Serial.println(bytesRead);?

Arduino
File file = SD.open("data.bin");
int bytesRead = 0;
while (file.available() && bytesRead < 15) {
  char c = file.read();
  if (c == '\n') {
    break;
  }
  Serial.print(c);
  bytesRead++;
}
file.close();
Serial.println(bytesRead);
A10
B5
C15
D0
Attempts:
2 left
💡 Hint

Count characters until newline or 15 bytes, whichever comes first.