Use GPIOs in multiple Threads

January 3, 2024, 21:46

lucio0428

Hey, I wrote a python program that opens multiple threads (less threads than the cor can max handle, so no overload) and I defined some gpios as inputs_pullups and as outputs. The threads should use the gpios and act with them, but I get no reaction. The code works, except for the GPIO interaction. does anyone know what the problem is, had similar problems or knows how to fix it? here is an excerpt of how the gpios are used with the trads:
import RPi.GPIO as GPIO
import threading
 
def led_test():
    while true:
        PIO.output(led_one, GPIO.HIGH)
 
        button_state = GPIO.input(bt_pin)
        if button_state == GPIO.LOW:
            PIO.output(led_one, GPIO.LOW)
 
led_thread = threading.Thread(target=led_test)
 
led_one = 24 
led_two = 18 
bt_pin = 27 
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(bt_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led_one, GPIO.OUT)
GPIO.setup(led_two, GPIO.OUT)
 
PIO.output(led_two, GPIO.HIGH)
led_thread.start()

_krazy.

Maybe it detected multiple processes trying to change gpio, which triggered a safety mechanism to protect the pi, hence any wrong command may burn the whole board, as they're all connected (SBC).

lucio0428

I solved it 🙂

oops.se

And please post "How"

lucio0428

...I used copy and paste, but the G in GPIO got lost and since I had built a try catch around it, this error was never noticed