Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 19 of 19

Thread: Mux encoders?

  1. #1
    Senior Member
    Join Date
    Nov 2016
    Posts
    179

    Mux encoders?

    Hi

    I have to poll way too much encoders so I slowly run out of inputs. I asked myself if there is a way to mux them like I mux e.g. switchbuttons with a 4051?
    I have found this: https://massimoriggi.files.wordpress...encoder_bb.png .. ans tried it out, but somehow it does not work.

    Can some suggest a way to mux encoders?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,322
    The easiest "mux" is using a Teensy LC to read several encoders and send the data as serial or I2C communication.

  3. #3
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by PaulStoffregen View Post
    The easiest "mux" is using a Teensy LC to read several encoders and send the data as serial or I2C communication.
    hi,

    hmmm.. I don't know if this could be a problem in the future.. I already need to power a Teensy3.2 and a RaspberryPi from the same source .. if I now add a Teensy LC.. could this lead to problems?

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    461
    There was a discussion with a working solution recently:
    https://forum.pjrc.com/threads/51002...light=encodersre

  5. #5
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by luni View Post
    There was a discussion with a working solution recently:
    https://forum.pjrc.com/threads/51002...light=encodersre
    BRO!!!!! .. THIS MIGHT SAVE MY LIFE !!! )) thank you!!!

    I will try this out as soon as I am at home. But I wonder about this code here:
    Code:
    void readInputs(uint8_t i) {
      selectAddress(i);
      delayMicroseconds(50);
      readEnc(i);
    }
    does this delay have to be there? delayMicroseconds(50);

    I would rather have no delays.. can I use millis() here?

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    461
    I didn't write the linked code but I assume that the delay is meant to wait for the switching time of the used mux but 50Ķs seems to be quite long. Look in the datasheet of the port extender you intend to use, it probably is much faster.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,322
    Quote Originally Posted by AdmiralCrunch View Post
    does this delay have to be there? delayMicroseconds(50);
    Yes, a delay is always required after switching a mux, because the mux output takes time to change. How much time is a good question. 50 us may be overly conservative. With rotary encoders, the time will depend on the pullup resistors. If you want a faster time, use really pullup resistors between 1K to 3.3K. The ones built into the chip are much higher impedance, so the signal takes longer to change.

  8. #8
    Senior Member
    Join Date
    May 2017
    Posts
    208
    In the linked code, you know what the next value of the variable 'i' is going to be, so you could change that readInputs function to set up the mux address to the next input to be read and reduce or eliminate the delay needed.

    Code:
    void readInputs(uint8_t i) {
    
      //delayMicroseconds(50);
      readEnc(i);
      selectAddress((i+1)&7);     // only works for 8 inputs
    }

  9. #9
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by PaulStoffregen View Post
    Yes, a delay is always required after switching a mux, because the mux output takes time to change. How much time is a good question. 50 us may be overly conservative. With rotary encoders, the time will depend on the pullup resistors. If you want a faster time, use really pullup resistors between 1K to 3.3K. The ones built into the chip are much higher impedance, so the signal takes longer to change.
    thanks you .. will try this out

    Quote Originally Posted by rcarr View Post
    In the linked code, you know what the next value of the variable 'i' is going to be, so you could change that readInputs function to set up the mux address to the next input to be read and reduce or eliminate the delay needed.

    Code:
    void readInputs(uint8_t i) {
    
      //delayMicroseconds(50);
      readEnc(i);
      selectAddress((i+1)&7);     // only works for 8 inputs
    }
    cool dude .. this is a very nice idea .. will check this out

  10. #10
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    guys, this works perfectly!! ) thank you so much

    maybe one last question.. how would I decouple this? .. a capacitor to the 4051? or 3k3 resistors from each pin to 5V as pull-ups and one 10nF capacitor from each pin to ground for de-bouncing?

  11. #11
    Senior Member
    Join Date
    Apr 2017
    Posts
    131
    I would recommend looking at the manual for the encoders that you have, they usually have their own recommended schematic for this.

  12. #12
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by vjmuzik View Post
    I would recommend looking at the manual for the encoders that you have, they usually have their own recommended schematic for this.
    hi, .. I did .. unfortunately for me as a total noob, this is all chinese to me.. :/
    I know that this must be decoupled but i wonder if I should decouple the ICs`? or the encoders itself? or both?

  13. #13
    Senior Member
    Join Date
    Apr 2017
    Posts
    131
    This is more or less the filter circuit that my encoders use. I didnít design that part of the board, but it looks very similar. The board was part of a old Open Labs controller before they went a different direction and stopped making and supporting their controllers. They never released 64 bit drivers for this controller so I repurposed it. I didnít decouple any of the 4051ís on my board, but I canít say wether you should or not I just know mine has worked pretty solid so far.
    Click image for larger version. 

Name:	B63AF000-CA4C-4C39-8169-BF14A49C7B02.jpeg 
Views:	23 
Size:	165.4 KB 
ID:	13654

  14. #14
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by vjmuzik View Post
    I didnít decouple any of the 4051ís on my board, but I canít say wether you should or not I just know mine has worked pretty solid so far.
    I use this Encoders here: https://www.amazon.de/gp/product/B00...?ie=UTF8&psc=1
    .. when i used directly on my Teensy, without a 4051, I had inconsistent values.. then I read about decoupling and did it like
    "one 3k3 resistor from each pin A/B to +3.3V as pull-ups and one 10nF capacitor from each pin A/B to ground for de-bouncing".. and this worked perfectly..

    but now I mux them with the 4051s and again I deal with this inconsistent values.. and now I'm asking myself, if I should decouple on the IC-Pins where the Encoder is connected(?) or decouple the IC itself(?)... or both?

  15. #15
    Senior Member
    Join Date
    Apr 2017
    Posts
    131
    Quote Originally Posted by AdmiralCrunch View Post
    I use this Encoders here: https://www.amazon.de/gp/product/B00...?ie=UTF8&psc=1
    .. when i used directly on my Teensy, without a 4051, I had inconsistent values.. then I read about decoupling and did it like
    "one 3k3 resistor from each pin A/B to +3.3V as pull-ups and one 10nF capacitor from each pin A/B to ground for de-bouncing".. and this worked perfectly..

    but now I mux them with the 4051s and again I deal with this inconsistent values.. and now I'm asking myself, if I should decouple on the IC-Pins where the Encoder is connected(?) or decouple the IC itself(?)... or both?
    Two questions.
    1. Did you make sure to disable interrupts for the encoders?
    2. How long of a delay did you use after switching the mux address?

  16. #16
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by vjmuzik View Post
    Two questions.
    1. Did you make sure to disable interrupts for the encoders?
    2. How long of a delay did you use after switching the mux address?
    Hi,

    1) ehm.. no .. oO .. I did not even know that one could do that(?) oO .. how would I do this?
    2) no delay.. I use rcarr's suggestion for the code..

    Code:
    void readInputs(uint8_t i) {
    
      //delayMicroseconds(50);
      readEnc(i);
      selectAddress((i+1)&7);     // only works for 8 inputs
    }

  17. #17
    Senior Member
    Join Date
    Apr 2017
    Posts
    131
    Quote Originally Posted by AdmiralCrunch View Post
    Hi,

    1) ehm.. no .. oO .. I did not even know that one could do that(?) oO .. how would I do this?
    2) no delay.. I use rcarr's suggestion for the code..

    Code:
    void readInputs(uint8_t i) {
    
      //delayMicroseconds(50);
      readEnc(i);
      selectAddress((i+1)&7);     // only works for 8 inputs
    }

    At the very top of your code before the encoder library is defined you have to
    Code:
    #define ENCODER_DO_NOT_USE_INTERRUPTS
    And if that still doesnít work you can try to add some delay and see if that fixes it.

  18. #18
    Senior Member
    Join Date
    Nov 2016
    Posts
    179
    Quote Originally Posted by vjmuzik View Post
    At the very top of your code before the encoder library is defined you have to
    Code:
    #define ENCODER_DO_NOT_USE_INTERRUPTS
    And if that still doesn’t work you can try to add some delay and see if that fixes it.
    aaaah okay,.. thank you ) .. I will try this out

Posting Permissions

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