1

I have a PI B and i am having a bit of trouble with my GPIO pins. I am running a simple script to check if a button has been pressed that is hooked up up to pin 7. The problem is as soon as i conntect the jumper cable to the pi (other end is free) the script starts saying that the button is pressed. This is before the pi is even hooked up to the breadboard.

Code is:

import RPi.GPIO as GPIO
import time

buttonPin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(buttonPin,GPIO.IN)

if (GPIO.input(buttonPin)):
    print("pressed")
     time.sleep(0.2)

thanks

5

You need a pull up or pull down resistor. right now your pin 7 is 'floating', meaning there is nothing to determine the voltage on that wire, so it picks up all the RF that is around it, everything from TV, Radio, Cell phone, WiFi, anything that uses radio waves. See here for more information.

2

As an expansion to Butters answer. The raspberry has build in pull-up and pull-down resistors. To activate this, in your case, use

GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.