Teensy 3.2 and Maxon motor encoder (+5V power supply)

Status
Not open for further replies.

33chen

New member
Dear All,

I am working on a project to connect Teensy 3.2 to read maxon motor encoder data. This is the specification sheet of maxon encoder. We can simply connect channel A and channel B, ground and 5v power supply to a microcontroller (like Teensy 3.2, Arduino Duo, and Arduino Uno, Lenonardo, Promini) to read this encoder date. The strange thing is that this connecting methods work for Arduino Uno, Lenonard and Arduino promini. However, we can not collect any data from this encoder if we use this way to connect it to Teensy 3.2 or Arduino Due. Do anybody know why???

The way we connect the encoder to the Teensy is basically

Encoder Teensy
5v Vin (3.6 to 6.0 volts) (I am questioning if this is right pin to power the encoder)
GND GND
ChannelA 2 (digital pin 2)
ChannelB 3 (digital pin 3)

I know the I/O pins are 5v tolerant. But does it mean I can connect channel A and B from my Teensy directly connect to pin 2 and 3 on teensy to read Encoder. The code is below. The code iteself should be fine because it works on Leonardo and promini.

Code:
//--------------------
// for encoder 0
unsigned long last_read_time;
volatile float angle0 = 0;
#define encoder0PinA 6
#define encoder0PinB 7
int encoder0Count = 0;
//--------------------
void setup() {

  //  // for encoder 0
  pinMode(encoder0PinA, INPUT); 
  pinMode(encoder0PinB, INPUT); 
  attachInterrupt(0, doEncoder0A, CHANGE);  
  attachInterrupt(1, doEncoder0B, CHANGE);  
  last_read_time = millis();
  Serial.begin(19200);
}
void loop() {
  unsigned long t_now = millis();
  float dt = (t_now - last_read_time)/1000.0;
  Serial.print(dt); 
  Serial.print("\t");
  // for encoder 0
  Serial.print(encoder0Count); Serial.print("\t");
  Serial.print(angle0); Serial.println("\t");
}

void doEncoder0A(){  // interrupt 0 function
  if (digitalRead(encoder0PinA) == HIGH) {  // look for a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way encoder is turning
      encoder0Count = encoder0Count + 1;
    } 
    else {
      encoder0Count = encoder0Count - 1;
    }
  }
  else {                                     // must be a high-to-low edge on channel A
    if (digitalRead(encoder0PinB) == HIGH) { // check channel B to see which way encoder is turning  
      encoder0Count = encoder0Count + 1;
    } 
    else {
      encoder0Count = encoder0Count - 1;
    }
  }
  angle0 = encoder0Count/10000;  // unit: degree
}

void doEncoder0B(){  // interrupt 1 function
  if (digitalRead(encoder0PinB) == HIGH) {   // look for a low-to-high on channel B
    if (digitalRead(encoder0PinA) == HIGH) { // check channel A to see which way encoder is turning
      encoder0Count = encoder0Count + 1;
    } 
    else {
      encoder0Count = encoder0Count - 1;
    }
  }
  else {                                    // must be a high-to-low edge on on channel B
    if (digitalRead(encoder0PinA) == LOW) { // check channel B to see which way encoder is turning
      encoder0Count = encoder0Count + 1;
    } 
    else {
      encoder0Count = encoder0Count - 1;
    }
  }
  angle0 = encoder0Count/10000;  // unit: degree
}

Thank you very much!!!

Ji
 

Attachments

  • Encoder_Maxon.pdf
    740.4 KB · Views: 134
The code iteself should be fine because it works on Leonardo and promini.
Obviously not. :)

You talk about pin 2 / 3, but in your code you have 6 / 7. attachInterrupt() on Teensy and most ARM boards uses the pin number. Use digitalPinToInterrupt for portable code.

Use digitalReadFast instead of digitalRead. It has much less overhead.
 
Thanks lot, tni. Sorry, I did update code to use pin 2 and 3. My coworker and I just realized as well that some syntax used for typical Arduino board should be modified to be used in the code for Teensy. We found out a library for encoder used with Teensy and built our main code from there to get the correct reading. Thanks again. Problem is solved. I need to be familiar with updated syntax used for Teensy now.

Best,

Ji
 
Status
Not open for further replies.
Back
Top