AS5134 readout using standard SPI

Hi,
just wanted to share my code for people trying to read the AS5134. I'm using an arduino nano.
All you need is a 10k pull-down resistor DIO->GND for this to work. The angle seems to overflow at 180 degree intervals.

#include <SPI.h>

const int slaveSelectPin = 10;

void setup(){
  SPI.begin();
  SPI.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE3));
  pinMode(slaveSelectPin, OUTPUT);
  digitalWrite(slaveSelectPin, LOW);
  Serial.begin(9600);
  Serial.println("welcome");
}

void loop(){
  digitalWrite(slaveSelectPin, HIGH);
  byte b0 = SPI.transfer(0);
  byte b1 = SPI.transfer(0);
  byte b2 = SPI.transfer(0);
  digitalWrite(slaveSelectPin, LOW);
  byte angle = ((b1&0xf)<<4|(b2>>4)&0xf);
  Serial.println(angle);
  delay(1000);
}

Hope this helps,
best regards,
Simon

1 Like

Thanks!

Something odd is happening here, the datasheet says this chip outputs 2 bytes, not 3, and that the angle is a 9 bit field from those 16 bits.

[ Ah, a bit more delving and it seems there's a 5 bit command section at the front of the SPI packet, so 21 bits are used. The angle is 9 bits, you are chopping off one of the bits, perhaps try

 int angle = ((b1&0xf)<<5)|((b2>>3)&0x1f);

Also you need to use SPI mode 0, not 3. The timing diagram clearly shows the clock phase and polarity...

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.