# Magnetic sensor MT6701 I2C integration

**URL:** <https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919>\
**Category:** developement\
**Tags:** mt6701, magnetic-sensor\
**Created:** [May 11, 2024, 9:45pm UTC](https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919 "2024-05-11T21:45:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alperbasarn](https://community.simplefoc.com/user_avatar/community.simplefoc.com/alperbasarn/32/6631_2.png) [@alperbasarn](https://community.simplefoc.com/u/alperbasarn)\
**Post date:** [May 11, 2024, 9:45pm UTC](https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919/1 "2024-05-11T21:45:27Z")

</div>

Dear folks,

Could not find any resource online, neither in this forum regarding with MT6701 i2c integration, so I have modified MagneticSensorI2C.cpp as below, and everything works like a charm. will make a MR on github sometime whne I have time. Enjoy!

Simply add snippet below at line 19,then you can do the following declaration in main program.

MagneticSensorI2C sensor = MagneticSensorI2C(MT6701\_I2C);

#include “MagneticSensorI2C.h”

/\*\* Typical configuration for the 12bit MT6701 magnetic sensor over I2C interface \*/  
MagneticSensorI2CConfig\_s MT6701\_I2C = {  
.chip\_address = 0x06,  
.bit\_resolution = 14,  
.angle\_register = 0x03,  
.data\_start\_bit = 15  
};

Full file:

/\*\* Typical configuration for the 12bit AMS AS5048 magnetic sensor over I2C interface \*/  
MagneticSensorI2CConfig\_s AS5048\_I2C = {  
.chip\_address = 0x40, // highly configurable. if A1 and A2 are held low, this is probable value  
.bit\_resolution = 14,  
.angle\_register = 0xFE,  
.data\_start\_bit = 15  
};

/\*\* Typical configuration for the 12bit MT6701 magnetic sensor over I2C interface \*/  
MagneticSensorI2CConfig\_s MT6701\_I2C = {  
.chip\_address = 0x06,  
.bit\_resolution = 14,  
.angle\_register = 0x03,  
.data\_start\_bit = 15  
};

// MagneticSensorI2C(uint8\_t \_chip\_address, float \_cpr, uint8\_t \_angle\_register\_msb)  
// @param \_chip\_address I2C chip address  
// @param \_bit\_resolution bit resolution of the sensor  
// @param \_angle\_register\_msb angle read register  
// @param \_bits\_used\_msb number of used bits in msb  
MagneticSensorI2C::MagneticSensorI2C(uint8\_t \_chip\_address, int \_bit\_resolution, uint8\_t \_angle\_register\_msb, int \_bits\_used\_msb){  
// chip I2C address  
chip\_address = \_chip\_address;  
// angle read register of the magnetic sensor  
angle\_register\_msb = \_angle\_register\_msb;  
// register maximum value (counts per revolution)  
cpr = \_powtwo(\_bit\_resolution);

// depending on the sensor architecture there are different combinations of  
// LSB and MSB register used bits  
// AS5600 uses 0..7 LSB and 8..11 MSB  
// AS5048 uses 0..5 LSB and 6..13 MSB  
// MT6701 uses 0..5 LSB and 6..13 MSB  
// used bits in LSB  
lsb\_used = \_bit\_resolution - \_bits\_used\_msb;  
// extraction masks  
lsb\_mask = (uint8\_t)( (2 \<\< lsb\_used) - 1 );  
msb\_mask = (uint8\_t)( (2 \<\< \_bits\_used\_msb) - 1 );  
wire = &Wire;  
}

MagneticSensorI2C::MagneticSensorI2C(MagneticSensorI2CConfig\_s config){  
chip\_address = config.chip\_address;

// angle read register of the magnetic sensor  
angle\_register\_msb = config.angle\_register;  
// register maximum value (counts per revolution)  
cpr = \_powtwo(config.bit\_resolution);

int bits\_used\_msb = config.data\_start\_bit - 7;  
lsb\_used = config.bit\_resolution - bits\_used\_msb;  
// extraction masks  
lsb\_mask = (uint8\_t)( (2 \<\< lsb\_used) - 1 );  
msb\_mask = (uint8\_t)( (2 \<\< bits\_used\_msb) - 1 );  
wire = &Wire;  
}

MagneticSensorI2C MagneticSensorI2C::AS5600() {  
return {AS5600\_I2C};  
}

void MagneticSensorI2C::init(TwoWire\* \_wire){

wire = \_wire;

// I2C communication begin  
wire-\>begin();

this-\>Sensor::init(); // call base class init  
}

// Shaft angle calculation  
// angle is in radians [rad]  
float MagneticSensorI2C::getSensorAngle(){  
// (number of full rotations)\*2PI + current sensor angle  
return ( getRawCount() / (float)cpr) \* \_2PI ;  
}

// function reading the raw counter of the magnetic sensor  
int MagneticSensorI2C::getRawCount(){  
return (int)MagneticSensorI2C::read(angle\_register\_msb);  
}

// I2C functions  
/\*

- Read a register from the sensor

- Takes the address of the register as a uint8\_t

- Returns the value of the register  
\*/  
int MagneticSensorI2C::read(uint8\_t angle\_reg\_msb) {  
// read the angle register first MSB then LSB  
byte readArray[2];  
uint16\_t readValue = 0;  
// notify the device that is aboout to be read  
wire-\>beginTransmission(chip\_address);  
wire-\>write(angle\_reg\_msb);  
currWireError = wire-\>endTransmission(false);

/\*

- Checks whether other devices have locked the bus. Can clear SDA locks.

- This should be called before sensor.init() on devices that suffer i2c slaves locking sda

- e.g some stm32 boards with AS5600 chips

- Takes the sda\_pin and scl\_pin

- Returns 0 for OK, 1 for other master and 2 for unfixable sda locked LOW  
\*/  
int MagneticSensorI2C::checkBus(byte sda\_pin, byte scl\_pin) {

motor that I coupled it with:  
[https://nl.mouser.com/ProductDetail/SparkFun/ROB-20441?qs=1Kr7Jg1SGW9zbyRI9In86Q%3D%3D&mgh=1&utm\_id=20333412842&gad\_source=1&gclid=CjwKCAjwrvyxBhAbEiwAEg\_Kgvi7ptAtE5Rgdw87GJZpCB4Hixt1Yirz6DBIkKL3UqeddUc\_MV7O1RoCKwsQAvD\_BwE](https://nl.mouser.com/ProductDetail/SparkFun/ROB-20441?qs=1Kr7Jg1SGW9zbyRI9In86Q%3D%3D&mgh=1&utm_id=20333412842&gad_source=1&gclid=CjwKCAjwrvyxBhAbEiwAEg_Kgvi7ptAtE5Rgdw87GJZpCB4Hixt1Yirz6DBIkKL3UqeddUc_MV7O1RoCKwsQAvD_BwE)

My setup:

 ![setup](https://community.simplefoc.com/uploads/default/original/2X/1/18724fc1550c719db547d839a292e2a58386bec4.jpeg)

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [May 11, 2024, 9:47pm UTC](https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919/2 "2024-05-11T21:47:43Z")

</div>

> [@alperbasarn](#):
>
> everything works like a charm

Thank you for the contribution.

Hopefully (@Antun_Skuric , @runger ?) it will get integrated into the next release if possible.

Cheers,  
Valentine

---

<div class="post-metadata">

**Author:** ![alperbasarn](https://community.simplefoc.com/user_avatar/community.simplefoc.com/alperbasarn/32/6631_2.png) [@alperbasarn](https://community.simplefoc.com/u/alperbasarn)\
**Post date:** [May 11, 2024, 10:00pm UTC](https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919/3 "2024-05-11T22:00:40Z")

</div>

`no worries, thank you guys for maintaining such useful open source project. Here is the MR:

> <https://github.com/simplefoc/Arduino-FOC/pull/398>

Btw, do you think it would be useful to add an example ?

---

<div class="post-metadata">

**Author:** ![Valentine](https://community.simplefoc.com/user_avatar/community.simplefoc.com/valentine/32/1024_2.png) [@Valentine](https://community.simplefoc.com/u/Valentine)\
**Post date:** [May 11, 2024, 10:46pm UTC](https://community.simplefoc.com/t/magnetic-sensor-mt6701-i2c-integration/4919/4 "2024-05-11T22:46:17Z")

</div>

> [@alperbasarn](#):
>
> think it would be useful to add an example

Hi, thank you.

Yes, an example would be awesome!

Cheers,  
Valentine
