16x2 LCD with LiquidCrystal library in Arduino - Time & Space Complexity
When using a 16x2 LCD with the LiquidCrystal library, it's helpful to know how the time to update the display changes as we write more characters.
We want to understand how the program's running time grows when printing text on the LCD.
Analyze the time complexity of the following code snippet.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
for (int i = 0; i < 32; i++) {
lcd.print('*');
}
}
void loop() {}
This code initializes a 16x2 LCD and prints 32 stars, filling both lines of the display.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls
lcd.print('*')32 times. - How many times: Exactly 32 times, once for each character printed on the LCD.
Each character printed takes roughly the same time, so the total time grows as we print more characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The time grows directly with the number of characters printed; doubling characters doubles time.
Time Complexity: O(n)
This means the time to print grows in a straight line with the number of characters you send to the LCD.
[X] Wrong: "Printing multiple characters at once is as fast as printing one character."
[OK] Correct: Each character requires communication with the LCD, so printing many characters takes proportionally more time.
Understanding how loops and repeated operations affect time helps you write efficient code for devices like LCDs, showing you can think about performance in real projects.
"What if we replaced the for-loop with a function that prints a whole string at once? How would the time complexity change?"
Practice
lcd.begin(16, 2) command do in the LiquidCrystal library?Solution
Step 1: Understand lcd.begin() function
Thelcd.begin()function initializes the LCD with the number of columns and rows.Step 2: Apply to given parameters
Usinglcd.begin(16, 2)sets the screen size to 16 columns and 2 rows.Final Answer:
It sets up the LCD screen size to 16 columns and 2 rows. -> Option BQuick Check:
lcd.begin(16, 2) = sets screen size [OK]
- Thinking lcd.begin clears the screen
- Confusing lcd.begin with lcd.print
- Assuming lcd.begin moves the cursor
Solution
Step 1: Recall LiquidCrystal constructor syntax
The constructor requires 6 pins: RS, Enable, D4, D5, D6, D7 in order.Step 2: Match given pins to correct syntax
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); matches the correct order and number of pins.Final Answer:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); -> Option CQuick Check:
Constructor needs 6 pins in order [OK]
- Using wrong number of pins
- Wrong pin order
- Using assignment instead of constructor
lcd.begin(16, 2);
lcd.setCursor(0, 1);
lcd.print("Hello");Solution
Step 1: Understand lcd.setCursor(0, 1)
Cursor moves to column 0, row 1 (second row, since rows start at 0).Step 2: lcd.print("Hello") prints at cursor
Text "Hello" appears on the second row starting at first column.Final Answer:
Hello on the second row -> Option AQuick Check:
setCursor(0,1) = second row [OK]
- Assuming row 1 is first row
- Forgetting cursor position before print
- Expecting text on both rows
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
lcd.begin(16, 2);
lcd.setCursor(1, 2);
lcd.print("Hi");Solution
Step 1: Check lcd.setCursor parameters
Rows are 0 and 1 for a 2-row LCD; row 2 is out of range.Step 2: Confirm other code parts
lcd.begin() is present, object created correctly, and print syntax is correct.Final Answer:
Cursor row 2 is invalid for 2-row LCD -> Option AQuick Check:
Row index must be 0 or 1 for 16x2 LCD [OK]
- Using row 2 instead of 0 or 1
- Forgetting lcd.begin() call
- Incorrect object creation syntax
Solution
Step 1: Calculate starting position for centering
LCD width is 16 columns; word length is 5 letters. (16 - 5) / 2 = 5.5, round to 6.Step 2: Set cursor to column 6 on first row (row 0)
Uselcd.setCursor(6, 0)to start printing "Ready" centered.Final Answer:
lcd.setCursor(6, 0); lcd.print("Ready"); -> Option DQuick Check:
(16-5)/2 = 5.5 ≈ 6 for centering [OK]
- Using wrong column for centering
- Confusing row numbers
- Not rounding the position
Ready to prove it?
Pass the test and this pattern moves unknown
