Reducing power consumption tips in Arduino - Time & Space Complexity
When we write code to save power on an Arduino, we want to know how much work the code does as it runs.
We ask: how does the time the code takes change when we add more steps or checks?
Analyze the time complexity of the following code snippet.
void loop() {
delay(1000); // wait 1 second
digitalWrite(LED_BUILTIN, HIGH); // turn LED on
delay(100); // short on time
digitalWrite(LED_BUILTIN, LOW); // turn LED off
delay(1000); // wait 1 second
}
This code blinks an LED with delays to save power by turning the LED off most of the time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop function repeats forever.
- How many times: Each loop cycle runs once, but inside it, delay pauses the program.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cycles of loop, each with fixed delays |
| 100 | 100 cycles, same fixed delays each time |
| 1000 | 1000 cycles, delays do not change |
Pattern observation: The time spent grows linearly with the number of cycles, but each cycle takes the same fixed time.
Time Complexity: O(n)
This means the total time grows directly with how many times the loop runs, but each loop takes a fixed amount of time.
[X] Wrong: "Adding more delay calls makes the program faster."
[OK] Correct: Delay pauses the program, so more delays actually make it slower, not faster.
Understanding how your code's timing grows helps you write better programs that save power and run efficiently on small devices like Arduino.
"What if we replaced delay() with a non-blocking timer? How would the time complexity change?"
Practice
Solution
Step 1: Understand power saving basics
Sleep modes reduce power by pausing the CPU when it is not needed.Step 2: Evaluate other options
Increasing clock speed or keeping hardware enabled increases power use; running without power is impossible.Final Answer:
Using sleep modes to pause the CPU when idle -> Option CQuick Check:
Sleep modes = power saving [OK]
- Thinking higher clock speed saves power
- Believing all hardware should stay enabled
- Ignoring the need for a power source
Solution
Step 1: Identify sleep mode function
The functionsleep_mode()is used to put the microcontroller into sleep mode.Step 2: Check other functions
digitalWrite()controls pins,delay()pauses code,pinMode()sets pin mode; none put it into sleep.Final Answer:
sleep_mode() -> Option DQuick Check:
sleep_mode() = enter sleep [OK]
- Confusing delay() with sleep functions
- Using digitalWrite() to control power
- Not knowing pinMode() sets pin direction
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// No sleep mode used
}
void loop() {
// Empty loop
}Solution
Step 1: Analyze pin state
Pin 13 is set to LOW, so it does not consume extra power from that pin.Step 2: Check for sleep mode
No sleep mode is used, so the CPU runs continuously, consuming normal power.Final Answer:
Power consumption remains high because no sleep mode is used -> Option AQuick Check:
No sleep mode = normal power use [OK]
- Assuming LOW pin means zero power
- Thinking empty loop stops CPU
- Believing pin 13 blinks automatically
void setup() {
sleep_enable();
sleep_mode();
sleep_disable();
}
void loop() {
// do nothing
}Solution
Step 1: Review sleep sequence
sleep_enable() and sleep_mode() are correctly called to enter sleep.Step 2: Check wake-up mechanism
Without an interrupt or event to wake the CPU, it may stay asleep indefinitely or behave unexpectedly.Final Answer:
The code does not put the CPU to sleep properly because it lacks an interrupt to wake up -> Option AQuick Check:
Sleep needs wake interrupt [OK]
- Thinking sleep_disable() must come before sleep_mode()
- Ignoring the need for wake interrupts
- Assuming pinMode() affects sleep functions
Solution
Step 1: Identify hardware disabling functions
Functions likepower_adc_disable()turn off specific hardware modules to save power.Step 2: Evaluate other options
Increasing clock speed uses more power; keeping all modules enabled wastes energy; digitalWrite() controls pins, not modules.Final Answer:
Use power_adc_disable() to turn off the analog-to-digital converter when not needed -> Option BQuick Check:
Disable unused hardware = power_adc_disable() [OK]
- Thinking digitalWrite() disables hardware modules
- Believing higher clock speed saves power
- Not disabling unused modules wastes battery
Ready to prove it?
Pass the test and this pattern moves unknown
