0

I'm a newbie to raspberry pi build a small circuit using a switch and capturing the input of the switch if I press it.

After the circuit been designed I dont't its working as expected. Is there a way to validate and see if my circuit is correct or wrong?

enter image description here

Here is my code:-

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.IN)

while True:
      input_value = GPIO.input(12)
      if input_value == True:
         print("the button has been pressed")
         while input_value == False:
               input_value = GPIO.input(12)
3
  • @CoderMike Added the code and the picture. – user1050619 Nov 6 '17 at 17:38
  • Change while input_value to if input_value, same with the first one. Also, add some debounce logic, else the button has been pressed may trigger multiple times. – PNDA Nov 9 '17 at 6:06
  • Oh, and the questions needs more info (pic of the Pi, pic of the entire setup, and explanation on what you want to do). I dont't its working as expected: What exactly are you expecting? – PNDA Nov 9 '17 at 6:07
3

Not sure why you've got Ground and 3V3 used in your circuit, you want one or the other depending on whether the input is pulled high or low.

You are also using the wrong pin (12) if you are using GPIO.BOARD - should be pin 32. If you were using GPIO.BCM then 12 would be correct. There is also no need for your 2nd while loop.

https://pinout.xyz/pinout/pin32_gpio12

Here is a circuit using 3V3 with the input pulled low :

enter image description here

I've amended the code for pin 32 :

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(32,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)

while True:
    input_value = GPIO.input(32)
    print(input_value)
    if input_value == True:
        print("the button has been pressed")
    time.sleep(0.1)
0

Be aware you should never connect a voltage greater than 5V to a Pi GPIO (they are all 3V3 safe only).

There are plenty of GPIO libraries which will display the GPIO state. Depending on your operating system both wiringPi and (my) pigpio are likely to be pre-installed.

For wiringPi use the gpio readall command. For pigpio use pigs r X where X is the Broadcom GPIO number.

See https://elinux.org/RPi_GPIO_Code_Samples for lots of alternatives.

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