Wiring the AS5311 magnetic encoder

I'm trying to test the AS5311 encoder with this board. I'm testing it by first reading the PWM signal by itself, then with a separate code i'm unplugging the PWM and reading the two square waves from the A and B pins. I'm getting a reading from the first PWM test, but the resolution is too small for me to see if the value is correct. During the second test, I'm not getting any signal, which is strange because the exact same code works on the AS5304 chip which has only A and B encoder signals.

Am I wrong in assuming the A and B pins will work the same as the AS5304 chip? I've been ignoring all the other programming pins because I assumed those are intended for functions I don't need. To be honest I'm not entirely sure what the the other pins do.

Here's the code I'm using

volatile int pwm_value = 0;
volatile int prev_time = 0;
 
void setup() {
  Serial.begin(115200);
  // when pin D2 goes high, call the rising function
  attachInterrupt(0, rising, RISING);
}
 
void loop() { }
 
void rising() {
  attachInterrupt(0, falling, FALLING);
  prev_time = micros();
}
 
void falling() {
  attachInterrupt(0, rising, RISING);
  pwm_value = micros()-prev_time;
  Serial.println(pwm_value);
}

Here's the encoder test code. This works perfectly on the AS5304

#include <Encoder.h>

// Change these pin 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 knobLeft(4, 5);
Encoder knobRight(7, 8);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
    Serial.print("x");
    Serial.println(newLeft);
    Serial.print("y");
    Serial.println(newRight);
    positionLeft = newLeft;
    positionRight = newRight;
    
    delay(50);
}

Another possibility is that the encoder magnet that shipped with the AS5311 board isn't compatible with the AS5311. There's no ID marking for the magnet they sent.

any help is much appreciated. Thanks in advance.

ibohan00:
I'm trying to test the AS5311 encoder with this board. I'm testing it by first reading the PWM signal by itself, then with a separate code i'm unplugging the PWM and reading the two square waves from the A and B pins. I'm getting a reading from the first PWM test, but the resolution is too small for me to see if the value is correct. During the second test, I'm not getting any signal, which is strange because the exact same code works on the AS5304 chip which has only A and B encoder signals.

Am I wrong in assuming the A and B pins will work the same as the AS5304 chip? I've been ignoring all the other programming pins because I assumed those are intended for functions I don't need. To be honest I'm not entirely sure what the the other pins do.

Here's the code I'm using

volatile int pwm_value = 0;

volatile int prev_time = 0;

void setup() {
  Serial.begin(115200);
  // when pin D2 goes high, call the rising function
  attachInterrupt(0, rising, RISING);
}

void loop() { }

void rising() {
  attachInterrupt(0, falling, FALLING);
  prev_time = micros();
}

void falling() {
  attachInterrupt(0, rising, RISING);
  pwm_value = micros()-prev_time;
  Serial.println(pwm_value);
}




Here's the encoder test code. This works perfectly on the AS5304


#include <Encoder.h>

// Change these pin 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 knobLeft(4, 5);
Encoder knobRight(7, 8);
//  avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
}

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
    Serial.print("x");
    Serial.println(newLeft);
    Serial.print("y");
    Serial.println(newRight);
    positionLeft = newLeft;
    positionRight = newRight;
   
    delay(50);
}




Another possibility is that the encoder magnet that shipped with the AS5311 board isn't compatible with the AS5311. There's no ID marking for the magnet they sent.

any help is much appreciated. Thanks in advance.

Read page 11.29 of the data sheet, you have to control the CSn to enable the "incremental interface"

on your code:

Whenever I use a AB encoder I connect A and B to two interrupts, and use the ISR to update an internal counter.

I have never used the Encoder.h Library.

Chuck.

1 Like

Thanks Chuck,
That did the trick. I was trying to save the interrupt pins because I plan on using two of these encoder chips on the Uno, and I'm using an interrupt pin in the PWM read function. I was hoping I could get by without using any interrupts for the encoder, but it looks like I'll need at least one.

-Ian

ibohan00:
Thanks Chuck,
That did the trick. I was trying to save the interrupt pins because I plan on using two of these encoder chips on the Uno, and I'm using an interrupt pin in the PWM read function. I was hoping I could get by without using any interrupts for the encoder, but it looks like I'll need at least one.

-Ian

Ian,
How fast will the encoder be changing? If they are slow enough (kilohz range) you can use Pin change interrupts. @GreyGnome's EnableInterrupt.h. It is a library that support all of the pin interrupts on the Arduino, both pins 2 and 3 and the PinChange interrupts (all of the Other Pins, on the UNO). I use this library with a AS5601 in 4096ppr mode on a GearMotor output shaft. The output shaft only turns at 30rpm, about, with the A B quatrature it is only 1024 interrupts on each pin per second. Just in case I miss a pulse, I read the internal Absolute position memory after and Before I start a movement. The most I have been off is 3counts on a 10,000 point move.

Chuck.

Hi,

I have the same encoder, but I'm trying to read the position using the CSn, CLK and DO pins. I've been following this code Simple 3-Wire MAX6675 Thermocouple ADC Arduino Interface as a reference for reading a 3-wire interface, but I'm not entirely sure that I'm implementing things correctly for this sensor. I'm used to having a library that processes the 3-wire and returns the desired output, but I can't seem to find one for this sensor. Any help would be greatly appreciated!

Kelsey

You shouldn't be calling attachInterrupt() inside an ISR like you are, you will probably get
spurious interrupts or lose interrupts. Have your ISR triggered on CHANGE, and make it handle
both cases.