Shop OBEX P1 Docs P2 Docs Learn Events
Reading a rotary encoder — Parallax Forums

Reading a rotary encoder

fma38fma38 Posts: 42
edited 2007-08-02 18:57 in BASIC Stamp
For my first attempt to program a BS2 (px), I started with a rotary encoder, which I plan to use for my motorized panohead project.

I used the method described here:

http://www.desktopaviator.com/Articles/Rotary_Encoder/st_ap8.pdf

but it dos not work very well... When I turn the knob, I have false values (turning in CW gives me some CCW values; the same problem occurs in the other direction). It seems that the BS2 is not fast enough. Is it the case ? Or is it my program?

Rotary encoder connected to pin6 and 7...

' {$STAMP BS2px}
' {$PBASIC 2.5}

old          VAR   Nib
new        VAR   Nib

Init:
    old = INB

Main:
    new = INB
    IF new = old THEN Main
    IF old.BIT3 ^ new.BIT2 = 0 THEN CCW
CW:
    DEBUG CR, "DEBUG: CW"
    GOTO Cont
CCW:
    DEBUG CR, "DEBUG: CCW"
Cont:
    old = new
    GOTO Main


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Fr

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-01 15:57
    Here are the two programs I use to handle reading a rotary encoder on the BASIC Stamp. I hope this helps. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • fma38fma38 Posts: 42
    edited 2007-06-01 16:35
    Thanks!

    My encoder is not continuous, but has some 'clicks'. At a click position, outputs pins are not connected to the common pin. And between 2 clicks, it seems that there are 4 transitions...

    So, I divided the count value by 4, and it works better. I get one increment per click, and much less wrong values (except when I turn it very fast). Maybe without the DEBUG command, things will work better.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Fr
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-01 16:43
    I have a little tutorial on different methods posted here:
    www.emesys.com/BS2fsm.htm#twobit

    The code you posted is slowed down terrifically by the DEBUG statements. If you want a faster indication, install leds on a couple of pins, say red for CCW, green for CW.

    It is true that the Stamp might not be fast enough, when you are able to spin the control fast by hand. The processor does have to catch every single transition of state, or else it will report incorrectly. Even if you remove the DEBUGs, there might be other code that you need this system to execute. The encoder counting might have to be offloaded to a peripheral chip, like an SX chip dedicated to position encoding, or run on a Propeller that can dedicate a whole cog to the encoder and keep up an any speed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-01 16:52
    Tracy,

    I should have mentioned that the DEBUG statements are only there to show the proof of concept. Using this code with several projects there are no DEBUG statements, although they are all BS2p or BS2px-based projects. Even then turning the knob extremely fast results in lost counts. But in the applications they’re in I think the BS2p and BS2px are quite suitable. It really depends on how many pulses/revolution the encoder has too. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • fma38fma38 Posts: 42
    edited 2007-06-01 17:13
    There is a nice circuit for such encoders:

    www.lsicsi.com/pdfs/LS7366.pdf

    But if I can avoid it...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Fr
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-06-01 17:28
    This question comes up from time to time. Awhile back, I posted some coprocessor code for the MoBoStamp-pe that will handle quadrature encoder tracking in the background. All the BASIC Stamp has to do is read the internal counter.

    -Phil
  • jeffjohnvoljeffjohnvol Posts: 197
    edited 2007-06-01 22:24
    I had a 100 ppr encoder and my bs2px had difficulty reading all the transitions. As it would normally go 1-0-2-3 1-0-2-3, I found that it would skip some if it rotated too fast (just with my fingers, more than 2 revolutions per second). I ended up going with an absolute encoder, but I may look at some of the other source examples to see if those might work.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-01 22:53
    Yeah, the Grayhill I use has 32 ppr. I have posted the part number before. Digikey carries it and it even has a push button built into the shaft.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • jeffjohnvoljeffjohnvol Posts: 197
    edited 2007-06-02 00:38
    What was the push button for?· Did it have a z channel? (extra pulse per rotation)
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-02 04:12
    The button is for when you use the encoder to enter information…For example, I am using these to alter parameters on an Audio Controller. When I select a parameter I use the button by pushing in the knob. Then I rotate to the value I want and press the knob to go to the next parameter. You could use it in a similar way for menus. Rotate to select a menu then press the knob in to select that option. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • fma38fma38 Posts: 42
    edited 2007-06-02 10:26
    I think that UI using encoder + push button are really great to use (if menus are well thinked). I loved my Nikon F801-S for that reason.

    I found an other nice circuit to decode quadrature encoders:

    www.lsicsi.com/pdfs/Data_Sheets/LS7083_LS7084.pdf

    I guess the second one is better, because it provides pulses and direction. With both, even if you miss a pulse, it does not go back!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Fr
  • ProfessorwizProfessorwiz Posts: 153
    edited 2007-08-02 18:22
    One quick question Chris, in your program you state this code will work on the BS2e, BS2SX, BS2P24, BS2P40, BS2pe, and BS2px24. Does it not work with the standard BS2 unit?

    Thanks,

    Russ
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-08-02 18:57
    Russ,

    The code mentions it works on the BS2 before mentioning the other BASIC Stamp Models. I guess I should have said 'also' works on...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.