KY-040 rotary encoder?

john2051

New Member
Hi, I notice there are quite a few threads regarding rotary encoders, except the ones I have!
The part number is KY-040. It looks relatively straightforward, but I just curious if anyone else has used them.
Hopefully the datasheet has attached ok.
Thanks, john
 

Attachments

eggdweather

Senior Member
So using e.g. Channel-A as reference, wait for it to go high (low to high or 0 to 1) then read Channel-B and if channel-B is low it's rotating CW and if high CCW.
 

cachomachine

Senior Member
Not exactly,
CW: chanel A=0, chanel B=0 (start)
chanel A=1, chanel B=0
chanel A=1, chanel B=1
chanel A=0, chanel B=1
chanel A=0, chanel B=0 (completed)

For CCW, start at the bottom and go up

Sorry for the french comments
 

Attachments

eggdweather

Senior Member
No need to detect a sequence, just detect a negative or positive edge on your chosen reference channel, then measure the other, if CW other channel will be 1 and CCW will be 0.

These units can be easily detected using interrupts or to feed a D-Type Flip-flop CLK=Channel-A and D=Channel-B, then Q = direction of rotation.
 

cachomachine

Senior Member
Do you have a basic program to show us?
what if you back up in the middle of the sequence? (turn opposite way before both chanel are high)
I must admit that i do not follow you
 

cachomachine

Senior Member
A D flip-flop will only determine the sense of rotation but will not count clicks.
The KY-040_rotary encoder produce 14 clicks per full 360 degrees rotation that can be easily monitored by a 08M2
The module in question includes the pull-up resistors and the switches debounce is done by the Picaxe, why bother with a D flip-flop?
 

eggdweather

Senior Member
No need for a discrete D-Type, I was merely illustrating that the D-type process can be easily replicated in software and even more efficiently using the PICAXE interrupts. e.g. Set the interrupts to trigger on either a positive or negative going transition of Channel-A, then in the interrupt routine read Channel-B and if its high rotation direction is CW and low CCW. Set the interrupts/clear the flags and Return.

In this forum Hippy has condensed this process down to a few lines of code, easy to copy that code and implement.
 

john2051

New Member
Apologies for taking ages to reply, we have been waiting for yet another virgin super hub 2! We are now on our third in just over a year. In answer to your
question, yes the encoders I got look identical to the one shown in your link.
Its good to see some sensible price encoders, some places are still asking over a hundred pounds.
regards john
 

gm28lab

New Member
Hi,
I was looking for the HW response time of X2 PiCaxe on the forum, I found your thread.
Here is a small contribution that is not mine.
Years ago, I found a very clever small snippet for a rotary encoder that reacts on each transition for each of 2 wires (written for a 08M2). I do not took note of the name of the author of this piece of software, unfortunately. Thanks to him!
Here it is:
=========================
do
b1=b0
b0=pins & %11
loop until b0<>b1
b1=b1&%10/2^b0&%1
if b1=1 then
'encoder rotated one click clockwise
else
'encoder rotated one click anti-clockwise
endif
loop
===========================
Hope that helps!
 

eggdweather

Senior Member
Good find, that code in (D-Type Flip-flop speak) waits for a edge transition on the clock (CLK) input b0 then reads the data (D) from which it determines direction, some really neat code.
 

eggdweather

Senior Member
Code:
do
  do
    b1=b0
    b0=pins & %11
  loop until b0<>b1

  b1=b1&%10/2^b0&%1
  if b1=1 then
    'encoder rotated one click clockwise
  else
   'encoder rotated one click anti-clockwise
  endif
loop
 
Last edited:
Top