0
0
Raspberry Piprogramming~10 mins

Digital output (GPIO.output) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Digital output (GPIO.output)
Setup GPIO mode
Set pin as output
Write HIGH or LOW to pin
Pin voltage changes
Connected device reacts
End or repeat
This flow shows how to set a pin as output and send a digital signal (HIGH or LOW) to control devices.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
GPIO.output(18, GPIO.LOW)
GPIO.cleanup()
This code sets pin 18 as output, turns it ON (HIGH), then OFF (LOW), and cleans up GPIO settings.
Execution Table
StepActionPinValue WrittenPin StateEffect
1Set GPIO mode---GPIO mode set to BCM
2Setup pin as output18-Output modePin 18 ready for output
3Write HIGH to pin18HIGHHIGH (3.3V)Pin 18 voltage set to HIGH, device ON
4Write LOW to pin18LOWLOW (0V)Pin 18 voltage set to LOW, device OFF
5Cleanup GPIO---GPIO pins reset to safe state
💡 All steps completed, GPIO cleaned up to avoid issues
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
GPIO modeNot setBCMBCMBCMBCM
Pin 18 modeUnknownOutputOutputOutputReset
Pin 18 stateUnknownUndefinedHIGHLOWReset
Key Moments - 3 Insights
Why do we need to set the pin mode before writing HIGH or LOW?
The pin must be set as output first (see Step 2 in execution_table) so it can send voltage signals; otherwise, writing HIGH or LOW won't work.
What happens if we write HIGH then LOW quickly?
The pin voltage changes accordingly (Step 3 and Step 4), turning the connected device ON then OFF, as shown in the Pin State and Effect columns.
Why call GPIO.cleanup() at the end?
Cleanup resets pins to safe states (Step 5), preventing unwanted signals or conflicts when the program ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin state after Step 3?
ALOW (0V)
BUndefined
CHIGH (3.3V)
DReset
💡 Hint
Check the 'Pin State' column at Step 3 in the execution_table
At which step is the pin set as output mode?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Action' and 'Pin' columns in the execution_table for when pin 18 mode changes
If we skip GPIO.cleanup(), what might happen?
APin mode resets automatically
BPin stays in last state, possibly causing issues
CProgram will not run
DPin voltage becomes random
💡 Hint
Refer to Step 5 and the key_moments about cleanup importance
Concept Snapshot
Digital output with GPIO.output:
- Set GPIO mode (e.g., BCM)
- Setup pin as output
- Use GPIO.output(pin, GPIO.HIGH/LOW) to set voltage
- HIGH = 3.3V (ON), LOW = 0V (OFF)
- Call GPIO.cleanup() to reset pins safely
Full Transcript
This visual trace shows how to control a Raspberry Pi GPIO pin as digital output. First, we set the GPIO mode to BCM numbering. Then we configure pin 18 as an output pin. Next, we write HIGH to pin 18, which sets its voltage to 3.3 volts and turns connected devices ON. After that, we write LOW to pin 18, setting voltage to 0 volts and turning devices OFF. Finally, we call GPIO.cleanup() to reset all pins to safe states. This prevents issues if the program ends or restarts. The variable tracker shows how GPIO mode, pin mode, and pin state change step-by-step. Key moments explain why setting pin mode first is necessary, how voltage changes affect devices, and why cleanup is important. The quiz tests understanding of pin states, setup steps, and cleanup effects.