Files
matrix64/test_buttons.py
2025-12-31 13:00:47 +01:00

63 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""
GPIO Button Test Script
Run this on the Raspberry Pi to identify which GPIO pins the buttons are on.
Press Ctrl+C to exit.
"""
import time
try:
import RPi.GPIO as GPIO
except ImportError:
print("RPi.GPIO not found. Install with: sudo apt install python3-rpi.gpio")
exit(1)
# Common GPIO pins to test (BCM numbering)
# Excluding pins used by the LED matrix (typically 2-27 are used by the matrix)
# These are additional GPIO pins that might be available
TEST_PINS = [4, 5, 6, 12, 13, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
def main():
print("=" * 50)
print("GPIO Capacitive Button Test")
print("=" * 50)
print("\nSetting up GPIO pins for input with pull-up resistors...")
print("Press buttons to see which GPIO pin responds.")
print("Press Ctrl+C to exit.\n")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set up all test pins as inputs with pull-up
active_pins = []
for pin in TEST_PINS:
try:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
active_pins.append(pin)
except Exception as e:
print(f" Pin {pin}: Could not configure ({e})")
print(f"Testing pins: {active_pins}\n")
# Store previous states to detect changes
prev_states = {pin: GPIO.input(pin) for pin in active_pins}
try:
while True:
for pin in active_pins:
current = GPIO.input(pin)
if current != prev_states[pin]:
if current == GPIO.LOW:
print(f">>> Button PRESSED on GPIO {pin} <<<")
else:
print(f" Button released on GPIO {pin}")
prev_states[pin] = current
time.sleep(0.05)
except KeyboardInterrupt:
print("\n\nTest complete.")
finally:
GPIO.cleanup()
if __name__ == "__main__":
main()