Teensy 4.0 and EC11E15244B2 encoder issues

Vakarian

New member
Hello,
my first post here, but have been lurking for couple of weeks and your posts have been most helpful in aiding me to complete the design for my project. I've been building a controller box for DCS and Teensy 4 is the brains, couple of 16 channel multiplexers for the buttons, couple of potentiometers for radio control and 2 encoders.

Now, I've been able to make standalone sketches that work just for reading buttons from multiplexers and another to read pots. Just as a proof-of-concept on a breadboard. When it comes to encoders, I've had 0 success for last 2 weeks, I'm out of ideas what have I been doing wrong and it's starting to really frustrate me. Encoders should be simple devices and with the plethora of libraries available I didn't expect I'll have issues, but here I am.

I've been using the example sketch that comes with Teensyduino IDE and here is the code and result. I have connected the encoder and just spun it in single direction for a full turn or two. Only thing I get as a result is alternation between 0 and 1. Because of that, I can't figure out did I turn encoder left or right and as a result I can't bind that to button output.
Code:
/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(1, 3);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

The result:

Code:
Basic Encoder Test:
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
... you get the idea

I have tried numerous libraries (Encoder_Polling, EncoderStepCounter, NewEncoder, QuadEncoder) with all their examples and the result was the same. If I got some result, I got this alternation result and that's it. It even doesn't matter in which direction I turn the encoder, the alternation just continues. I would expect the encoder to start counting if I just turn it in one direction, but this doesn't happen.

The end result that I'd like is just to be able to recognize did I turn encoder left or right and assign those to button outputs as I'll be using the Teensy in Joystick mode.


Connection scheme:
Encoder pin A -> Teensy pin 1
Encoder pin B -> Ground
Encoder pin C -> Teensy pin 3

There is also a 10kOhm resistor between Teensy pins 1 and 3 and 3.3V
I've attached a picture of that too.

Also, as I have bought 5 of those, I did try to put all of them in the breadboard and the result was identical between the encoders. So, either I got the failed batch or there is something I just fail to understand with this kind of encoder.

Oh, one more thing that I've noticed and could help in resolving this. I have noticed that sometime I get 5-6 lines in the output for just one detent in a turn. Don't really understand what that means.

So if anyone can help me in achieving this, I would be really grateful as this is last thing that's barring me from continuing on the project.
 

Attachments

  • 20220326_012104.jpg
    20220326_012104.jpg
    131.3 KB · Views: 32
The connection you described in your connection scheme is definitely wrong and can't work. You need to connect the channels A and B to the input pins 1 and 3 and the common pin C to GND. However, from your picture it does not look like you connected the encoder as you described. The blue wire (GND?) correctly goes to the middle pin C of the encoder. But, it might be that you have a short between the green and the blue wire.

The output pattern you showed looks like one of the channels either has no connection to the encoder (broken cable?) or it is tied to GND by something like a short. Your soldering looks not very good, could also be that you have a "cold" joint, i.e., no connection. (See e.g. here https://learn.adafruit.com/adafruit-guide-excellent-soldering/common-problems for some soldering tips)

Oh, one more thing that I've noticed and could help in resolving this. I have noticed that sometime I get 5-6 lines in the output for just one detent in a turn. Don't really understand what that means.

The encoder you are using generates 4 quick state changes between the detents. Depending on the used encoder library and the user readout code you will see them as rapid counts. As soon as your hardware works you will notice that with some of the encoder libraries the encoder count advances by 4 if you move it one detent. Actually it will not do one step by 4, but four fast steps in between two detents. Additionally, the mechanical encoder switches will bounce. I.e., for some short time (few ms) the mechanical contacts will jump up and down and generate a rapid series of up/down counts. Good encoder libraries (e.g. the standard encoder library) ensure that you will end up with the right value. Encoder libraries made to handle mechanical encoders can get rid of these rapid up/down counts during bouncing as well. (e.g. https://github.com/luni64/EncoderTool)


ADDITION:

Here some simple test code showing what happens at your pins which might be useful to fix any hardware issue:
Code:
#include "Arduino.h"

constexpr unsigned pinA = 0;
constexpr unsigned pinB = 1;

void setup()
{
    pinMode(pinA, INPUT_PULLUP);
    pinMode(pinB, INPUT_PULLUP);
}

unsigned oldA, oldB;

void loop()
{
    unsigned A = digitalReadFast(pinA);
    unsigned B = digitalReadFast(pinB);
    if (A != oldA || B != oldB)
    {
        Serial.printf("A:%d B:%d\n", A, B);
        oldA = A;
        oldB = B;
    }
}

Prints here for one detent:
Code:
A:1 B:1
A:1 B:0
A:1 B:1
A:1 B:0
A:1 B:1
A:1 B:0
A:1 B:1
A:1 B:0
A:1 B:1
A:1 B:0
A:1 B:1
A:1 B:0
A:0 B:0
A:1 B:0
A:0 B:0
A:1 B:0
A:0 B:0
A:1 B:0
A:0 B:0
A:0 B:1
A:1 B:1
(this also shows the mentioned bouncing effect)


And here example code showing how to use the EncoderTool (note: this will not work until you fixed your hardware issue)

Code:
#include <EncoderTool.h>
using namespace EncoderTool;

Encoder myEnc;

void setup()
{
    myEnc.begin(0, 1);
}

int oldVal = 0;

void loop()
{
    if (myEnc.valueChanged())
    {
        int val         = myEnc.getValue();
        const char* dir = val > oldVal ? "up" : "down";
        oldVal          = val;

        Serial.println(dir);
    }
}

Or, simpler if you want to use the callback functionality of the library:

Code:
#include <EncoderTool.h>
using namespace EncoderTool;


Encoder myEnc;

void onValueChanged(int value, int delta )
{
    Serial.println(delta > 0 ? "Up" : "Down");
}

void setup()
{
    myEnc.begin(0, 1, onValueChanged);
}

void loop()
{
}

Both versions print "Up" or "Down" whenever the encoder is moved.
 
Last edited:
Luni, thanks a lot.

It's been late when I wrote it, and I see I made a mistake in the connection write up. The middle pin is channel C and it's connected to ground as per EC11 datasheet, outer pins are A and B and those were connected to Teensy 1 and 3 pins.

What did fix the issue is to go away from soldering the leads on the encoder and just trying to jam it in the breadboard. With the breadboard I have it's a bit harder then I'm used to to plug stuff in, so encoder with its short legs is having some difficulty, but I managed to connect it to breadboard and connect it to Teensy from there. With a connection like that it finally works. Thanks for giving me a nudge to look at the hardware side instead of the software. Yeah, I'll admit that that soldering was done very poorly and rightfully so as I did it in a very rushed manner with a soldering iron with a huge tip. Thought it'll be enough just for testing but it was now most likely the part of my downfall. Here's to almost 2 weeks of me trying to solve software with hardware being an issue :)

Can't thank you enough. It goes like the saying, the longer you spend trying to fix something, the more trivial the error is.
 
Back
Top