Jump to content

I2C with Magnetometer HMC5883L

Recommended Posts


I'm new with Energia but not with Arduino.

I'm not able to communicate with HMC5883L throw I2C. The board is launchpad with msp430g2553 microcontroller.


I read some threads about this issue but I didn't find a solution.

#include <Wire.h>#define address 0x1E //0011110b, I2C 7bit address of HMC5883void setup(){  Serial.begin(9600);  // start serial for output  Wire.begin();        // join i2c bus (address optional for master)  //Put the HMC5883 IC into the correct operating mode  Wire.beginTransmission(address); //open communication with HMC5883  //Write CRA (00) 
Link to post
Share on other sites

Please find hint and answers that may help you at

Link to post
Share on other sites

Hi Rei Vilo,


thank you for your reply!

I've already read these threads.

These are my feedbacks.

1) With external pull-up (10K) resistors it doesn't work.

2) I changed the twi.c file and it doesn't work.


It's strange because each of these threads has a different solution.





Link to post
Share on other sites

Hi Rei Vilo,

Now it works with 4.7K pull-up risistors and the arduino sketch provided by Sparkfun (https://www.sparkfun.com/products/10530).

It also works with your library "software I2C" in pins P2_3 and P2_4.


I've only a problem: The magnetometer sends 6 bytes and this is the code:

 Wire.requestFrom(address, 6);
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb

If I use the I2C software library it loses the last byte ("Y lsb").

If I change the first line of this code to "Wire.requestFrom(address, 7);" it works.

Maybe is there a little bug in the library?


Thank you



Link to post
Share on other sites

Thank you for pointing the bug.


In the function uint8_t SoftwareWire::requestFrom(uint8_t address, uint8_t length)

for (uint8_t i=0; i<length-1; i++) rxBuffer[i] = readI2C(false);

should be

for (uint8_t i=0; i<length; i++) rxBuffer[i] = readI2C(false);

As an alternative, you can use size_t SoftwareWire::write(const uint8_t *data, size_t length)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...