Counting the number of rotations...Help!

Discussion in 'Embedded Systems and Microcontrollers' started by indeedisuper, Oct 26, 2015.

  1. indeedisuper

    Thread Starter New Member

    Oct 14, 2015

    I was wondering if anyone has a good idea on how to count the number of rotations that an absolute rotary encoder went through.

    For example, if the encoder goes through a whole revolution clockwise, a rotation counter increments, and if it goes 1 revolution counter clockwise, the counter decrements.

    I'm having a hard time figuring out a method to identify whether the encoder is going clockwise or counterclockwise, and if so if it has been a full rotation or not.

    I'm thinking of setting up a marker, say if the last five readings were smaller than this marker then the encoder is moving clockwise. If the last five readings were bigger than the marker, than the encoder is going counterclockwise. But there are cases where this would not catch.

    Any insight is appreciated.
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    I think the last time I worked with dual direction encoder, it had two outputs. One output for one direction, another output for the other direction.

    Do you have encoder with just one output?
  3. MaxHeadRoom


    Jul 18, 2013
    An absolute encoder (as opposed to quadrature) usually either has serial output or 8bit 12 bit parallel.
    The one shown appears to have analogue or PWM output and I have never used the one shown.
    If the analogue out is used, then input into a Pic or other μp.
    What is the method used to read it?
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    I'd never seen that component before. It's an interesting thing.

    Since this is the Embedded Systems and Microcontrollers section, I assume you plan to use a processor. What I'd do is read the transducer output at some rate, frequent enough that you'd get several results during each turn of the shaft at maximum speed. Every time you get an input, subtract the previous reading (N) from the latest reading (N'). Do a 5-way test on the result, like this (assume the scale is 0-99):

    (N' - N) = 0: No motion, do nothing
    0 < (N' - N) < 50: Clockwise, don't change rotation count
    (N' - N) >= 50: Counter-clockwise, decrement rotation count
    0 > (N' - N) > -50: Counter-clockwise, don't change rotation count
    (N' - N) <= -50: Clockwise, increment rotation count

    The point is that the device gives you a number that (going "clockwise") increments slowly to the maximum and then jumps to zero. In the "counter-clockwise" direction, it decrements slowly from the maximum and then jumps back to the maximum. You compare each reading with the previous one, and if the difference is more than 49, you assume that there was that sudden jump from max to min or vice versa, and that's when you increment/decrement the turns count. Smaller changes don't indicate a sudden jump and just tell you the position within a turn.

    If I haven't made it clear enough, sketch out a sawtooth waveform on a piece of paper and run your finger along it. You can count cycles by detecting the jumps from maximum to minimum, or the opposite.
  5. indeedisuper

    Thread Starter New Member

    Oct 14, 2015
    Max is right. The encoder that I am using is the PWM version. I read it via a ISR that stores the rise/fall time whenever a pin change happens. I later convert the rise/fall time to pulse width and can figure out the appropriate rotation/distance from there.

    The reason why I need to figure out how many rotations the encoder has gone through is because my MCU isn't fast enough to catch all the pin changes when the high or low pulse width is less than 2us which occurs right before/after a rotation through the 0 degree point. As of now my encoder reading's absolute accuracy is off so I need to adjust for the error during every rotation.
  6. indeedisuper

    Thread Starter New Member

    Oct 14, 2015

    Hi John,

    This was similar to the thought that I had in the beginning, but due to the fact that I have a blind spot from ~2500 to 65 (the range for the encoder in ticks is from 0 to 2565) because of my MCU I am looking into other ways.
  7. indeedisuper

    Thread Starter New Member

    Oct 14, 2015
    Just figured it out using the blind spot to my advantage. Thanks for the input!