LCD display (16x2) with I2C backpack in Raspberry Pi - Time & Space Complexity
When working with an LCD display using an I2C backpack on a Raspberry Pi, it's important to understand how the time taken to update the display changes as the amount of data grows.
We want to know how the program's running time changes when we write more characters to the screen.
Analyze the time complexity of the following code snippet.
from smbus2 import SMBus
import time
bus = SMBus(1)
address = 0x27
def write_string(string):
for char in string:
bus.write_byte(address, ord(char))
time.sleep(0.01)
This code sends each character of a string one by one to the LCD via I2C, pausing briefly after each character.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character in the string to send it via I2C.
- How many times: Once for every character in the input string.
As the string gets longer, the number of I2C write operations grows directly with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes + 10 pauses |
| 100 | 100 writes + 100 pauses |
| 1000 | 1000 writes + 1000 pauses |
Pattern observation: The time grows steadily and directly with the number of characters sent.
Time Complexity: O(n)
This means the time to send data to the LCD increases in a straight line as the number of characters grows.
[X] Wrong: "Sending one character or one hundred characters takes the same time because the I2C bus is fast."
[OK] Correct: Each character requires a separate write operation and a small delay, so more characters mean more time spent.
Understanding how hardware communication time grows with data size helps you write efficient code and explain your design choices clearly.
"What if we removed the delay after each character? How would the time complexity change?"
Practice
Solution
Step 1: Understand I2C backpack purpose
The I2C backpack simplifies wiring by using only two data lines (SDA and SCL) instead of many GPIO pins.Step 2: Compare options
Options A, B, and C are unrelated to I2C backpacks; only It reduces the number of GPIO pins needed to control the LCD correctly states the advantage.Final Answer:
It reduces the number of GPIO pins needed to control the LCD -> Option BQuick Check:
I2C reduces wiring = D [OK]
- Thinking I2C changes LCD resolution
- Assuming I2C adds color support
- Confusing power supply with data interface
Solution
Step 1: Identify libraries purpose
RPLCD is designed for controlling LCD displays, including those with I2C backpacks.Step 2: Eliminate unrelated libraries
PIL is for images, NumPy for math arrays, Matplotlib for plotting; none control LCDs.Final Answer:
RPLCD -> Option CQuick Check:
LCD control library = RPLCD [OK]
- Confusing image or math libraries with LCD control
- Using libraries without I2C support
- Misspelling library names
from RPLCD.i2c import CharLCD
lcd = CharLCD('PCF8574', 0x27)
lcd.write_string('Hello\nWorld!')Solution
Step 1: Understand write_string with newline
The '\n' moves the cursor to the second line on the LCD.Step 2: Check address and syntax
Address 0x27 is a common default; no syntax error in code.Final Answer:
Hello on first line, World! on second line -> Option AQuick Check:
Newline moves text to line 2 = C [OK]
- Thinking '\n' causes syntax error
- Assuming text stays on one line
- Using wrong I2C address without checking
from RPLCD.i2c import CharLCD
lcd = CharLCD(i2c_expander='PCF8574', address=0x3F)
lcd.write_string('Test')Solution
Step 1: Check CharLCD parameters
CharLCD requires 'i2c_expander', 'address', and 'port' parameters for I2C setup.Step 2: Identify missing parameter
The code misses the 'port' parameter, which is needed to specify the I2C bus number.Final Answer:
Missing 'port' parameter for CharLCD initialization -> Option AQuick Check:
Missing required 'port' parameter = A [OK]
- Assuming address 0x3F is always wrong
- Confusing parameter names
- Omitting required parameters
Solution
Step 1: Understand scrolling on 16x2 LCD
The LCD does not auto-scroll long messages; you must manually shift text.Step 2: Choose method to scroll
Using a loop to write substrings with delays simulates scrolling smoothly.Step 3: Eliminate wrong options
Write the full message once and rely on LCD auto-scroll is incorrect because LCD won't auto-scroll; C causes flicker; D is unrelated.Final Answer:
Use a loop to write substrings shifting one character at a time with delays -> Option DQuick Check:
Manual substring loop scrolls text = B [OK]
- Expecting LCD to auto-scroll long text
- Clearing display too often causing flicker
- Trying to change I2C address during scrolling
Ready to prove it?
Pass the test and this pattern moves unknown
