0
\$\begingroup\$

I am trying to get a lever micro switch working with my raspberry pi GPIO. This is the lever switch in question.

KW11-3Z

On the switch it has the model number on one side KW11-3Z and then the other side it says 5a250vac.

I have wired it to COM to GND and NO to GPIO 18. I have tested with a simple push button and this work as expected again wiring to GND and GPIO 18. Therefore I know the python code is correct.

The part I dont know is correct is the switch itself and the wiring.

Here are some images that will help to show how I have things currently.

working with tactile switch You can see I have a breadboard with a tactile switch here and this is working as expected. Each time the button is pressed the output on terminal is "Button pressed".

not working with micro switch Running the same python code I have swapped for the micro switch with using the same pins on raspberry pi of GND and GPIO 18 however each time the lever is pressed it isn't showing anything in terminal, no echo of "Button pressed".

Can anyone please advise what I am doing wrong here. I suspect I have got the configuration of the wires wrong as the code is ok.

\$\endgroup\$
  • \$\begingroup\$ Do you have access to a multimeter? \$\endgroup\$ – Voltage Spike Dec 16 '16 at 20:02
  • \$\begingroup\$ @laptop2d no sorry I do not, I can possibly get one though as this is my first real raspberry pi project but I hope to do more in the future. \$\endgroup\$ – Phil Cook Dec 19 '16 at 11:05
  • \$\begingroup\$ I have now been given a multi meter and can do some testing, any advice here? \$\endgroup\$ – Phil Cook Jan 12 '17 at 8:12
  • \$\begingroup\$ Make sure one leg of the switch is at ground and the other at 3.3V when open, when closed both should go to ground. If you want to test the switch, put the multimeter in ohms mode and put it across the terminals of the switch, open should read infinite and closed should read near zero (your measuring resistance) \$\endgroup\$ – Voltage Spike Jan 12 '17 at 16:55
2
\$\begingroup\$

Your lever switch is called a single-pole-double-throw switch, SPDT. As it sits, you will measure very little resistance between COM and NC. Between COM and NO you should measure very high resistance (like infinite, or off-scale resistance). And between NO and NC, you should measure very high resistance.

schematic

simulate this circuit – Schematic created using CircuitLab For the schematic shown, your GPIO will read "low" (logic 0) when the lever of SW2 is not activated, and will go "high" (logic 1) when the lever is pushed. Be aware that any switch when it changes state, will bounce around between the two logic states, for an undetermined number of bounces, before settling to its next state. This bouncing may take many milliseconds before settling. So if you wish to count button-presses, you may count very many bounces for a single press.
The resistor R1 is required for reliability. It is called here a "pull-up" resistor, because it applies a 3.3v logic level to the GPIO pin when the button is pushed. Its value is not critical, but as shown, 33 microamps is pulled from the 3.3v supply until the button is pushed. So you don't want to make R1 too small in value.
You have wired it in an alternate way, with GPIO going to "NO" terminal, as in SW3. The pull-up resistor should be wired to this terminal. Now, a push will yield logic low, while un-pushed will yield a logic high. This works too, but use that pull-up resistor so that un-pushed, GPIO sees a "high". This may be a safer circuit. If you were to re-program a GPIO pin to be an output rather than an input, your output would see a 10K resistor until the button was pressed. Once the lever is pressed, the GPIO would see a short to ground, and could harm the driver inside the PI's CPU. GPIO's generally power-up as "inputs" rather than output. Ensure that your GPIO is set as "input" when reading switch state.

\$\endgroup\$
  • \$\begingroup\$ thanks for the answer however most of this does go over my head a little as I am more a software person. I think I will try and show some more of how I currently have it setup with my push button and then maybe get a steer of you how to change to the switch. \$\endgroup\$ – Phil Cook Dec 21 '16 at 14:34
  • \$\begingroup\$ I have followed the diagram you did and it now works as I expected. Thanks for your advise. \$\endgroup\$ – Phil Cook Jan 12 '17 at 13:16
1
\$\begingroup\$

Make sure to attach a pull-up resistor -- about 3.3k -- from your GPIO pin to +3.3V so that your input isn't floating when the switch isn't pushed. Other than that you should be good.

\$\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.