📖 Scenario: You have a small Arduino project that needs to read text data stored on an SD card. This is common in projects like data loggers or music players where the Arduino reads files from the SD card.
🎯 Goal: Build a simple Arduino program that reads the contents of a text file called data.txt from an SD card and prints it to the Serial Monitor.
📋 What You'll Learn
Use the SD library to access the SD card
Initialize the SD card on pin 10
Open the file data.txt for reading
Read the file content line by line
Print each line to the Serial Monitor
💡 Why This Matters
🌍 Real World
Reading data from an SD card is useful in projects like data loggers, audio players, or any device that needs to access stored files.
💼 Career
Understanding how to interface with SD cards is important for embedded systems developers and IoT engineers working with external storage.
Progress0 / 4 steps
1
Set up SD card and Serial communication
Write code to include the SD and SPI libraries, create a File variable called myFile, and start Serial communication at 9600 baud inside setup(). Also, initialize the SD card on pin 10 using SD.begin(10) and print "SD card initialized." if successful.
Arduino
Hint
Remember to include both SD.h and SPI.h libraries. Use SD.begin(10) to start the SD card on pin 10.
2
Open the file for reading
Inside setup(), after initializing the SD card, open the file data.txt for reading using myFile = SD.open("data.txt"). Then check if myFile is valid (opened successfully) and print "File opened successfully." if it is.
Arduino
Hint
Use SD.open("data.txt") to open the file. Check if myFile is true to confirm it opened.
3
Read the file content line by line
Inside setup(), after opening the file, use a while loop with myFile.available() to read each character from the file. Use myFile.read() to get each character and print it to Serial using Serial.write().
Arduino
Hint
Use while (myFile.available()) to loop through the file. Read each character with myFile.read() and print it with Serial.write(). Don't forget to close the file.
4
Print the file content to Serial Monitor
Add a Serial.println("End of file."); statement after closing the file to indicate the file reading is complete.
Arduino
Hint
Print "End of file." after closing the file to show reading is done.
Practice
(1/5)
1. What is the correct function to open a file for reading from an SD card in Arduino?
easy
A. SD.open(filename, FILE_READ)
B. SD.read(filename)
C. SD.begin(filename)
D. SD.load(filename)
Solution
Step 1: Identify the function to open files on SD card
The Arduino SD library uses SD.open() to open files.
Step 2: Specify the mode for reading
To read a file, the mode FILE_READ must be passed as the second argument.
Final Answer:
SD.open(filename, FILE_READ) -> Option A
Quick Check:
Open file for reading = SD.open(filename, FILE_READ) [OK]
Hint: Use SD.open() with FILE_READ to open files for reading [OK]
Common Mistakes:
Using SD.read() to open files
Forgetting FILE_READ mode
Using SD.begin() to open files
Using SD.load() which doesn't exist
2. Which of the following is the correct syntax to check if a file opened successfully before reading?
easy
A. if (file != NULL) { /* read file */ }
B. if (file.isOpen()) { /* read file */ }
C. if (file) { /* read file */ }
D. if (file.exists()) { /* read file */ }
Solution
Step 1: Understand file object truthiness in Arduino
In Arduino, File objects can be checked directly in if statements to see if they opened successfully.
Step 2: Recognize correct syntax
Using if (file) is the standard way to check if the file opened.
Final Answer:
if (file) { /* read file */ } -> Option C
Quick Check:
Check file open with if (file) [OK]
Hint: Use if (file) to check if file opened successfully [OK]
Common Mistakes:
Comparing file to NULL (not Arduino style)
Using file.isOpen() which doesn't exist
Using file.exists() which checks SD, not file open
3. What will be the output of this Arduino code snippet reading from an SD card file named "data.txt" containing the text "Hello"?
The read() method returns the ASCII value of the first character, but when assigned to a char and printed with Serial.println(char), it prints the character itself.
Step 2: Check code carefully
Since c is a char and Serial.println(c) prints the character, the output is 'H'.
Final Answer:
H -> Option A
Quick Check:
file.read() returns char 'H' printed as H [OK]
Hint: file.read() returns one char; Serial.println prints char [OK]
Common Mistakes:
Thinking read() returns whole string
Expecting ASCII number printed
Confusing file.read() with readStringUntil()
4. Identify the error in this Arduino code snippet that reads a line from an SD card file:
File file = SD.open("log.txt", FILE_READ);
if (file) {
String line = file.readStringUntil('\n');
Serial.println(line);
}
file.close();
medium
A. readStringUntil() is not a valid File method
B. file.close() is called outside the if block
C. Missing SD.begin() before opening file
D. Using single quotes instead of double quotes in readStringUntil
Solution
Step 1: Check file.close() placement
file.close() should be called only if file opened successfully, so it must be inside the if block.
Step 2: Understand consequences
If file failed to open, calling file.close() causes error because file is invalid.
Final Answer:
file.close() is called outside the if block -> Option B
Quick Check:
Close file only if opened [OK]
Hint: Call file.close() only inside if (file) block [OK]
Common Mistakes:
Calling file.close() unconditionally
Assuming readStringUntil() is invalid
Forgetting SD.begin() but not shown here
Confusing single vs double quotes in char argument
5. You want to read all lines from a file "config.txt" on an SD card and print each line to Serial. Which code snippet correctly does this?
hard
A. File file = SD.open("config.txt", FILE_READ);
while (file) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
B. File file = SD.open("config.txt", FILE_READ);
while (!file.available()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
C. File file = SD.open("config.txt", FILE_READ);
while (file.read()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
D. File file = SD.open("config.txt", FILE_READ);
while (file.available()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
Solution
Step 1: Use file.available() to check for data
file.available() returns true if there is data left to read, so loop while available() is true.
Step 2: Read each line with readStringUntil('\n')
This method reads until newline, perfect for line-by-line reading.
Step 3: Close file after reading
Call file.close() after finishing reading to free resources.
Final Answer:
Use while (file.available()) with readStringUntil('\n') -> Option D
Quick Check:
Loop with file.available() to read lines [OK]
Hint: Loop while file.available() to read all lines [OK]
Common Mistakes:
Using while (!file.available()) which loops incorrectly
Using while (file.read()) which reads bytes, not lines
Using while (file) which doesn't check data availability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.