1

I'm completely new to python and have a problem. I'm working on a project for school with the Raspberry Pi and have trouble reading two buttons at once. Both buttons work but I dont know how I can get input from both at the same time. I only managed to read button 1 first and then button 2 couldn't even read them more then once. My question is: How can I manage to read them in any order and multiple times?

1
  • You can use either seperate thread to read the states of the buttons or nest two ifs together. I could post you a solution once I get home Commented Feb 14, 2017 at 9:54

1 Answer 1

1

I had the same problem. First you must declare the GPIO, importing relevant GPIO library

import RPi.GPIO as GPIO
import time

#Substitute 24 and 25 for whatever pins your push buttons are connected to.
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Then assign these buttons to the variables
Button_1 = GPIO.input(24)
Button_2 = GPIO.input(25)

while True:
    if Button_1 == False and Button_2 == False:
        print('Both buttons are pressed')
        time.sleep(0.2)

This code works, so please ask questions if you have any problems.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.