0
0
Raspberry Piprogramming~10 mins

GPIO cleanup on exit in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GPIO cleanup on exit
Start program
Setup GPIO pins
Run main code
Detect program exit
Call GPIO.cleanup()
Release GPIO resources
Program ends
The program sets up GPIO pins, runs main code, then on exit calls cleanup to free GPIO resources.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
try:
    GPIO.output(18, GPIO.HIGH)
finally:
    GPIO.cleanup()
This code sets up GPIO pin 18 as output, turns it on, and ensures cleanup runs on exit.
Execution Table
StepActionGPIO StateNotes
1Import GPIO and set mode BCMNo pins setPrepare GPIO library and numbering mode
2Setup pin 18 as outputPin 18 ready as outputPin 18 configured for output
3Try block: Set pin 18 HIGHPin 18 is ONPin 18 output set to HIGH
4Program ends or exception occursPin 18 still ONReady to cleanup
5Finally block: Call GPIO.cleanup()All pins resetGPIO pins released and reset
6Program exitsNo GPIO resources heldSafe exit with cleanup done
💡 Program exits after cleanup to avoid GPIO resource conflicts on next run
Variable Tracker
VariableStartAfter SetupAfter OutputAfter CleanupFinal
GPIO pin 18 stateUndefinedConfigured as outputSet HIGHReset to input (default)Released
Key Moments - 2 Insights
Why do we call GPIO.cleanup() in a finally block?
Using finally ensures cleanup runs even if an error happens, as shown in step 5 of the execution_table.
What happens if we don't call GPIO.cleanup() before program exit?
GPIO pins stay configured and may cause conflicts next time, explained in exit_note and step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the GPIO pin 18 state after step 3?
APin 18 is ON
BPin 18 is OFF
CPin 18 is not configured
DPin 18 is reset
💡 Hint
Check the 'GPIO State' column in row for step 3
At which step does GPIO.cleanup() get called according to the execution_table?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the action mentioning cleanup in the 'Action' column
If GPIO.cleanup() was not called, what would be the likely state of GPIO pins after program exit?
APins reset to default input
BPins remain configured and may cause conflicts
CPins are automatically cleaned by OS
DPins are disabled permanently
💡 Hint
Refer to the exit_note and key_moments about cleanup importance
Concept Snapshot
GPIO cleanup on exit:
- Setup pins with GPIO.setup()
- Use try-finally to run GPIO.cleanup()
- cleanup() resets pins to safe state
- Always cleanup to avoid conflicts
- Ensures safe program exit
Full Transcript
This example shows how a Raspberry Pi program sets up GPIO pin 18 as an output and turns it on. The program uses a try-finally block to guarantee that GPIO.cleanup() is called when the program ends or if an error occurs. Cleanup resets all GPIO pins to their default state, preventing conflicts on future runs. The execution table traces each step: importing GPIO, setting up pin 18, turning it on, then cleaning up before exit. The variable tracker shows pin 18 state changes from undefined to configured, then ON, and finally reset. Key moments explain why cleanup is in finally and what happens if skipped. The quiz tests understanding of pin states and cleanup timing. This ensures safe and clean GPIO usage on Raspberry Pi.