Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Easy button

$
0
0
Sorry for the newb question. I thought this would be easy but turning out to fight me.
When I leave the batteries in it returns all 1s without pushing and when I take out AA batteries it returns 0s. Neither recognize button push.
I have jumper going to gpio 27 and ground. 😞

import RPi.GPIO as GPIO
import time

# Set up GPIO
GPIO.setmode(GPIO.BCM) # Use BCM numbering
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Use GPIO 27 with pull-down resistor

try:
while True:
gpio_state = GPIO.input(27) # Read GPIO state
print("GPIO State: ", gpio_state) # Should print 0 when not pressed, 1 when pressed
if gpio_state == GPIO.HIGH:
print("Button Pressed!")
time.sleep(0.2) # Small debounce delay
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
IMG_6356.jpeg

Statistics: Posted by Bmilla — Fri Oct 25, 2024 3:16 am



Viewing all articles
Browse latest Browse all 1251

Trending Articles