LED class and methods in Raspberry Pi - Time & Space Complexity
We want to understand how the time it takes to control LEDs grows as we use more LEDs or call methods multiple times.
How does the number of LEDs or method calls affect the total work done?
Analyze the time complexity of the following code snippet.
class LED:
def __init__(self, pin):
self.pin = pin
self.state = False
def turn_on(self):
self.state = True
def turn_off(self):
self.state = False
leds = [LED(pin) for pin in range(5)]
for led in leds:
led.turn_on()
This code creates 5 LED objects and turns each one on by calling the turn_on method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
turn_onmethod on each LED object. - How many times: Once for each LED in the list (5 times in this example).
As the number of LEDs increases, the number of times we call turn_on grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to turn_on |
| 100 | 100 calls to turn_on |
| 1000 | 1000 calls to turn_on |
Pattern observation: The work grows directly with the number of LEDs.
Time Complexity: O(n)
This means if you double the number of LEDs, the time to turn them all on roughly doubles.
[X] Wrong: "Turning on multiple LEDs happens all at once, so time stays the same no matter how many LEDs there are."
[OK] Correct: Each LED needs its own method call, so the total time adds up with more LEDs.
Understanding how loops over objects affect time helps you explain how your code scales when controlling hardware like LEDs.
What if we added a nested loop to blink each LED multiple times? How would the time complexity change?
Practice
What does the on() method of an LED class typically do in Raspberry Pi programming?
Solution
Step 1: Understand the purpose of
Theon()methodon()method is designed to switch the LED light to the ON state.Step 2: Compare with other methods
Other methods likeoff()turn the LED off, andblink()makes it blink. Cleanup is done separately.Final Answer:
Turns the LED light on -> Option AQuick Check:
on()means LED ON [OK]
- Confusing on() with off()
- Thinking on() cleans GPIO pins
- Assuming on() makes LED blink
Which of the following is the correct way to create an LED object on GPIO pin 17 using the gpiozero library?
from gpiozero import LED led = ?
Solution
Step 1: Recall LED object creation syntax
In gpiozero, you create an LED by calling LED(pin_number), e.g., LED(17).Step 2: Check other options for syntax errors
Options A, B, and D use invalid Python syntax for object creation.Final Answer:
LED(17) -> Option AQuick Check:
LED(pin) = LED(17) [OK]
- Using square brackets instead of parentheses
- Trying to call a method like LED.pin()
- Using curly braces which are invalid here
What will be the output of this code snippet controlling an LED?
from gpiozero import LED
from time import sleep
led = LED(22)
led.on()
sleep(1)
led.off()
print('LED state:', led.is_lit)Solution
Step 1: Trace the LED state changes
The LED is turned on, then after 1 second turned off before printing.Step 2: Check the printed LED state
Afterled.off(),led.is_litis False, so output is 'LED state: False'.Final Answer:
LED state: False -> Option DQuick Check:
LED off means is_lit = False [OK]
- Assuming LED stays on after off()
- Confusing is_lit property value
- Expecting syntax error due to sleep
Identify the error in this code that tries to blink an LED on pin 5:
from gpiozero import LED from time import sleep led = LED(5) led.blink(on_time=0.5, off_time=0.5) sleep(2) led.off()
Solution
Step 1: Review the code for GPIO cleanup
The code blinks the LED and turns it off but does not release GPIO resources.Step 2: Identify missing cleanup step
Proper practice is to callled.close()to free GPIO pins after use.Final Answer:
Should callled.close()to clean GPIO -> Option BQuick Check:
Always close GPIO pins after use [OK]
- Thinking blink() needs on() first
- Assuming blink() requires times argument
- Ignoring GPIO cleanup after program
You want to create a program that blinks two LEDs on pins 17 and 27 alternately every 0.5 seconds for 5 seconds total. Which code snippet correctly implements this using the LED class?
from gpiozero import LED
from time import sleep
led1 = LED(17)
led2 = LED(27)
start = 0
while start < 5:
led1.on()
led2.off()
sleep(0.5)
led1.off()
led2.on()
sleep(0.5)
start += 1
led1.off()
led2.off()
led1.close()
led2.close()Solution
Step 1: Analyze the loop timing and LED control
The loop runs 5 times, each iteration takes 1 second (two 0.5s sleeps), total 5 seconds blinking alternately.Step 2: Check LED on/off sequence and cleanup
LED1 and LED2 turn on/off alternately with proper sleep delays, and GPIO pins are closed at the end.Final Answer:
This code blinks LEDs alternately for 5 seconds correctly -> Option CQuick Check:
Loop count x sleep = total blink time [OK]
- Miscounting total blink duration
- Forgetting to close GPIO pins
- Assuming blink() method is mandatory
Ready to prove it?
Pass the test and this pattern moves unknown
