3

It's my understanding that if a switch is wired to ground and the other to a GPIO pin you need a pull-up on the GPIO. Then the GPIO will usually read high and will usually read low when the switch is closed.

GPIO.setmode(GPIO.BCM)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.input(16)

However, this appears to be the opposite for me. Using the code above my button, (connected to ground and GPIO 16) reads True/1 when pushed/pressed, why?

This is the button I'm using:

push to release button

4
  • Is it a latching button?
    – joan
    Dec 13, 2017 at 21:18
  • Do you mean it reads false when released and true when pressed? BTW a picture of a button is useless. If you have the part number or datasheet we can see if its a Normally-Open (NO) or Normally Closed (NC) Switch. No way to tell from a picture
    – crasic
    Dec 13, 2017 at 21:27
  • @crasic sorry my question is a little dumb and I realise that a picture does not help. Indeed, it reads 1 when pressed down and 0 when released. Should I assume that this is an always closed switch then (in release state)? would that be why?
    – MarkK
    Dec 13, 2017 at 22:06
  • @MrKnotts it's just a common option/variety of switch that is for sale. I have added an answer that will hopefully clear stuff up.
    – crasic
    Dec 13, 2017 at 23:23

1 Answer 1

3

Switches, relays, etc. come in Normally Closed (NC) or Normally Open (NO) varieties,

enter image description here

You can easily check which one you have with a multimeter.

Many Switches come in both varieties and you choses when you order, its just a matter of electrical need, sometimes you need NO sometimes NC, if you want to know why this would matter I suggest asking another question here or on electronics.stackexchange.com .

However, all is not lost. You have the flexibility of inverting your logic, you may do this in code

read_value = !read_value

or you can do this in hardware

PU -> PD
Switch to 0V -> Switch to 3V3

And from your software POV it would be what you expect the value (1 when released 0 when pressed)

1
  • 1
    Both of those switch actions are described as momentary in that the change in connections that result in holding your mitt on the button is undone when you remove it - as well as the NO and NC descriptions they can also be described as OFF-(ON) and ON-(OFF) with the bracketed state being the momentary one...
    – SlySven
    Dec 14, 2017 at 1:02

Your Answer

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

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