Install libs:
sudo apt-get install python3-rpi.gpio
Then run:
python3 test_gpio_output.py
python3 test_gpio_input.py
``
Install libs:
sudo apt-get install python3-rpi.gpio
Then run:
python3 test_gpio_output.py
python3 test_gpio_input.py
``
import RPi.GPIO as GPIO | |
import time | |
# Set up GPIO using BCM numbering | |
GPIO.setmode(GPIO.BCM) | |
# List of GPIO pins to monitor | |
pins = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] | |
# Set up all pins as inputs | |
for pin in pins: | |
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Use pull-down resistors to avoid floating pins | |
# Dictionary to store the last state of each pin | |
last_states = {pin: GPIO.input(pin) for pin in pins} | |
print("Monitoring GPIO pins for changes. Press Ctrl+C to exit.\n") | |
try: | |
while True: | |
for pin in pins: | |
current_state = GPIO.input(pin) | |
# Check if the state has changed | |
if current_state != last_states[pin]: | |
state_str = "HIGH" if current_state == GPIO.HIGH else "LOW" | |
print(f"GPIO {pin} changed to {state_str}") | |
# Update the last state of the pin | |
last_states[pin] = current_state | |
time.sleep(0.1) # Small delay to avoid overwhelming CPU | |
except KeyboardInterrupt: | |
print("\nStopped monitoring.") | |
finally: | |
GPIO.cleanup() | |
print("GPIO pins reset to default state.") |
import RPi.GPIO as GPIO | |
# Set up GPIO using BCM numbering | |
GPIO.setmode(GPIO.BCM) | |
# List of GPIO pins to test | |
pins = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] | |
# Set up all pins as output, but don't set them HIGH yet | |
for pin in pins: | |
GPIO.setup(pin, GPIO.OUT) | |
try: | |
# Loop through each pin, setting it to HIGH, waiting for a key press, then setting it to LOW | |
for pin in pins: | |
print(f"Setting GPIO {pin} HIGH") | |
GPIO.output(pin, GPIO.HIGH) # Set current pin to HIGH | |
input(f"GPIO {pin} is HIGH. Press Enter to reset...") # Wait for user to press Enter | |
GPIO.output(pin, GPIO.LOW) # Set current pin to LOW | |
print(f"GPIO {pin} reset to LOW.\n") | |
print("Test completed for all pins.") | |
finally: | |
# Clean up the GPIO states and release resources | |
GPIO.cleanup() | |
print("GPIO pins reset to default state.") |