rpi-interface-board/rpi-scripts/examples/read_digital_inputs.py

38 lines
993 B
Python

import os
import sys
# Add the parent directory to the module search path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
# Include the common pin assignment (from parent folder)
from interface_board_pins import GPIO_DIGITAL_INPUTS # map of which GPIO pins are associated with which input terminal (1-8)
import RPi.GPIO as GPIO
import time
# Initialize GPIO
GPIO.setmode(GPIO.BCM)
for pin in GPIO_DIGITAL_INPUTS.values():
print(f"configuring pin {pin} as input")
GPIO.setup(pin, GPIO.IN)
# Repeatedly read GPIOs
print("Reading digital inputs:")
try:
while True:
print("reading all gpio pins...")
for label, pin in GPIO_DIGITAL_INPUTS.items():
print(f"reading pin {pin}")
state = GPIO.input(pin)
print(f"Input {label} (GPIO {pin}): {'HIGH' if state else 'LOW'}")
print("-" * 40)
time.sleep(0.5)
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup()