Encoder counts wrong on Teensy, fine on Arduino Mega

Status
Not open for further replies.

overthrow

Member
I have an AS5040 10-bit magnetic encoder. I'm reading both absolute position using SSI and quadrature with index. When I connect it to an Arduino 2560 Mega I can feed it either 3.3V or 5V and I get correct and stable absolute positions.

I connected it to a Teensy 3.2 and feed it 3.3V (also tried powering encoder with 5V, same result). CS = 15, CLK = 15, DO = 13. Quadrature is being tracked by interrupts and looks ok but when the encoder is not moving I get values that bounce between x and x/2. The encoder might show 900, 900, 900, 450, 450, 450, 900... If I move the motor to let's say position 256 it will randomly bounce between that and about 128. It's not exactly x/2 but close. Each output is showing fault on the Teensy.

This is the library I'm using. https://github.com/MarkTillotson/AS5040

Thanks for any insight.
 
My guess is this library depends on the slow speed of regular AVR and fails when run on a much faster board.

Try lowering Teensy's speed in Tools > CPU Speed. 24 MHz might be a good starting point, since that's the slowest where USB still works.

Even if you need faster speed, the idea is to learn whether this library is incompatible due to Teensy's faster speed, or if we're dealing with some other sort of software issue like dependency on 8 bit stuff.

But looking at the library, I see delays of 1 microsecond which we know is 3+ on Mega, and places in the code like this:

Code:
    digitalWrite (_pinCLK, LOW) ;
    digitalWrite (_pinCLK, HIGH) ;
    value = (value << 1) | digitalRead (_pinDO) ;

So my first guess is this library depends on the slower speed of AVR. It's easy to add more delays, but first determine if this is a "speed" or a "logic" problem.
 
I connected it to a Teensy 3.2 and feed it 3.3V (also tried powering encoder with 5V, same result). CS = 15, CLK = 15, DO = 13. Quadrature is being tracked by interrupts and looks ok but when the encoder is not moving I get values that bounce between x and x/2. The encoder might show 900, 900, 900, 450, 450, 450, 900... If I move the motor to let's say position 256 it will randomly bounce between that and about 128. It's not exactly x/2 but close. Each output is showing fault on the Teensy.

Had spurious problems with my Teensy 3.6 and a pair of CUI AMT10 encoders (set at 3200 ppr) with symptoms similar to yours. See thread below. In short, it was a circuit error on my part. And for your thread, I swapped in a Teensy 3.2, and it still works as expected.

https://forum.pjrc.com/threads/4144...for-Teensy-3-6?p=131374&viewfull=1#post131374

I use the PJRC encoder library. In fact, I used that very library on Arduino Due projects.

https://www.pjrc.com/teensy/td_libs_Encoder.html
 
Last edited:
Reviving an old thread. I've been using an AS5040 with a Raspberry Pi but I want to get rid of the RPI and go back to a Teensy 3.2. I went back and looked at the datasheet for the AS5040 and I noticed the maximum clock was 1 Mhz. I'm reviewing the Python code and it looks like there are 0.01 delays between setting loops of setting the clk pin high which I'm guessing is mimicking a clock signal?

I don't like the idea of putting a bunch of delays in the Teensy code to slow it down. Could I just get a clock (something under 1 Mhz) like https://abracon.com/Oscillators/ASEK.pdf ? 32kHz seems like plenty. If I'm understanding it the clock would, 32k times per second, send a signal to the encoder to generate a new position and then I just read it whenever I need? As long as I'm reading it slower than that (probably 20-40x/second) I should always get a fresh value?

I'm building a new PCB for this so adding a clock is no big deal.

as5040-ssi.jpg
 
Status
Not open for further replies.
Back
Top