Writing data to SD card in Arduino - Time & Space Complexity
When writing data to an SD card using Arduino, it's important to understand how the time taken grows as you write more data.
We want to know how the program's running time changes when the amount of data increases.
Analyze the time complexity of the following code snippet.
#include <SD.h>
File dataFile;
void setup() {
SD.begin(10);
dataFile = SD.open("data.txt", FILE_WRITE);
for (int i = 0; i < 100; i++) {
dataFile.println(i);
}
dataFile.close();
}
void loop() {}
This code opens a file on the SD card and writes 100 lines of data, then closes the file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing a line to the SD card inside the for loop.
- How many times: The loop runs 100 times, writing 100 lines.
As the number of lines to write increases, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes to SD card |
| 100 | 100 writes to SD card |
| 1000 | 1000 writes to SD card |
Pattern observation: Doubling the number of lines roughly doubles the time taken.
Time Complexity: O(n)
This means the time to write data grows linearly with the number of lines you write.
[X] Wrong: "Writing to the SD card happens instantly, so time does not increase with more data."
[OK] Correct: Each write takes time because the SD card needs to physically save data, so more lines mean more time.
Understanding how writing data scales helps you design efficient programs that handle storage well, a useful skill in many real projects.
"What if we buffered multiple lines in memory and wrote them all at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand file opening modes
To write data, the file must be opened in write mode, which is done usingFILE_WRITE.Step 2: Identify the correct function call
The functionSD.open(filename, FILE_WRITE)opens the file for writing. Other options do not open files for writing.Final Answer:
Use SD.open(filename, FILE_WRITE) -> Option CQuick Check:
Open file for writing = SD.open(filename, FILE_WRITE) [OK]
- Using SD.read() to open file for writing
- Forgetting FILE_WRITE mode
- Trying to write without opening file
Solution
Step 1: Identify the write functions
To write text with a newline,file.println()is used.file.print()writes without newline,file.write()writes bytes.Step 2: Choose the function that writes string with newline
file.println("Hello")writes "Hello" followed by a newline, which is common for logging.Final Answer:
file.println("Hello"); -> Option BQuick Check:
Write string with newline = file.println() [OK]
- Using file.read() to write data
- Confusing file.write() with file.print()
- Forgetting to add newline if needed
File file = SD.open("data.txt", FILE_WRITE);
if (file) {
file.println("Line 1");
file.println("Line 2");
file.close();
}Solution
Step 1: Understand file writing with println()
Each call tofile.println()writes the string plus a newline character.Step 2: Check file closing
The file is closed properly withfile.close(), so data is saved.Final Answer:
Line 1 Line 2 -> Option DQuick Check:
file.println() adds newline, file.close() saves data [OK]
- Thinking file.close() is missing
- Assuming println() does not add newline
- Confusing escape sequences with actual newlines
if (!SD.begin()) {
Serial.println("Initialization failed!");
}
File file = SD.open("test.txt", FILE_WRITE);
file.println("Data");
// Missing file.close()Solution
Step 1: Check SD initialization
SD.begin() is correctly called before opening the file.Step 2: Identify missing file.close()
The file is opened and written to, but not closed, which can cause data not to be saved properly.Final Answer:
File is not closed after writing, risking data loss -> Option AQuick Check:
Always close file after writing to save data [OK]
- Forgetting to close the file
- Calling SD.begin() after opening file
- Misunderstanding FILE_WRITE mode
Solution
Step 1: Check SD initialization before file operations
The correct code first calls and checksSD.begin()before attempting to open the file.Step 2: Proper file open, write, close, and error handling
Open withSD.open("readings.txt", FILE_WRITE), verify withif (dataFile), writedataFile.println(sensorValue),dataFile.close(), and print error if init fails.Final Answer:
if (SD.begin()) { File dataFile = SD.open("readings.txt", FILE_WRITE); if (dataFile) { dataFile.println(sensorValue); dataFile.close(); } } else { Serial.println("SD init failed"); } -> Option AQuick Check:
Check SD.begin() first, then open, write, close file [OK]
- Opening file before SD.begin()
- Not closing file after writing
- Ignoring SD initialization failure
Ready to prove it?
Pass the test and this pattern moves unknown
