Encoder library using pins 2 and 4

Status
Not open for further replies.

dparker

Member
Greetings,
I'm using a Teensy 3.2 and am trying to use the Encoder library with an encoder connected to pins D2 and D4. It doesn't seem to be tracking properly.

Output from Encoder -> Basic example while turning the encoder clockwise:

Code:
Encoder myEnc(2, 4);

Code:
Basic Encoder Test:
0
-1
0
-1
0
-1

Looking more closely at D4, it seems to be always pulled HIGH. I tried different pins (like D3 and D12) and it worked as expected. Is there something special going on with this pin that would prevent me from using it w/ an encoder?

Many thanks!
 
Most inexpensive mechanical encoders are essentially 2 switches that short the signal pins to ground.

If the signal is staying at 3.3V, that's a pretty sure sign the hardware is not connected properly or otherwise defective. As a quick test, just touch a wire or paperclip between pin 4 and GND. You should see the voltage go to 0 volts when the wire shorts the pin to GND. Unless you have some sort of very fancy encoder with special circuitry, all the encoder does is the same shorting to ground as a wire.
 
Here is a screenshot of what I'm seeing w/ the analyzer when hooked up to the Teensy.
Screen Shot 2018-12-27 at 1.49.22 PM.jpg

The same encoder and circuit with the other two I/O pins (D3 & D12) works . As an additional sanity check, I also hooked it up to an UNO using pins 2 and 4 and the sketch worked. I wanted to check to make sure D4 didn't have a special function I had to disable or something else that would cause it to always output 3.3v.

Here is how it's connected:
Screen Shot 2018-12-27 at 3.47.10 PM.jpg
 
I can confirm pin 4 does not have any special feature you need to disable.

As a quick sanity check, I uploaded this to a Teensy 3.2.

Code:
#include <Encoder.h>

Encoder myEnc(2, 4);

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);
  }
}

I don't have an actual encoder handy, but I did try tapping a wire between GND and each pin.

When I touch it to pin 4, I see this in the serial monitor:

Code:
1
0
1
0
1
0

When I touch the wire to pin 2, I see this:

Code:
-1
0
-1
0
-1
0

Pin 4 definitely is responding. I'm pretty sure your problem is simply a loose or broken connection between your encoder and Teensy pin 4.
 
However, I would suggest reducing resistors R2 and R3. 10K is fairly high. The chip's pullup resistors are usually between 20K to 50K, so on the low end you may not get the signal to go all the way to logic low. But it would still certainly change enough to be viewable on any voltmeter. If you're actually measuring with a voltmeter and the pin is staying solidly at 3.3V, that almost certainly means the encoder circuit isn't connected or isn't working properly. There's nothing Teensy can do if your circuit doesn't actually change the voltage on pin 4.

If the only way you are viewing the signal at the pin is with that logic analyzer, consider that it doesn't really show you the voltage at the pin. It only slows logic low vs logic high, and what voltage threshold it considers low vs high may not be the same as other digital pins. Your circuit very likely may be pulling the pin to about 0.8 to 1.0 volts for logic low (not close enough to GND to be reliable), and a digital-only tool will never show you this sort of analog problem with your circuitry. Use a real voltmeter or an oscilloscope. Or some of those newer logic analyzers have analog channels which really do show voltage. Logic analyzers are excellent tools for some types of problems, but they hide the analog reality of the world from you. That's a huge limitation when your signals aren't clean digital low & high - as they very likely are not in a case like this where you have an analog circuit with resistors and capacitors.
 
Last edited:
You were right (of course!), pin 4 is going between 3.3v and 1.6v instead of 3.3v and 0v. I'm guessing it's working on the UNO because 1.6v is considered LOW on a 5v I/O pin. Thanks again for the help!
 
Status
Not open for further replies.
Back
Top