Sensor Data Reading Backwards

I’m using a arduino mega and SimpleFOC Shield testing the AS5048A megnetic sensor via Spi The sketch didn’t work at first so I declared the spi pins it works but when I turn my motor in the cw direction the sensor data goes ccw and vise versa, trued multiple AS5048A Encoders Samething, even in other sketches when the motor is initializing and align with the sensor it says the sensor is ccw Could someone point my in the right direction literally here’s the code

#include <SimpleFOC.h>

// Define the SPI pins
const int spiMISO = 50; // MISO pin
const int spiMOSI = 51; // MOSI pin
const int spiSCK = 52; // SCK pin
const int spiSS = 53; // SS (Slave Select) pin

SPISettings spiSettings(1000000, MSBFIRST, SPI_MODE1);
// SPI settings (clock speed, bit order, SPI mode)

// MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs)
// config - SPI config
// cs - SPI chip select pin
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(AS5048_SPI, spiSS);

void setup() {
// Monitoring port

// Initialize SPI communication

// Initialize magnetic sensor

Serial.println(“Sensor ready”);

void loop() {
// Iterative function updating the sensor internal variables
// It is usually called in motor.loopFOC()
// This function reads the sensor hardware and
// has to be called before getAngle and getVelocity

// Display the angle and the angular velocity to the terminal

Hello @JaySlapCity
I’m using a different version of the AS5048 sensor. It’s included in the simpleFOCDrivers library.

Maybe that helps to find the right direction.

One thing crossed my mind: Did you mount the sensor-chip facing the magnet or do you measure “through the PCB”?

1 Like

the sensor direction is kind of arbitrary, like o_lampe says it is from the perspective of the IC. You should just configure things in mind with whatever “real” direction you want- the software does not care.

1 Like

I have the chip facing up towards the magnet

Google Photos

Google Photos

I’m going to look at that library thanks looks very promising

Interesting I using the sensor raw data to set angle and torque limits at 0 and 100, the motor will apply torque to keep from going past the limits maybe I could try and add a piece of code to convert negative readings to positive and allow that to be recieved as the sensors data let’s see how this goes

you can just define a constant called real_dir with a value of 1/-1 that corresponds to the direction you actually want, and just multiply any target torque/velocity/position by this to ensure that it’s always consistent.

1 Like

Thanks I got it to work with this code

float MagneticSensorSPI::getSensorAngle() {
int raw_count = getRawCount();

// Reverse the count direction
int reversed_count = 4096 - raw_count;

// Convert the raw count to angle (radians)
float angle = (2 * PI * reversed_count) / (1 << bit_resolution);

// Normalize the angle to be within [0, 2*PI)
if (angle < 0) {
angle += 2 * PI;

return angle;