Electrical – Wire microswitch so state can be read from Raspberry gpio

gpioraspberry piwiring

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

Best Answer

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