Uploading and running a sketch in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we upload and run a sketch on an Arduino, we want to know how long it takes as the sketch size or complexity grows.
We ask: How does the time to upload and run change when the sketch gets bigger or more complex?
Analyze the time complexity of the following code snippet.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
This sketch turns the built-in LED on and off every second repeatedly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop() function runs forever, repeating the LED on/off commands.
- How many times: It repeats endlessly, cycling through turning the LED on and off with delays.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 seconds running | About 5 on/off cycles |
| 100 seconds running | About 50 on/off cycles |
| 1000 seconds running | About 500 on/off cycles |
Pattern observation: The number of operations grows linearly with time running, repeating the same steps over and over.
Time Complexity: O(n)
This means the time to run grows directly in proportion to how long the sketch runs, repeating the same steps each cycle.
[X] Wrong: "The sketch runs instantly and does not take more time if it runs longer."
[OK] Correct: The sketch repeats actions continuously, so running longer means more operations and more time spent.
Understanding how repeated actions affect running time helps you explain how programs behave over time, a useful skill for many programming tasks.
What if we added a nested loop inside loop() that blinks the LED multiple times each cycle? How would the time complexity change?
Practice
Solution
Step 1: Understand the upload process
Uploading sends the program to the Arduino's memory.Step 2: Recognize sketch behavior after upload
The Arduino automatically runs the sketch once upload finishes.Final Answer:
The sketch starts running automatically on the board. -> Option CQuick Check:
Upload triggers automatic run [OK]
- Thinking you must press reset to start sketch
- Believing sketch waits for manual start
- Confusing upload with download
Solution
Step 1: Identify correct upload procedure
You must select the correct board and port in the IDE.Step 2: Perform upload action
Clicking the Upload button sends the sketch to the board.Final Answer:
Select the board and port, then click the Upload button. -> Option DQuick Check:
Board + port selected, then upload [OK]
- Skipping board or port selection
- Trying to upload without connecting board
- Using serial monitor to upload code
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}What will happen after uploading and running this sketch?
Solution
Step 1: Analyze setup function
Pin 13 is set as an output pin.Step 2: Analyze loop function behavior
The LED on pin 13 turns HIGH (on), waits 1 second, then LOW (off), waits 1 second, repeating.Final Answer:
The built-in LED on pin 13 will blink on and off every second. -> Option AQuick Check:
Pin 13 output toggles every 1s [OK]
- Confusing delay units (milliseconds vs seconds)
- Assuming LED stays on without blinking
- Thinking loop is empty
Solution
Step 1: Understand the error message
This error means the IDE cannot communicate with the board.Step 2: Identify common communication issues
Usually caused by wrong board or port selection or bad connection.Final Answer:
The wrong board or port is selected in the Arduino IDE. -> Option BQuick Check:
Communication error = wrong board/port selected [OK]
- Assuming syntax errors cause communication errors
- Ignoring USB cable connection
- Thinking IDE installation causes this error
Solution
Step 1: Identify the correct COM port
Disconnect Arduino and note available ports, then reconnect and see which port appears.Step 2: Select the newly appeared port in the IDE
This port corresponds to the Arduino board for uploading.Final Answer:
Disconnect the Arduino, check ports, reconnect it, then select the new port shown. -> Option AQuick Check:
New port after reconnect = correct port [OK]
- Guessing COM port without checking
- Assuming IDE auto-selects port
- Uploading without port selection
