Hi, I'm new to SPI and I thought a small project with AS5048A encoder would be a good introduction but so far no luck!
I got my hands on an AS5048A chip which I made a PCB for using the datasheet as a guide but so far I can't get a proper reading from the sensor. I'll post the schematics and the code so you'll better understand what I'm trying to do. I'm just trying to read the encoder position for a start.
I'm using Arduino Leonardo (so I can make a joystick) and using pins 13 for SCK, 12 for MISO, 11 for MOSI and 10 for CS. I also connected ground and power (I tried both 3v3 and 5 with no luck).
All I get from serial is this:
It makes no difference to the output whether the sensor is connected or not which drives me to the conclusion that it could be a fake chip.
Any help would be appreciated, thanks.
#include <SPI.h>
/*PINS
Arduino SPI pins
MOSI = 11, MISO = 12, SCK = 13, CS = 10
*/
const byte CS_pin = 10; //Chip select pin for manual switching
uint16_t rawData = 0; //bits from the encoder (16 bits, but top 2 has to be discarded)
float degAngle = 0; //Angle in degrees
uint16_t command = 0b1111111111111111; //read command (0xFFF)
void setup()
{
SPI.begin();
Serial.begin(9600);
Serial.println("AS5048A - Magnetic position encoder");
pinMode(CS_pin, OUTPUT); //CS pin - output
}
void loop()
{
SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE1));
//--sending the command
digitalWrite(CS_pin, LOW);
SPI.transfer16(command);
digitalWrite(CS_pin, HIGH);
delay(10);
//--receiving the reading
digitalWrite(CS_pin, LOW);
rawData = SPI.transfer16(command);
digitalWrite(CS_pin, HIGH);
SPI.endTransaction();
rawData = rawData & 0b0011111111111111; //removing the top 2 bits (PAR and EF)
degAngle = (float)rawData / 16384.0 * 360.0; //16384 = 2^14, 360 = 360 degrees
Serial.print("Deg: ");
Serial.println(degAngle);
}