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 Answers
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.
- 5,053
- 17
- 30