Bird
0
0
Arduinoprogramming~5 mins

OLED display with I2C (SSD1306) in Arduino - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: OLED display with I2C (SSD1306)
O(n)
Understanding Time Complexity

When working with an OLED display using I2C, it's important to understand how the time to update the screen changes as the amount of data grows.

We want to know how the program's running time changes when we send more pixels or text to the display.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#include <Wire.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, <TwoWire> &Wire);

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  for (int i = 0; i < 10; i++) {
    display.setCursor(0, i * 8);
    display.print("Line ");
    display.print(i);
  }
  display.display();
}

void loop() {}
    

This code writes 10 lines of text on the OLED screen using I2C communication and then updates the display once.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop that writes 10 lines of text to the display buffer.
  • How many times: The loop runs exactly 10 times, once for each line.
How Execution Grows With Input

As the number of lines (n) increases, the time to prepare the text grows roughly in a straight line.

Input Size (n)Approx. Operations
1010 text writes + 1 display update
100100 text writes + 1 display update
10001000 text writes + 1 display update

Pattern observation: The time grows linearly with the number of lines because each line is processed once before updating the display.

Final Time Complexity

Time Complexity: O(n)

This means the time to prepare and write text grows directly in proportion to the number of lines you want to display.

Common Mistake

[X] Wrong: "Updating the display takes the same time no matter how many lines I write."

[OK] Correct: Actually, the time to prepare the display buffer grows with the number of lines, so more lines mean more work before the update.

Interview Connect

Understanding how display updates scale helps you write efficient code for devices with limited speed and memory, a useful skill in many embedded projects.

Self-Check

"What if we called display.display() inside the loop for each line? How would the time complexity change?"