how to read ky40 mechanical encoder ?


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326

    Default how to read ky40 mechanical encoder ?

    Good day to all of you in this forum.
    I used an optical encoder in the past with pbp and I had good result.
    Now I have a mechanical encoder similar to the ky40 or ky-040 that is widely available on the net.
    I am not able to make it working correctly and still having bad results.
    I did filtered the A and B signals with two 0.1uf and 10k resistors pull_up: no solution up to now.
    Any help on the this matter please ? Or any sample code available ?
    Thanks
    Ambrogio

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    Is your encoder like this ?
    http://www.ebay.com/itm/400964660325...%3AMEBIDX%3AIT

    These encoders are (supposedly) destroyed by pullup resistors.

    Or is your encoder just a noisey old mechanical switch type?
    Comparators are supposed to be good buffers for mech switches.

    OK I found my answer, Mech. as stated in your post
    You are needing to "debounce" these.
    Last edited by Archangel; - 11th October 2015 at 21:21.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    this is my ecoder:
    http://www.ebay.com/itm/1PCS-KY-040-...item27e8f12b89

    Any help , please ?
    Thanks
    Ambrogio

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    CAUTION: UNTESTED CODE
    Code:
    main:
    
    A var portb.6
    B var portb.7
    state var byte
    if(( A == 1)&&(B==1)) then state = 1
    if(( A == 1)&&(B==0)) then state = 2
    if(( A == 0)&&(B==1)) then state = 3
    if(( A == 0)&&(B==0)) then state = 4
    ;pause 100
    if state = 1 then portC = %00000001
    if state = 2 then portc = %00000010
    if state = 3 then portc = %00000100
    if state = 4 then portc = %00001000
    
    debug "  state = ",#state
    
    
    goto main
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,390


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    I found this interesting.
    http://makeatronics.blogspot.it/2013...ture-with.html

    is your "re " a 20 count per turn with indents ? the 10 for a $1 ones I got from ebay have enough contact bounce to start a spring factory but the inherent software filtering of the above code does an amazing job on them 1 (4k7 pu r's and .1uFs included). I have not ported this to pbp but have for mikro c and xc8 if that helps

    http://www.circuitsathome.com/mcu/re...age-1#comments
    this link has some other insights

    basically you just need to adjust your re count using this current value / old value matrix (illegal values [bounces] are just ignored )
    Attached Images Attached Images  

  6. #6
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    One of possible ways to implement filter(Not tested)
    Code:
    OutputValue=0
    Select Case OldValue
    Case 0
    If Current=1 then OutputValue=-1
    If Current=2 then OutputValue=1
    Case 1
    If Current=3 then OutputValue=-1
    If Current=0 then OutputValue=1
    Case 2
    If Current=0 then OutputValue=-1
    If Current=3 then OutputValue=1
    Case 3
    If Current=2 then OutputValue=-1
    If Current=1 then OutputValue=1
    EndSelect

  7. #7
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    still not able to make my panel encoder working .
    Could someone post the complete code applicable to mechanical encoder like my one please ?
    Thanks in advance.
    regards,
    Ambro

Similar Threads

  1. Question about mechanical relay
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st October 2012, 10:37
  2. Mechanical question
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 11th October 2011, 05:41
  3. Probe to read quadrature encoder with DT_INT need help
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 31st August 2009, 20:43
  4. Mechanical question
    By selbstdual in forum Off Topic
    Replies: 10
    Last Post: - 8th February 2008, 14:34
  5. Replies: 6
    Last Post: - 14th October 2005, 19:41

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts