import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) print(GPIO.gpio_function(18))
The code sets pin 18 as an output pin using BCM numbering. The gpio_function returns 1 for output mode.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(22, "OUT")
The GPIO.setup function requires the mode argument to be a constant like GPIO.OUT, not a string. Passing "OUT" causes a ValueError.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, GPIO.HIGH) GPIO.output(17, GPIO.LOW) GPIO.cleanup()
The code sets the pin HIGH and then immediately LOW without any pause. The change happens too quickly to notice. Adding a delay between the two commands would make the toggle visible.
GPIO.BCM mode refers to the Broadcom chip's GPIO numbering, while GPIO.BOARD mode refers to the physical pin numbers on the Raspberry Pi's header.
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) pins = [5, 6, 13, 19] for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.setup(6, GPIO.IN) GPIO.setup(19, GPIO.IN)
Initially, pins 5, 6, 13, and 19 are set as outputs. Then pins 6 and 19 are reconfigured as inputs. So only pins 5 and 13 remain outputs.
