Quantcast
Viewing all articles
Browse latest Browse all 5402

Beginners • GPIO pins always stuck on low

Hey all,

I am currently working with a limit switch and the pi. While testing the limit switch, I interrupted the script with the keyboard.

Since having done that, I no longer get a response to say the limit switch has been pressed from my GPIO pin. Through raspi-config I have checked the status of the pins and am able to reset the pins, so I don't think it's the physical pins.

However, I've tried GPIO pins 16, 20 and 21, and they all worked the first time, and displayed the correct information when I clicked the limit switch.

But after an interrupt on each of those pins, I can no longer get the pi to read the limit switch on those pins.

This is the code I have so far

What am I doing wrong?

Code:

import RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)SWITCH_PIN = 16DEBOUNCE_TIME_MS = 200GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)switch_state = GPIO.input(SWITCH_PIN)prev_switch_state = switch_statedef button_callback(channel):    global switch_state    switch_state = GPIO.input(SWITCH_PIN)GPIO.add_event_detect(SWITCH_PIN, GPIO.BOTH, callback=button_callback, bouncetime=DEBOUNCE_TIME_MS)try:    while True:        if switch_state != prev_switch_state:            if switch_state == GPIO.HIGH:                print("The limit switch: TOUCHED -> UNTOUCHED")            else:                print("The limit switch: UNTOUCHED -> TOUCHED")                        prev_switch_state = switch_state        if switch_state == GPIO.HIGH:            print("The limit switch: UNTOUCHED")        else:            print("The limit switch: TOUCHED")except KeyboardInterrupt:    GPIO.cleanup()
A little more info:

I have run sudo apt update, I'm on raspbian OS and on a Pi4. I am using the rpi-lgpio package, but only just switched to that after having issues getting edge detection on the old package.

I am also connected from a gnd pin on my Pi to the NO part of an 'SPDT 250V 5A Standard Micro Switch with Lever' from Jaycar, with whichever GPIO pin for data to COM.

I had the switch working fine the other day with the same script for hours. I have tried 2 separate limit switches as well to take that out of the equation. This is also a brand new Pi4 that I bought at the time I got the limit switches.

Statistics: Posted by theguykai — Wed Jul 03, 2024 11:04 am



Viewing all articles
Browse latest Browse all 5402

Trending Articles