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!
 
I use teensy encoder library..
For fast rotation use 10nF capacitor.


Enc.png

Teensy.png
 
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!
 
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:
[size=3][color=#000000][/color][color=#000099]#include[/color] [color=#000099]"EncoderTool.h"[/color][color=#000099][/color]
[color=#000000][/color][color=#001177]using namespace[/color] [color=#000000]EncoderTool[/color][color=#000000];[/color]
[color=#000000][/color]
[color=#000000]Encoder encoder[/color][color=#000000];[/color]          [color=#000000][/color][color=#0b810d]// interrupt based encoder[/color]
[color=#000000][/color]
[color=#000000][/color][color=#0000ff]void[/color] [color=#000000][/color][color=#000000][b]setup[/b][/color][color=#000000][/color][color=#000000]()[/color]
[color=#000000][/color][color=#000000]{[/color]
[color=#000000]    encoder[/color][color=#000000].[/color][color=#000000][/color][color=#000000][b]begin[/b][/color][color=#000000][/color][color=#000000]([/color][color=#000000][/color][color=#a52a2a]2[/color][color=#000000][/color][color=#000000],[/color] [color=#000000][/color][color=#a52a2a]3[/color][color=#000000][/color][color=#000000]);[/color] [color=#000000][/color][color=#0b810d]// using pins 2 and 3 to connect encoder[/color]
[color=#000000][/color][color=#000000]}[/color]
[color=#000000][/color]
[color=#000000][/color][color=#0000ff]void[/color] [color=#000000][/color][color=#000000][b]loop[/b][/color][color=#000000][/color][color=#000000]()[/color]
[color=#000000][/color][color=#000000]{[/color]
[color=#000000][/color]    [color=#001177]if[/color] [color=#000000][/color][color=#000000]([/color][color=#000000]encoder[/color][color=#000000].[/color][color=#000000][/color][color=#000000][b]valueChanged[/b][/color][color=#000000][/color][color=#000000]())[/color] [color=#000000][/color][color=#0b810d]// do we have a new value?[/color]
[color=#000000][/color]    [color=#000000]{[/color]
[color=#000000]        Serial[/color][color=#000000].[/color][color=#000000][/color][color=#000000][b]println[/b][/color][color=#000000][/color][color=#000000]([/color][color=#000000]encoder[/color][color=#000000].[/color][color=#000000][/color][color=#000000][b]getValue[/b][/color][color=#000000][/color][color=#000000]());[/color]
[color=#000000][/color]    [color=#000000]}[/color]
[color=#000000][/color][color=#000000]}[/color][color=#000000][/color][/size]
 
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/Adrian_myController13/tree/master/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.
 
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/6989...encoder-issues?p=303103&viewfull=1#post303103
 
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.

screenshot.png

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.

valve.jpg

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:
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.
 
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!
 
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.
 
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.
 
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.
 
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.

Can you share the part number of the level shifter?
 
Can you share the part number of the level shifter?

Hiya, it doesn't seem to have one, identical looking ones ones, with the same red pcb, on ebay, eg

item 392995725837, China, or 173404294539, UK

although the one I used has qifei written on the underside. Not much point in sending a pic of mine in enclosure, just see a mass of wires :). Simple to wire up, though - 5v to Hv, 3.3v to Lv, GND to GND, A and B outputs from the encoder to any two high side pins, I used HV1 and HV2, and then the corresponding low side pins, ie LV1 and LV2, to pins on teensy - I used pins 29 and 30 for this particular one. Cheap and dirty, with no mounting holes, but seems to work ok. Just stuck it onto board with a self-adhesive pad. 5V/GND to encoder from either external psu or teensy, provided GND is is commoned, of course. My concern was frying the teensy with a 5V pulse from the encoder, all these little boards do is just drop it down in voltage. Bi-directional - also said that you can put in a 3.3V signal on the LVx pin and get a 5V signal on the corresponding HVx pin.

Got mine a little while back. I bought a bunch of small components on ebay for potential use at some point from China deliberately, mainly to see what the drawbacks/delays would be for import to the UK, and for my partners curiosity about the potential issues of vat/import duties (she's a bookkeeper). Ever since the chancellor removed the £18 threshold, I've been far more reluctant to buy things from outside the UK on a whim or impulse. Came through OK in around 2 weeks, with a VAT reciept from ebay, no less, so figure that ebay have automated that process on purchase. Haven't bought anything from aliexpress for a while for the same reason, although I suspect that that will not be as smooth.....

Paul H.
 
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.

Just figured I would reply back here to round out the original problem/discussion.

1) Everything worked fine with the cleanly soldered Teensy's, not the butchered test one, which I was suspecting form the beginning. Code is purring away, and everything worked like a charm on the final PCBs.

2) The problems I was having during the testing phase were entirely due to the cheap breadboard. Someone ordered cheap ones, which still are not exactly 'cheaper' than a decent one, which were effectively useless. I still am confused why people are willing to fight so hard over saving a few pennies or dollars when those savings are negligible compared to the headaches of just buying the proper thing to begin with. I pitched them and ordered them several from Twin Industries, If I remember correctly. Wouldve been easier if they had asked before this whole thing started, but whatever.

3) I didnt even bother using the cables from the breadboard kit. I was able to find a spool of #22, which is what I soldered everything too, since I was crunched for time.
 
Back
Top