First of all, I am new user to arduino but I have already programmed on other developpment kit.
In this project, I would like to interface the magnetic sensor from MEMSIC the MMC5883MA-B.
This device looks very precise and easy to interface but I am stuck with arduino Wire librairy.
I looks like I cannot read from any register from the device.
I found the wire librairy a bit complicated to understand. I looked other several example on the forum and the iterface for the same sensor on gitHub ( https://github.com/pigtwn/Driver_MMC5883MA) but I cannot find a solution to my problem.
My tests showed that I correctly created a START condition as I found a low value on the osciloscope on SDA line. But then I can't read any register, the value obtained is always 255, even on the DEVICE_ID register.
For the Arduino Wire library you provide the I2C address not the address byte. The I2C address consists of 7bits and for the MMC5883MA it's 0x30. You use the same address for read and write operations, the library is responsible to set the corresponding bit in the address byte correctly.
This sensor is a 3.3V device, according to the datasheet the voltage on the I2C interface should not exceed 3.6V. If you connect that device directly to the UNO you might damage it.
Wire.beginTransmission(MMC5883MA_ADDR); //Adress of I2C device
Wire.write(MMC5883MA_PRODUCT_ID);// Selecting I2C ID_Register
Wire.endTransmission();
According to the datasheet a stop condition should not happen between the register address and the read request (that's called a repeated start condition), so in this code part you should give Wire.endTransmission a false parameter.
void loop() {
//////////////// GET PRODUCT ID ///////////////
Wire.requestFrom(MMC5883MA_ADDR_READ, 1);
byte data1 = Wire.read(); //Get Product ID
Serial.println(data1); // value here is always 255
delay(1000);
}
The datasheet doesn't say anything about repeated read requests with the specification of a register address in-between. This might or might no work.
You should check the result codes of Wire.endTransmission() and Wire.requestFrom() for errors.
If you still get wrong results, post a complete wiring diagram of your setup.
Yup I made a mistake while writing the previous post, adding comments etc... the code wasn't compiling due to the MEMSIC adress that was not defined.
Anyway, thank you for your reply, I did have a wrong I2C address, tried with 0x30 and I am now able to read the right I product ID of the device : 0x0C with this code :
There is clearly something that I am doing wrong. I tried with SET and RESET parts, still the same. I also tried to put the loop code into the setup so it does it once, it is not acting better.
Also I did not notice any problems with the returned values from Wire.endTransmission() and Wire.requestFrom.
I finnaly tried to remove the false param form endTransmisssion on each read parts. or just remove the line without success.
I did not try to write into control register 2 as I was refering to the example on the datasheet. But I'll try and post results ASAP
Hi, I have been having a similar problem with the MMC5883MA-B sensor, and I was wondering if you ever figured out how to get accurate readings? All the magnetic field readings I get seem to be ~10000 mG off from my predicted readings with regards to the earths magnetic field. Thanks for your time!