raspberry-gpio-python Wiki
A Python module to control the GPIO on a Raspberry Pi
Brought to you by:
croston
1. First set up RPi.GPIO (as described here)
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT)
2. To set an output high:
GPIO.output(12, GPIO.HIGH) # or GPIO.output(12, 1) # or GPIO.output(12, True)
3. To set an output low:
GPIO.output(12, GPIO.LOW) # or GPIO.output(12, 0) # or GPIO.output(12, False)
4. To output to several channels at the same time:
chan_list = (11,12) GPIO.output(chan_list, GPIO.LOW) # all LOW GPIO.output(chan_list, (GPIO.HIGH,GPIO.LOW)) # first LOW, second HIGH
5. Clean up at the end of your program
GPIO.cleanup()
Note that you can read the current state of a channel set up as an output using the input() function. For example to toggle an output:
GPIO.output(12, not GPIO.input(12))
Wiki: BasicUsage
Wiki: Examples
I am having some troubles for signalizing of Raspberry Pi digital pins.
Let me explain the problem
The thing I was trying to do make a single pin constantly open, and ON-OFF other pins
Lets say, GPIO.OUT 16 will constantly be open, meanwhile, the PIN 17, 27 and 22 will be open and close in a sequence
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, GPIO.HIGH)
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.HIGH)
GPIO.output(22, GPIO.HIGH)
time.sleep(2)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
time.sleep(2)
GPIO.cleanup()
As you see from the code, I am trying to open PIN17, 16, 27 and 22, but closing only 17, 27, 22, however, in my experimental hardware, the code does not work and I do not see the LEDS blinking, can you tell me where is the problem ?
Your Programm runs only once.
Try it within a while loop:
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
while True:
GPIO.output(16, GPIO.HIGH)
GPIO.output(17, GPIO.HIGH)
GPIO.output(27, GPIO.HIGH)
GPIO.output(22, GPIO.HIGH)
time.sleep(2)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
time.sleep(2)
GPIO.cleanup()
https://www.tutorialspoint.com/python/python_while_loop.htm
Last edit: Ness 2021-05-22