# Wire microswitch so state can be read from Raspberry gpio

I'm trying to check whether a series of valves (described here Can I measure if current is on or off in cable with 24v DC (in a cheap way)) are open or not.

Updated question to be more specific after getting some answers:

If I mount a micro switch, like the one in the image below, on the valve so that the "normally open" pin on the switch is closed when the valve is opened, how would I wire it to a gpio on a Raspberry so I can check if it closed or not, e.g. every 10 minutes?

I might use the gpio utility to check the pin. Would I then wire the "common" to a 3.3v on the Raspberry and the "normally open" to a gpio and then set the pin's mode to pulldown with gpio mode <pin> down? And the read the pin with gpio read <pin>?

Question 2: Can I wire multiple switches to the same voltage pin on the Rapsberry?

Question 3: Can I freely select to use either 3.3v and 5v?

## 3 Answers

• The pin on the left is the common.
• The pin in the middle is normally open.
• The pin on the right is normally closed. (You can see the contacts touching).

How you wire it up depends on the input configuration for the GPIO.

• If you configure the Pi GPIO with internal pull-up resistors then the switch needs to "pull-down" to ground.
• If you configure the Pi GPIO with internal pull-down resistors then the switch needs to "pull-up" to 3.3 V.

simulate this circuit – Schematic created using CircuitLab

Figure 1. Pull-up and pull-down arrangements.

If I mount a micro switch, like the one in the image below, on the valve so that the "normally open" pin on the switch is closed when the valve is opened, how would I wire it to a gpio on a Raspberry so I can check if it closed or not, e.g. every 10 minutes?

I might use the gpio utility to check the pin. Would I then wire the "common" to a 3.3v on the Raspberry and the "normally open" to a gpio and then set the pin's mode to pulldown with gpio mode down? And the read the pin with gpio read ?

Correct.

Question 2: Can I wire multiple switches to the same voltage pin on the Rapsberry?

Yes, they will all share the same voltage and can be wired to the same pin. There is very little current involved.

Question 3: Can I freely select to use either 3.3v and 5v?

No. You need to use the same voltage that the chip is using.

• +1 In some cases you might wish to use lower value external pull-ups or pull-downs, or to add some filtering or series resistance if the wires to the switch are long and in a noisy environment, but the above will work perfectly otherwise. – Spehro Pefhany Feb 5 '16 at 20:18
• The buffer in the schematics, does that symbolize the gpio or do I need a buffer (maybe to protect the gpio from spikes)? What resistor should I use if I connect it to 5v? – Mathias Rönnlund Feb 5 '16 at 22:02
• The box represents the Pi. The pull-up/down resistors are required for so many applications that they're built into many micro-controller chips to reduce the external component count. You need to configure these in your application. I can't answer your question about the spikes as you haven't given any details of your application. Add them to your original question and put a comment here to let me know. – Transistor Feb 5 '16 at 22:06
• @transistor Updated the question with some details and more questions. – Mathias Rönnlund Feb 6 '16 at 19:36
• Hi, @MathiasR: Answer updated with more details and more answers! – Transistor Feb 6 '16 at 19:42

You can, but you would have trouble when the switch is open. When the GPIO pin is not connected to anything (when the switch is open), it would act like an antenna and go high and low at random. So you'd want to tie a resistor from that pin to ground.

There are a few technical reasons to do this a little differently. Connect a resistor between 3.3v and the leftmost pin (with GPIO), and connect the middle pin to ground. When done this way, you will input a 1 when the switch is open, and a 0 when it's pressed. Your software can reverse this logic if necessary.

@transistor has given an excellent answer, but I would like to add a little to it. When interfacing to a switch which may be a short distance from the Raspberry Pi I suggest adding a few components to protect the inputs of Pi from voltage spikes that are so easily induced into wiring. The following circuit will do exactly that. In addition it will provide a little contact debouncing. Search the forum for "switch debouncing" to understand it.

simulate this circuit – Schematic created using CircuitLab