Electronic – KY-040 rotary middle push button switch is unreliable

rotaryswitches

I have the following rotary encoder with a built in push switch:

enter image description here

http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/

I connect the switch pin to a regular digital input pin, putting it into input mode and looking for when does the pin go LOW (that is the pushed in state). However this is unreliable, sometimes even turning the rotary encoder triggers the switch input. Any idea how to fix this?

Best Answer

enter image description here

As you should be able to see from the picture above, pull-up resistors are required. In addition to this the arduino sketch recommends this: -

It has no switch debounce, nor does it use interrupts. A fully developed application might need to incorporate these in order to make it robust.

So, switch debounce could be a problem too. I suspect the Arduino sketch enables internal pull-up resistors on the chip BTW. I also recommend you read through the list of comments to glean any other useful information on the application of this part.

The diagram below also tells you that "both (are) open" in certain positions and this informs you that you need pull-ups or pull-downs.

enter image description here

Why are they not fitted? Because they cannot know whether you want to fit pull-ups or pull-downs i.e. it is an application thing.