Reading data from SD card in Arduino - Time & Space Complexity
When reading data from an SD card, it is important to understand how the time taken grows as the amount of data increases.
We want to know how the program's running time changes when reading more data.
Analyze the time complexity of the following code snippet.
#include <SD.h>
File dataFile;
void setup() {
SD.begin(10);
dataFile = SD.open("data.txt");
}
void loop() {
while (dataFile.available()) {
char c = dataFile.read();
}
dataFile.close();
while(true) {}
}
This code opens a file on the SD card and reads it character by character until the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each character from the file inside the while loop.
- How many times: Once for every character in the file until no more data is available.
As the file size grows, the number of read operations grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 characters | About 10 reads |
| 100 characters | About 100 reads |
| 1000 characters | About 1000 reads |
Pattern observation: The time grows in a straight line as the file size increases.
Time Complexity: O(n)
This means the time to read the file grows directly with the number of characters in the file.
[X] Wrong: "Reading from the SD card takes the same time no matter how big the file is."
[OK] Correct: Each character must be read one by one, so more data means more time spent reading.
Understanding how reading data scales helps you write efficient code for devices with limited speed and memory.
"What if we read the file in larger chunks instead of one character at a time? How would the time complexity change?"
Practice
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 AQuick Check:
Open file for reading = SD.open(filename, FILE_READ) [OK]
- Using SD.read() to open files
- Forgetting FILE_READ mode
- Using SD.begin() to open files
- Using SD.load() which doesn't exist
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 CQuick Check:
Check file open with if (file) [OK]
- Comparing file to NULL (not Arduino style)
- Using file.isOpen() which doesn't exist
- Using file.exists() which checks SD, not file open
File file = SD.open("data.txt", FILE_READ);
if (file) {
char c = file.read();
Serial.println(c);
file.close();
} else {
Serial.println("Error opening file");
}Solution
Step 1: Understand file.read() returns a byte
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 AQuick Check:
file.read() returns char 'H' printed as H [OK]
- Thinking read() returns whole string
- Expecting ASCII number printed
- Confusing file.read() with readStringUntil()
File file = SD.open("log.txt", FILE_READ);
if (file) {
String line = file.readStringUntil('\n');
Serial.println(line);
}
file.close();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 BQuick Check:
Close file only if opened [OK]
- Calling file.close() unconditionally
- Assuming readStringUntil() is invalid
- Forgetting SD.begin() but not shown here
- Confusing single vs double quotes in char argument
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 DQuick Check:
Loop with file.available() to read lines [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
