Hello,
I try to use MAG3110 sensor with an arduino due board.
I have a problem when i want to write in the "Ctrl_reg2" register.
I want to write 0x80 in the register but when i read it (just to verify), the register value is 0x00...
I haven't this issue with ctrl_reg1.
Here ou can see the writing code :
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // ctrl register1
Wire.write(0x00); // Standby
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // ctrl register2
Wire.write(0x80); // send 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
Is there any special things to do with this register ?
Many thanks for the help.
hi Tomas,
I'm really sorry for late reply but i had a big issue on another project so i hadn't a lot of time...
you can see below my Arduino source code :
I can see if there is a mistake...
Many thanks for your help.
regards,
Nicolas.
#include <Wire.h>
#define MAG_ADDR 0x0E // 7-bit address for the MAG3110, doesn't change
void setup()
{
pinMode(38, INPUT);
Serial.begin(115200); // start serial for output
Wire.begin(); // join i2c bus (address optional for master)
config(); // turn the MAG3110 on
}
void loop()
{
if (digitalRead(38) == HIGH){
print_values();
}
}
void config(void)
{
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // ctrl register1
Wire.write(0x00); // Standby
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // ctrl register2
Wire.write(0x80); // send 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // ctrl register2
Wire.write(0x30); // send 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // ctrl register1
Wire.write(0xC9); // send 0x01, active mode
Wire.endTransmission(); // stop transmitting
}
void print_values(void)
{
int LSB_MASK = 0x00FF;
Serial.write(0x01);
int16_t X_T = readx();
Serial.write(X_T>>8); // MSB
Serial.write(X_T & LSB_MASK); // LSB
int16_t Y_T = ready();
Serial.write(Y_T>>8); // MSB
Serial.write(Y_T & LSB_MASK); // LSB
int16_t Z_T = readz();
Serial.write(Z_T>>8); // MSB
Serial.write(Z_T & LSB_MASK); // LSB
Serial.write(0x02);
}
//////////////////////////////// Read X /////////////////////////////////////
int16_t readx(void)
{
byte xl = 0;
byte xh = 0; // define the MSB and LSB
int16_t xout = 0;
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x01); // x MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xh = Wire.read(); // receive the byte
}
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x02); // x LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xl = Wire.read(); // receive the byte
}
//xl = 0; // Ici on met le lsb à 0 pour éviter les fluctuations
xout = (xl|(xh << 8)); // concatenate the MSB and LSB
return xout;
}
//////////////////////////////// Read Y /////////////////////////////////////
int16_t ready(void)
{
byte yl = 0;
byte yh = 0; // define the MSB and LSB
int16_t yout = 0;
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x03); // y MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yh = Wire.read(); // receive the byte
}
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x04); // y LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yl = Wire.read(); // receive the byte
}
//yl = 0; // Ici on met le lsb à 0 pour éviter les fluctuations
yout = (yl|(yh << 8)); // concatenate the MSB and LSB
return yout;
}
//////////////////////////////// Read Z /////////////////////////////////////
int16_t readz(void)
{
byte zl = 0;
byte zh = 0; // define the MSB and LSB
int16_t zout = 0;
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x05); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zh = Wire.read(); // receive the byte
}
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x06); // z LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zl = Wire.read(); // receive the byte
}
//zl = 0; // Ici on met le lsb à 0 pour éviter les fluctuations
zout = (zl|(zh << 8)); // concatenate the MSB and LSB
return zout;
}
//////////////////////////////// Read CTRL_REG_2 /////////////////////////////////////
byte readctrl2(void)
{
byte CTRL2; // define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
CTRL2 = Wire.read(); // receive the byte
}
return CTRL2;
}
//////////////////////////////// Read CTRL_REG_1 /////////////////////////////////////
byte readctrl1(void)
{
byte CTRL1; // define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); // needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
CTRL1 = Wire.read(); // receive the byte
}
return CTRL1;
}
Hi Nicolas,
This is correct behaviour, the AUTO_MRST_EN is a write-only bit and always reads back as 0.
Let me know if you have any other questions.
Regards,
Tomas
PS: If my answer helps to solve your question, please mark it as "Correct". Thank you.
hi Tomas,
Sorry for late reply, i was in vacation :smileyhappy:
Many thanks for your help !
However i have one more question :
If RAW bit is 1, output data should be beetween -20000 and +20000 is that right?
Because when i put this bit to 1, output values are still beetween -32768 and +32768... is that normal ? or i have an issue in my software?
Regards,
Nicolas
Hi Nicolas,
Having such big values is not normal, unless the device is exposed to strong magnetic fields. Could you please share here your source code and raw values you read from registers 0x01 – 0x06? There may be a problem with converting raw values to signed 16-bit numbers or with incorrect interpreting on output device (if used).
Regards,
Tomas