Displaying sensor readings on OLED in Raspberry Pi - Time & Space 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.
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 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.
Each new sensor reading adds one more text write operation before updating the display.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 text writes + 1 display update |
| 100 | 100 text writes + 1 display update |
| 1000 | 1000 text writes + 1 display update |
Pattern observation: The time grows roughly in direct proportion to the number of readings.
Time Complexity: O(n)
This means the time to update the display grows linearly as you add more sensor readings.
[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.
Understanding how display updates scale with data size helps you write efficient code for real devices, a skill valued in many projects.
"What if we buffered all readings and updated the display only once at the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand OLED screen function
OLED screens are used to show information visually, such as text or images.Step 2: Relate to sensor data usage
Displaying sensor readings on OLED helps users see live data clearly and quickly.Final Answer:
To display sensor readings clearly and in real-time -> Option CQuick Check:
OLED shows sensor data live = A [OK]
- Thinking OLED stores data permanently
- Confusing display with sensor accuracy
- Assuming OLED powers sensors
Solution
Step 1: Identify libraries for OLED control
Adafruit_SSD1306 is a popular Python library designed to control SSD1306 OLED displays.Step 2: Exclude unrelated libraries
matplotlib and pygame are for graphics but not specific to OLED control; numpy is for math operations.Final Answer:
Adafruit_SSD1306 -> Option AQuick Check:
Use Adafruit_SSD1306 for SSD1306 OLED [OK]
- Choosing matplotlib which is for plotting graphs
- Selecting pygame meant for game development
- Picking numpy which is for numerical calculations
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont
# Initialize display
disp = Adafruit_SSD1306.SSD1306_128_64(rst=None)
disp.begin()
disp.clear()
disp.display()
# Create blank image for drawing
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
# Draw text
font = ImageFont.load_default()
draw.text((0, 0), 'Temp: 25C', font=font, fill=255)
# Display image
disp.image(image)
disp.display()Solution
Step 1: Analyze text drawing commands
The code draws 'Temp: 25C' at position (0,0) using a default font on a blank image.Step 2: Understand display update
Calling disp.image(image) and disp.display() sends the image to the OLED, showing the text.Final Answer:
The OLED will show the text 'Temp: 25C' at the top-left corner -> Option DQuick Check:
Text drawn and displayed = A [OK]
- Assuming display stays blank without disp.display()
- Confusing image creation with display output
- Thinking code has syntax errors
disp = Adafruit_SSD1306.SSD1306_128_64(rst=None)
disp.begin()
disp.clear()
image = Image.new('1', (disp.width, disp.height))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
sensor_value = 30
draw.text((0, 0), f'Temp: {sensor_value}C', font=font, fill=255)
# Missing disp.image(image) here
disp.display()Solution
Step 1: Check display update sequence
To update OLED, image must be sent with disp.image(image) before disp.display().Step 2: Identify missing command
The code misses disp.image(image), so display shows old or blank content.Final Answer:
Missing disp.image(image) before disp.display() -> Option AQuick Check:
disp.image(image) needed before disp.display() [OK]
- Forgetting to send image to display
- Calling disp.clear() too late
- Using wrong font loading method
import time
while True:
temp = read_sensor()
image = Image.new('1', (disp.width, disp.height))
draw = ImageDraw.Draw(image)
draw.text((0, 0), f'Temp: {temp}C', font=font, fill=255)
disp.image(image)
disp.display()
time.sleep(1)What is the best improvement to reduce flicker?
Solution
Step 1: Understand flicker cause
Creating new image and draw objects each loop causes flicker due to redraw overhead.Step 2: Optimize by reusing objects
Creating image and draw once outside loop and clearing draw area inside loop reduces flicker.Final Answer:
Create the image and draw objects once outside the loop and reuse them -> Option BQuick Check:
Reuse image/draw objects to reduce flicker [OK]
- Recreating image each loop causing flicker
- Removing sleep causing too fast updates
- Calling disp.clear() after display has no effect
Ready to prove it?
Pass the test and this pattern moves unknown
