Issue regarding MT6835 encoder

Hello everyone,
I just got a MT6835 encoder, and to get started I tried to apply the example code. However, I’ve already ran into an issue. No matter the position of the magnet, calling Sensor.getAngle() returns 12867.88, which does not make sense to me. The velocity output is always 0. The getStatus() function seems to work, as it normally returns 000, but does return 010 when the magnet is removed.
Any suggestions would be appreciated.

#include <Arduino.h>

#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"

#include "encoders/mt6835/MagneticSensorMT6835.h"

#define SENSOR_nCS 18

SPISettings myMT6835SPISettings(1000000, MT6835_BITORDER, SPI_MODE3);
MagneticSensorMT6835 sensor = MagneticSensorMT6835(SENSOR_nCS, myMT6835SPISettings);

long ts;

void setup() {
    SimpleFOCDebug::enable();
    sensor.init();
    ts = millis();
}

void loop() {
    sensor.update();
    long now = millis();
    if (now - ts > 1000) {
        ts = now;
        SimpleFOCDebug::print("A: ");
        SimpleFOCDebug::println(sensor.getAngle());
        SimpleFOCDebug::print(" V: ");
        SimpleFOCDebug::println(sensor.getVelocity());
        SimpleFOCDebug::print(" S: ");
        SimpleFOCDebug::println(sensor.getStatus());
        Serial.println("----");
    }
    delay(10);
}

Sure, this is the method I used for reading it

#include <SPI.h>
#include <Arduino.h>

const int CSN_PIN = 10;
long ts;

void setup() {
    ts = millis();
    // Serial communication initialisation
    Serial.begin(9600);
  
     // Configuration of the CSN pin
    pinMode(CSN_PIN, OUTPUT);
    digitalWrite(CSN_PIN, HIGH);  
  
    // SPI communication initialisation with the MT6835 parameters
    SPI.begin();
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
}

void loop() {
    long now = millis();
    if (now - ts > 1000) {
        ts = now;
        Serial.println("---");

        // Read the angle and status registers from the MT6835
        uint8_t data0 = spiReadRegister(0x003);
        uint8_t data1 = spiReadRegister(0x004);
        uint8_t data2 = spiReadRegister(0x005);
        uint8_t data3 = spiReadRegister(0x006);

        // Merge the angle bytes into one 21-bit integer
        uint32_t data = ((uint32_t)data0 << 13) | (data1 << 5) | (data2 >> 3); 

        // Calculate the angle in degrees
        float angle_deg = (float)data / 2097152.0 * 360.0;
        Serial.println(angle_deg, 10);

        // Check warnings
        uint8_t status = data2 & 0x7;
        if (status & 0x01) {
            Serial.println("Warning : overspeed!");
        }
        if (status & 0x02) {
            Serial.println("Warning : Low magnetic field!");
        }
        if (status & 0x04) {
            Serial.println("Warning : Low voltage!");
        }
    }
    delay(10);
}

// Fonction to read a register from the MT6835 with SPI
uint8_t spiReadRegister(uint16_t address) {
  // reading command is 0x3 (C3~C0 = 0011) followed by the adress on 12 bits
  uint16_t command = (0x3 << 12) | (address & 0xFFF);
  uint8_t result = 0;

  // Activate the SPI module (put CSN to LOW)
  digitalWrite(CSN_PIN, LOW);
  
  // Send the reading command and the adress
  SPI.transfer16(command);
  
  // Read the MT6835 answer
  result = SPI.transfer(0x00);
  
  // Desactivate the SPI module (put CSN to HIGH)
  digitalWrite(CSN_PIN, HIGH);

  return result;
}

Hi @filal253 , welcome to SinpleFOC!

Your code looks ok actually. Which MCU type is this running on?

Could you maybe also share some details of the wiring between MCU and sensor?

Hi, I’m running it on an ATmega32U4 pro micro. This is the hookup diagram:


In the meantime I wrote my own script which reads the registers 0x003-0x006 over SPI and does the angle conversion, and that worked correctly.