Why data logging matters in Arduino - Performance Analysis
When we log data on an Arduino, we want to know how long it takes as we collect more data points.
We ask: How does the time to log data grow as we record more entries?
Analyze the time complexity of the following code snippet.
void logData(int data[], int size) {
for (int i = 0; i < size; i++) {
Serial.println(data[i]);
}
}
This code sends each data point from an array to the serial monitor one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the data array to print each item.
- How many times: Exactly once for each data point in the array.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print commands |
| 100 | 100 print commands |
| 1000 | 1000 print commands |
Pattern observation: The number of operations grows directly with the number of data points.
Time Complexity: O(n)
This means the time to log data grows in a straight line as you add more data points.
[X] Wrong: "Logging a few more data points won't affect the time much."
[OK] Correct: Each extra data point adds more work, so time increases steadily, not stays the same.
Understanding how data logging time grows helps you write efficient code for real devices that collect lots of information.
"What if we buffered data and sent it all at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of data logging
Data logging records data over time for later analysis.Step 2: Identify benefits of data logging
It helps find patterns and troubleshoot issues by saving information.Final Answer:
It saves information to review and find problems later -> Option AQuick Check:
Data logging = Save info for review [OK]
- Thinking data logging speeds up Arduino
- Confusing data logging with power saving
- Believing data logging changes hardware
Solution
Step 1: Identify commands related to data output
Serial.print() sends data to the serial monitor for viewing or logging.Step 2: Exclude unrelated commands
digitalWrite() controls pins, pinMode() sets pin mode, delay() pauses code.Final Answer:
Serial.print() -> Option AQuick Check:
Send data to serial = Serial.print() [OK]
- Using digitalWrite() to send data
- Confusing pinMode() with data output
- Thinking delay() logs data
void setup() {
Serial.begin(9600);
Serial.print("Temp: ");
Serial.print(25);
Serial.println(" C");
}
void loop() {}Solution
Step 1: Analyze Serial.print() and Serial.println() usage
Serial.print("Temp: ") prints 'Temp: ' without newline. Serial.print(25) prints '25' next to it. Serial.println(" C") prints ' C' and adds newline.Step 2: Combine printed parts
Combined output is 'Temp: 25 C' followed by a new line.Final Answer:
Temp: 25 C -> Option BQuick Check:
Serial.print parts combined = Temp: 25 C [OK]
- Ignoring spaces in strings
- Confusing Serial.print() and Serial.println() effects
- Missing spaces between printed parts
#include <SD.h>
File dataFile;
void setup() {
SD.begin(4);
dataFile = SD.open("log.txt", FILE_WRITE);
dataFile.println("Start logging");
dataFile.close();
}
void loop() {}Solution
Step 1: Review SD card initialization
SD.begin() returns true if SD card initializes correctly; this should be checked to avoid errors.Step 2: Check other code parts
File object can be global, SD.open() supports FILE_WRITE, dataFile.println() is valid.Final Answer:
SD.begin() should check if initialization succeeded -> Option CQuick Check:
Always verify SD.begin() success [OK]
- Ignoring SD.begin() return value
- Thinking File must be local to loop()
- Believing SD.open() can't write files
- Assuming dataFile.println() is invalid
Solution
Step 1: Understand data safety on power loss
Data saved to SD card only after file is closed or flushed; keeping file open risks data loss.Step 2: Evaluate options for safe logging
Opening, writing, and closing file each time ensures data is saved immediately. Other options risk losing data if power fails.Final Answer:
Open the file, write data, and close the file each time in loop() -> Option DQuick Check:
Close file after each write to save data safely [OK]
- Keeping file open all time risking data loss
- Logging only to Serial without saving
- Delaying writes causing big data loss on failure
Ready to prove it?
Pass the test and this pattern moves unknown
