Bird
0
0
Raspberry Piprogramming~5 mins

Displaying sensor readings on OLED in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Displaying sensor readings on OLED
O(n)
Understanding Time Complexity

When showing sensor readings on an OLED screen, the time it takes to update depends on how many readings we process and display.

We want to know how the time to update grows as we show more sensor data.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


import board
import adafruit_ssd1306

i2c = board.I2C()
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

sensor_readings = [23, 25, 22, 24, 26]
for i, reading in enumerate(sensor_readings):
    display.text(f"Temp {i}: {reading}C", 0, i*10, 1)
display.show()
    

This code reads a list of sensor values and writes each one as text on the OLED screen, then updates the display.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through each sensor reading to write text on the display.
  • How many times: Once for each reading in the list.
How Execution Grows With Input

Each new sensor reading adds one more text write operation before updating the display.

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 roughly in direct proportion to the number of readings.

Final Time Complexity

Time Complexity: O(n)

This means the time to update the display grows linearly as you add more sensor readings.

Common Mistake

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

[OK] Correct: Each reading adds a text write operation, so more readings mean more work before the display updates.

Interview Connect

Understanding how display updates scale with data size helps you write efficient code for real devices, a skill valued in many projects.

Self-Check

"What if we buffered all readings and updated the display only once at the end? How would the time complexity change?"