Tuesday, November 17, 2015


Arduino KY-040 Rotary encoder module

This was the first time i used a rotary encoder and i took me more time than i expected. The info about the KY-040 Rotary encoder module was a bit confusing. The KY-40 Rotary encoder is not very well documented in one place. Several examples can be found, some of them i tried.
On Tkkrlab is an example with additional leds and a link to a Youtube video. This needed example needs several additional components and there is no clear info how to wire it. I wanted a demo with as less additional components as possible.
The information on Henrysbench seemed more complete and useful to understand the internals of the encoder. CLK and DT give you the rotation.
On  Bigdanzblog i found some fine tuning of the programming code
The bottom of the page  http://arduino.stackexchange.com/questions/16365/reading-from-a-ky-040-rotary-encoder-with-digispark has another code example.
On Hobbycomponents was the info that finaly gave finally me a clue how to use this little device. SW is a push switch. It is not used is some of the other examples as this functionality is not always present. The program also gave me some understandable data. Not every little movement gives me data and i need to rotate the knob several steps to get a signal.
To make it more robust more development is needed.
Some of the examples use interrupts. There are some other things that i did not try yet:

On codebender.cc there are some program examples using an analog input. 
There are examples on the internet using a 0,47µ capacitor from ground to CLK and another 0,47µ capacitor from ground to DT.


It seems that there are several a little different versions of the encoder. The push button SW is not present in all versions. And 10 KiloOhm pullup resistor from +5V to SW needs to be added to sometimes as it seems not included in all versions of the KY-040. My version of the KY-040 Rotary Encoder module contains 3 resistors on the PCB and has the push button function. Therefore i did not need extra parts. However adding additional 0,47µ capacitors could perhaps improve my experience with this KY-040 module.

First example

Example that worked for me. 

Hardware connection

  • Pin 2 DT
  • Pin 3 CLK
  • Pin 4 SW
  • +5V  +


Watch output on the serial port of the Arduino.
Number increment or decrement depending on rotation direction of knob.
Text "Switch pressed" if knob is pushed.
Not all rotation movement of knob is detected.
To really use the KY-040 Rotary encoder more development is needed. 

No comments: