0
\$\begingroup\$

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?

Micro switch

\$\endgroup\$
4
\$\begingroup\$
  • 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.

schematic

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.

\$\endgroup\$
  • \$\begingroup\$ +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. \$\endgroup\$ – Spehro Pefhany Feb 5 '16 at 20:18
  • \$\begingroup\$ 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? \$\endgroup\$ – Mathias Rönnlund Feb 5 '16 at 22:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Transistor Feb 5 '16 at 22:06
  • \$\begingroup\$ @transistor Updated the question with some details and more questions. \$\endgroup\$ – Mathias Rönnlund Feb 6 '16 at 19:36
  • 1
    \$\begingroup\$ Hi, @MathiasR: Answer updated with more details and more answers! \$\endgroup\$ – Transistor Feb 6 '16 at 19:42
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

@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.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$

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.