2

I'm new to stackexchange and also to the pi. And i'm trying my first project. I was wondering how to wire three of these COM-00097 Switches to a Raspberry pi to make it output a different message when a different button is pressed. unfortunatelyi still don't fully understand how to connect a component to a GPIO (Resistors to use, pins to connect to) and all the guides i've found out there just explain how the GPIO is made or how to wire a specific device to the PI, they don't explain how to calculate and design the circuit. If you could point me in the right direction and give me some advice about the COM-00097 that would be great! Thanks.

  • 1
    They are just a switch. When you press the button two tags on one side are electrically connected to the two tags on the other side. Just follow any tutorial for a switch and tell us if you have a problem. – joan Oct 8 '15 at 21:40
3

The basics:

The Raspberry Pi's GPIO pins work with 0V-3V3 differential from GND, and can work in either Output (the RPi sends out a signal) or Input (the RPi listens for a signal) mode. In Output mode, if you make the pin HIGH in your program, the GPIO pin's voltage will be at 3V3 compared to GND. If you make the pin LOW in your program, the GPIO pin's voltage will be at 0V compared to GND. In Input mode, the pin has a value corresponding with voltage supplied by the component attached to it - either 0V or 3V3 compared to GND, provided the component is connected to the same GND as the rPi (otherwise voltage levels are "random") [Technically, there are thresholds between 0V and 3V3 below which the pin is considered LOW and above which the pin is considered HIGH; Also, make sure the component supplies 3V3 signals, not 5V or higher, because applying a voltage that is too high could break your RPi]. If there is no component connected, or the connected component does not complete an electric circuit using the pin's wire, the pin is considered to be "floating" and the voltage can be anything between 0V and 3V3. You can remedy this situation by enabling a built-in pull-up or a pull-down resistor on the rPi end through your code. A pull-up will make a floating pin read 3V3, a pull-down will make a floating pin read 0V.

Connecting your switch

The simplest way to connect your switch is by connecting one end to the RPi's 3V3 pin, and the other end to a GPIO pin. In your program, make sure the GPIO pin is set to Input, and make sure you have a pull-down resistor enabled to make the pin LOW when the button is not pressed (without a pull-down resistor you might be detecting phantom button presses). Now monitor the state of the GPIO pin in your program and you will notice it will be showing as HIGH when the button is pressed, and LOW when not.

There is however an important caveat: your switch is rated for maximum 50mA, and so are the GPIO pins. It is not fully documented how much current the 3V3 pin on the RPi can supply - some say 50mA, some say 100mA - but the net is that this setup pulls as much current as possible from the 3V3 pin through the switch into the RPi's GPIO pin. It likely will not damage your RPi (otherwise I wouldn't even mention this setup) but to be safe, you should put a resistor in the switch circuit (either between 3V3 and switch, or between switch and GPIO pin - the latter is most typically done). The resistor will limit the current through the circuit as defined in the V=I*R equation. Since you know V (3V3) and want to limit I to as small as possible (let's say <10mA), the equation gives 3.3 = 0.010 * R or R = 3.3/0.010 = 330Ohm. So any resistor bigger than or equal to 330Ohm will do (470Ohm, 1kOhm, even 10kOhm).

Multiple Switches:

Wiring up multiple switches is now simple - use the same 3V3 supply wire, branch it to each of the switches, and connect the other end of each switch to a different GPIO pin. Follow my earlier instructions and you can now programmatically monitor the state of each button. You might want to look at events in the language you'll be using so that you do not have to write a program that actively scans the GPIO pins all the time to see if a button was pressed, but rather have it wait idly until the event associated with the button press fires.

PS: Never, EVER, set your GPIO pin to Output and then treat it like an Input. Setting the pin to Output disables a bunch of protective circuits inside the RPi, and when the current from the input signal arrives at the rPi it goes straight to the Broadcom chip, potentially frying it as a result.

  • Good answer -- but isn't it simpler to use one resistor (in the size range you mention, for an external pull up from 3.3V) so unpressed is high on the input and a press is low (sends 3.3V to ground)? This way you do not need to worry about a floating state (the input is either high, or grounded). Note I am not sure if that set-up has more limitations with multiple buttons in parallel (but it should be fine with three, maybe aim for the small end w/ the resistors??). – goldilocks Oct 9 '15 at 14:58
  • E.g. learn.adafruit.com/… <- Actually those are 10k. I imagine it does not take much current to hold the input up. – goldilocks Oct 9 '15 at 15:03
  • @goldilocks: Sure, you could do an external pull-up as well, as long as the pull-up resistor is of a higher value than the resistance of the GPIO pin. However, wouldn't you need a pull-up resistor for each button that way? – Phil B. Oct 9 '15 at 16:15
  • Okay good point -- you can put the resistor between the 3.3V out and the rail. I missed the implicitness of that; I have not tried it that way (although I think you could do the same thing with the pull-up method). I think your method (pull-down, button up = low) is better in the sense that doesn't use any power when the buttons are up. This is good for me to think about because I have a multi button thing on my TODO list. You could also try using a GPIO output instead of the 3.3V and not need any resistors, I think. – goldilocks Oct 9 '15 at 17:28
  • @Goldilocks - you made me re-read my post and I noticed a few mistakes which I have just corrected (e.g. it said "enable either pull-up or pull-down" which should have been only "pull-down" ("pull-up" would have the pin always read 3V3 and then you have no way of detecting if the button is pressed) – Phil B. Oct 9 '15 at 18:06

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.