First GPIO program (LED blink) in Raspberry Pi - Time & Space Complexity
When we write a program to blink an LED on a Raspberry Pi, it runs some instructions repeatedly. Understanding how the time it takes grows helps us know if the program will run smoothly or slow down.
We want to see how the program's running time changes as it blinks more times.
Analyze the time complexity of the following code snippet.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
for i in range(10):
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
GPIO.output(18, GPIO.LOW)
time.sleep(1)
GPIO.cleanup()
This code turns an LED on and off 10 times, waiting 1 second each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that repeats turning the LED on and off.
- How many times: Exactly 10 times in this example.
Each blink cycle takes a fixed amount of time. If we increase the number of blinks, the total time grows directly with that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 blink cycles |
| 100 | 100 blink cycles |
| 1000 | 1000 blink cycles |
Pattern observation: The total time grows in a straight line as the number of blinks increases.
Time Complexity: O(n)
This means the time to run the program grows directly in proportion to how many times the LED blinks.
[X] Wrong: "The program runs in the same time no matter how many times the LED blinks because each blink is simple."
[OK] Correct: Each blink adds more steps, so more blinks mean more total time. The program takes longer if you blink more times.
Understanding how repeated actions affect running time is a key skill. It helps you explain how your code behaves when it runs longer or with more data, which is useful in many real projects.
"What if we changed the for-loop to blink the LED forever without stopping? How would the time complexity change?"
Practice
GPIO.setup(pin, GPIO.OUT) command in a Raspberry Pi LED blink program?Solution
Step 1: Understand GPIO pin modes
The Raspberry Pi GPIO pins can be set as input or output. To control an LED, the pin must send signals, so it needs to be an output.Step 2: Role of GPIO.setup
The commandGPIO.setup(pin, GPIO.OUT)configures the pin as an output pin, allowing the program to turn the LED on or off.Final Answer:
It sets the specified pin as an output to control the LED. -> Option DQuick Check:
GPIO.setup with GPIO.OUT means output pin [OK]
- Confusing input and output pin setup
- Thinking setup turns LED on
- Mixing cleanup with setup
Solution
Step 1: Importing the GPIO library
The standard library to control GPIO pins on Raspberry Pi isRPi.GPIO, imported asGPIO.Step 2: Setting pin numbering mode
We useGPIO.setmode()to choose pin numbering style.GPIO.BOARDuses physical pin numbers, which is common for beginners.Final Answer:
import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BOARD) -> Option BQuick Check:
Import RPi.GPIO and setmode BOARD [OK]
- Using gpiozero instead of RPi.GPIO
- Wrong method names like setupmode
- Setting mode to GPIO.OUT instead of BOARD or BCM
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
for i in range(3):
GPIO.output(11, GPIO.HIGH)
time.sleep(1)
GPIO.output(11, GPIO.LOW)
time.sleep(1)
GPIO.cleanup()Solution
Step 1: Analyze the loop and GPIO output commands
The loop runs 3 times. Each time, the LED pin is set HIGH (on), waits 1 second, then set LOW (off), waits 1 second.Step 2: Understand the LED blinking pattern
This means the LED turns on for 1 second, off for 1 second, repeated 3 times, resulting in 3 blinks.Final Answer:
The LED will blink on and off 3 times with 1 second intervals. -> Option AQuick Check:
Loop with sleep controls blink count and duration [OK]
- Thinking LED stays on continuously
- Ignoring sleep delays causing rapid blinking
- Assuming missing setup causes error here
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.HIGH) time.sleep(1) GPIO.output(18, GPIO.LOW) GPIO.cleanup()
Solution
Step 1: Check pin numbering mode and pin number
The code usesGPIO.setmode(GPIO.BCM), which means pin numbers refer to Broadcom chip pins. Pin 18 is valid in BCM mode, but if the user expects physical pin numbering (BOARD), this causes confusion.Step 2: Understand the mismatch impact
If the user wired the LED to physical pin 18 (BOARD), but code uses BCM mode, the LED won't work as expected. This is a common error mixing modes and pin numbers.Final Answer:
GPIO.setmode(GPIO.BCM) conflicts with pin numbering 18 if BOARD mode expected. -> Option CQuick Check:
Pin numbering mode must match wiring [OK]
- Confusing BOARD and BCM pin numbers
- Calling cleanup too early
- Forgetting to import time module
Solution
Step 1: Setup pins as output and initialize GPIO
Both pins 7 and 11 are set as outputs usingGPIO.setup(pin, GPIO.OUT). The mode is set to BOARD to match physical pin numbers.Step 2: Alternate LEDs in a loop with delays
The loop runs 5 times. Inside each cycle, pin 7 is turned on and pin 11 off, then after 0.5 seconds, pin 7 is off and pin 11 on, creating alternate blinking.Step 3: Cleanup GPIO pins after use
CallingGPIO.cleanup()resets the pins safely after blinking finishes.Final Answer:
Code in option A correctly blinks two LEDs alternately for 5 cycles. -> Option AQuick Check:
Alternate output with sleep and cleanup [OK]
- Setting pins as input instead of output
- Not toggling both pins alternately
- Missing time delays causing no visible blink
Ready to prove it?
Pass the test and this pattern moves unknown
