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

Thread: Using 5v Rotary Encoders with Teensy 4.1

  1. #1

    Using 5v Rotary Encoders with Teensy 4.1

    Hello,

    I'm currently working on a midi controller sketch that needs to use some rotary encoders.

    I ended up going with the Alps EC12E24104A6, 24 pulse no detents, as I like the feel. Data sheet https://www.mouser.com/datasheet/2/15/EC12E-1370769.pdf

    What is the best way of hooking these up to the digital pins on the Teensy4.1 since the digital pins are not rated to 5v, which the encoders are?

    For a quick test, I decided to just try hooking up the encoder's A pin to Digital 0 and B pin to Digital 1. I set each of those pins to PULLUP, but I have had mixed results when checking it with Digital Read. Sometimes it appears to be enough to pull the data pin down to ground, others... not so much.

    I'm sure I'm overlooking something simple here, but I'm so used to just hooking these up to 5v with a 1k pullup and calling it a day.

    Thanks!

  2. #2
    Senior Member Rolfdegen's Avatar
    Join Date
    Sep 2020
    Location
    Germany
    Posts
    324
    I use teensy encoder library..
    For fast rotation use 10nF capacitor.




  3. #3
    Are you using the internal pullups in addition to the 10k pull ups, or just sticking with the physical components for pullup and decoupling?

    With the encoders being rated to 5v, I havent tried hooking them up to the 3.3v, but I dont see there being a problem since they are only pulling the pins down to ground anyways.

    Thanks for the clear diagram!

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    2,056
    Quote Originally Posted by Fizzbopgalore View Post
    I'm sure I'm overlooking something simple here, but I'm so used to just hooking these up to 5v with a 1k pullup and calling it a day.
    If you don't have a harsh environment, you can simply connect the C pin to GND and directly connect A/B to the Teensy. Just use pinmode(INPUT_PULLUP) for the pins. No need for resistors or capacitors. The quadrature signal is "self debouncing" if read out correctly (you can use e.g. the standard encoder.h library (included in Teensyduino) or the encoderTool which provides a few more convenience functions (https://github.com/luni64/EncoderTool)

    Here an example how to use the EncoderTool:
    Code:
    #include "EncoderTool.h"
    using namespace EncoderTool;
    
    Encoder encoder;          // interrupt based encoder
    
    void setup()
    {
        encoder.begin(2, 3); // using pins 2 and 3 to connect encoder
    }
    
    void loop()
    {
        if (encoder.valueChanged()) // do we have a new value?
        {
            Serial.println(encoder.getValue());
        }
    }

  5. #5
    Ya, I tried doing that up until now, but it reacts hit or miss.

    I was purely checking to see if the encoder was enough to pull the pin down to 0, just using digital read here, so library agnostic, and the encoder on its own seemed unable to pull the voltage of the pin back down to gnd. Checked the encoder with a meter, and it appears to be fine, to rule out a lemon.

    Additionally, I'm not too confident in my breadboard, so at this point I'm just trying to sort through which component route to go down, as I'll eventually need to have PCBs produced; I'd rather not have to do that portion multiple times. I have been stung a few times where I could have simplified the PCB layout, as you mentioned, when using a clean PCB rather than a breadboard; however, in this scenario, I just do not have the time to really fuss with this too many times.

    Regarding Encoder Libraries, I found this library: https://github.com/blinkworth1/Adria...r/myController for polling based encoders rather than interrupt based. I have taken this as a starting point to go in and re-write my own custom classes and functions to use. There is a lot that I would prefer to keep wrapped in the encoder object itself instead of keeping a bunch of separate variables and functions for each encoder within the ino. It makes it easier for me to keep track of whats doing what, decreases the number of functions in the the actual ino and for the purposes of the controller, it makes it easier for other people to do what they need to on a case by case basis. They only need to instantiate the encoders, reference the calling encoder into the transmit function, and call it a day.

    Currently, I have run into a small hiccup regarding how the linked c++ code passes its callback functions for the SetHandleLeft/Right, but I will finish adapting modifying that tomorrow so I can pass a reference to the individual calling rotary, switch, and fader objects.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    2,056
    was purely checking to see if the encoder was enough to pull the pin down to 0, just using digital read here, so library agnostic, and the encoder on its own seemed unable to pull the voltage of the pin back down to gnd. Checked the encoder with a meter, and it appears to be fine, to rule out a lemon.
    These encoders are just mechanical switches. If you can not pull the pin down, something is probably wrong with the encoder or your wiring.
    Here what I usually do in such cases https://forum.pjrc.com/threads/69895...l=1#post303103

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,944
    Luni is correct. This type of encoder is simply mechanical switches. By itself, it doesn't output a particular voltage at all. It simply connects or does not connect either or both of the signals pins to GND.

    The rated voltage and current shows in the ALPS datasheet should be understood as maximum values ALPS recommends.

    Click image for larger version. 

Name:	screenshot.png 
Views:	8 
Size:	90.6 KB 
ID:	31165

    These ratings do NOT mean the encoder will necessarily output 5 volts or conduct 0.5 mA per pin. It is simply metal parts that do or do not touch. The actual voltage and current depend on the external circuitry you connect. ALPS is advising you to design your circuitry so the voltage is not more than 5 volts and the current is not more than 0.5 mA.

    For an analogy, image a plumbing project you install this high quality ball valve.

    Click image for larger version. 

Name:	valve.jpg 
Views:	0 
Size:	13.1 KB 
ID:	31166

    This valve is rated to 600 psi WOG (water, oil, gas) printed right on the valve! The rating is a maximum possible pressure. It does not mean the pressure will really be 600 psi. The valve doesn't have any control over the actual pressure. It's just metal parts that move to allow or restrict flow. The rating is simply the maximum it's designed to use.

    Likewise with the encoder, it is just metal parts that do or do not touch. It doesn't create any specific voltage or have any specific current. The actual voltage and current depend on the power supply and resistor you connect, just like the pressure and flow rate in a pipe depend on what plumbing you connect. The rating on the part is merely a maximum recommendation which the manfacturer guarantees will work.
    Last edited by PaulStoffregen; 05-22-2023 at 11:57 AM.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,944
    To use this ALPS EC12E encoder with Teensy 4.1, I would recommend connecting terminal C (the center pin) to GND. Then connect each of the other 2 pins to a 6.8K resistor connected to 3.3V. Or if 6.8K is not easily available, 10K should also work. But do not use resistors less than 6.8K. Definitely do not use 1K as mentioned in the original message on this thread, as ALPS rates the encoder for 0.5 mA. If you use a 1K resistor, with 3.3V the current would be 3.3mA, and with 5V the current would be 5 mA, which is 10 times more than rated. (but I would be pretty surprised if running at 10X rated current actually causes any problems)

    For the sake of troubleshooting, I would highly recommend first using a DC voltmeter. Or if you can get 2 voltmeters, even better to connect one to each signal, so you can see both voltages at the same time. As you slowly turn the encoder, you should see the voltages change between 0 to 3.3V.

    Encoders with detents will tend to move 4 states per "click", so it's difficult to see the voltage change since each 4 states end up with the same voltage shown. But if I've understood the ALPS info, EC12E24104A6 is supposed to be a part without the detents and show turn smoothly. It is 24 pulses per revolution, which means each state should be 1/96th of a revolution, or 3.75 degrees. You might need to mount a large knob to the shaft to be able to physically rotate it by such small angles.

    You really should do this voltmeter test before connecting to Teensy, so you can be sure the hardware really is working.

    If it doesn't work, either with the voltmeter, or does work by voltage measurements but then does not work with Teensy, we can try to help you resolve the problem... if let us actually see. Please, do yourself a favor and take some photos to let us see your hardware and wiring. We can help diagnose hardware problems so much better when we can actually see the hardware.

  9. #9
    Quote Originally Posted by PaulStoffregen View Post
    To use this ALPS EC12E encoder with Teensy 4.1, I would recommend connecting terminal C (the center pin) to GND. Then connect each of the other 2 pins to a 6.8K resistor connected to 3.3V. Or if 6.8K is not easily available, 10K should also work. But do not use resistors less than 6.8K. Definitely do not use 1K as mentioned in the original message on this thread, as ALPS rates the encoder for 0.5 mA. If you use a 1K resistor, with 3.3V the current would be 3.3mA, and with 5V the current would be 5 mA, which is 10 times more than rated. (but I would be pretty surprised if running at 10X rated current actually causes any problems)

    For the sake of troubleshooting, I would highly recommend first using a DC voltmeter. Or if you can get 2 voltmeters, even better to connect one to each signal, so you can see both voltages at the same time. As you slowly turn the encoder, you should see the voltages change between 0 to 3.3V.

    Encoders with detents will tend to move 4 states per "click", so it's difficult to see the voltage change since each 4 states end up with the same voltage shown. But if I've understood the ALPS info, EC12E24104A6 is supposed to be a part without the detents and show turn smoothly. It is 24 pulses per revolution, which means each state should be 1/96th of a revolution, or 3.75 degrees. You might need to mount a large knob to the shaft to be able to physically rotate it by such small angles.

    You really should do this voltmeter test before connecting to Teensy, so you can be sure the hardware really is working.

    If it doesn't work, either with the voltmeter, or does work by voltage measurements but then does not work with Teensy, we can try to help you resolve the problem... if let us actually see. Please, do yourself a favor and take some photos to let us see your hardware and wiring. We can help diagnose hardware problems so much better when we can actually see the hardware.
    Thanks Paul, I did not realize that I typed 1k instead of 10k. I was looking to go back and edit the original post with a clarification of the error you pointed out, but if someone reads through the thread, they'll eventually hit this. I completely missed it when I posted. That's what I get for using a cheap wireless keyboard and not double checking.

    Regarding the wiring, I'm going to give it a test today using the above, posted circuitry. I have used these in other ARM based projects in the past, so I do not suspect it will take long to figure out what is going on. I did not post a photo since the circuitry for the encoders is the standard ACB layout—nothing crazy.

    I'll report back with the results to round out the archival of this for future forum-perusers.

    Also, I love how quick and friendly this forum is. Talk about support!

  10. #10
    Update:

    It was indeed a combo of hardware issues.

    1) It looks like the GND on the left side of my test teensy doesnt work as intended. It was purchased without pins, so this is most likely this is due to the soldering connections. Luckily, the other GND on the right side works fine. This is a bespoke test unit, so I'm not too concerned with that for now. I expect the other boards to be fine, as they have not been touched yet.

    2) It was also a combination of the breadboard. I ended up soldering some better connections onto the encoder—to fit the breadboard—and the encoder works as expected if I plug into the Data/GND pins on the row connecting to the Teensy's pins. If I try to patch anywhere else on the board and then connect to the Teensy's pins' rows, it starts throwing a fit again. The nice thing is, I can get away with just using the internal pullup—which is what I was expecting since the encoders are just connecting to GND!

    Using No. 1, I am able to use both polling based libraries and interrupt libraries and fine tune the resolution of the encoder as desired. I think there is time to try having a simple PCB made to rule out any finicky business from the cheap breadboard, so I'll probably give that a swing as well to rule out any hardware anomalies before jumping into any, substantial code.

  11. #11

  12. #12
    Member
    Join Date
    May 2017
    Location
    Edinburgh
    Posts
    20

    5-24v rotary encoders

    Hello there - just thought I'd chip in with related note, and say that I've used a cheap (~4) 8 channel level shifter between teensy 4.1 and the A and B outputs of the relatively cheap (~10, although slowly creeping up in price) optical 5-24V encoders commonly found on ebay/aliexpress, which seems to work pretty well. Common GND, and 5V to encoder from external psu. Obviously not the same as the mechanical encoders, but no detents, the encoder library works very well with them, they're reasonably precise and accurate, and they're fast enough for my midi projects. Just wish they were physically smaller, at 39mm across they take up a fair chunk of space.

    Paul H.

  13. #13
    Senior Member Rolfdegen's Avatar
    Join Date
    Sep 2020
    Location
    Germany
    Posts
    324
    Do you have a picture? Thanks

  14. #14
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,944
    Quote Originally Posted by Fizzbopgalore View Post
    the encoder works as expected if I plug into the Data/GND pins on the row connecting to the Teensy's pins. If I try to patch anywhere else on the board and then connect to the Teensy's pins' rows, it starts throwing a fit again.
    If you're using "breadboard wires" which have little pins crimped onto the ends, you might spend a moment with an ohm meter to check if they really do conduct. Those products tend to be very cheaply made. The conductor inside the wire is incredibly thin and tend to break easily. I personally saw a batch of them some time ago where all the orange ones didn't conduct at all (and the wires which did conduct were more than 1 ohm resistance over just a few inches), so I'm inclined to believe they were poorly made rather than just randomly failed with usage.

    I mostly use #22 solid wire. It's the same wire we include with this tutorial kit. That #22 wire is a lot more expensive than the cheap breadboard wires, but it's highly reliable and makes a good connection in most breadboards.

    We've also see a massive difference in quality between good breadboards and the cheap ones (which almost all maker companies sell). The 2 places we've found with high quality breadboards are Twin Industries and BPS BusBoard. PJRC stocks the BPS BusBoard breadboard.

    A low quality breadboard and especially unreliable wires can really cost a lot of time and frustration. The good quality ones do cost more, but it's really not very much extra money to put yourself onto a path for success.

Posting Permissions

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