joshupenrose
Member
I've used rotary encoders with several projects in the past with both Arduino Uno and MEGA. I'm using a 200 pulse/rotation NPN incremental encoder. For this project I need to use 4x encoders, so porting over to the Teensy 4.0 because I'll need 8 total interrupt pins.
However, when I stripped down my basic working encoder code to test with Teensy 4.0, I'm not registering any changed when I spin. The one issue I may be having is the addition of two simple separate voltage dividers at each of the encoder digital pins to bring the 5V signal down to 3.3V for the Teensy inputs (using a basic 2k/1k divider). Image attached for reference. Ultimately, I'm planning to use a TXS0108E level conversion module, thinking this will help the speed. But I thought I could at least prove out my prototype with a simple voltage divider. Could this be why I'm not registering any changes when I spin the encoder?
Again, I'm A/B testing here with an UNO, and the circuit/code works with the UNO (without the voltage divider), but when I wire the encoder output through the voltage divders, I can't get the Teensy to register any change with the encoder.
Any help would be appreciated!
However, when I stripped down my basic working encoder code to test with Teensy 4.0, I'm not registering any changed when I spin. The one issue I may be having is the addition of two simple separate voltage dividers at each of the encoder digital pins to bring the 5V signal down to 3.3V for the Teensy inputs (using a basic 2k/1k divider). Image attached for reference. Ultimately, I'm planning to use a TXS0108E level conversion module, thinking this will help the speed. But I thought I could at least prove out my prototype with a simple voltage divider. Could this be why I'm not registering any changes when I spin the encoder?
Again, I'm A/B testing here with an UNO, and the circuit/code works with the UNO (without the voltage divider), but when I wire the encoder output through the voltage divders, I can't get the Teensy to register any change with the encoder.
Any help would be appreciated!
Code:
int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 10000;
int lastMSB = 0;
int lastLSB = 0;
void setup() {
Serial.begin (9600);
Serial.println("hello");
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH);
digitalWrite(encoderPin2, HIGH);
attachInterrupt(digitalPinToInterrupt(2), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), updateEncoder, CHANGE);
}
void loop(){
Serial.println(encoderValue);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1);
int LSB = digitalRead(encoderPin2);
int encoded = (MSB << 1) |LSB;
int sum = (lastEncoded << 2) | encoded;
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue--;
lastEncoded = encoded;
}