Triple Axis Magnetometer HMC5883L Not Working

So I’m trying to get the Triple Axis Magnetometer HMC5883L working with the Spark Core using the tutorial and code from I’ve combined all of the libraries and source into one and got it compiling to the core; however, the serial output is nonsense. Either it’s constant 0’s for everything or rapidly and drastically changing numbers despite the magnetometer sitting still on a table.

I’ve noticed other threads pertaining to having issues with I2C so I also tried shifting the address one bit to the left getting 0x3C instead of 0x1E. The results were the same as above.

When hooked up to an Arduino Mega, everything works as expected with the correct data being output. Do you guys have any ideas what the problem might be or is there anything I can do or provide to help you narrow it down?


@Shadow6363 there is definitely some oddness getting I2C workign on this thing. I’ll le tyou know if I manage to do it!

Thanks @Soulhuntre. Eager to see what you make of it. For any others, the code I’m currently running is I pretty much just took each of the files from the prior link, copy/pasted them into one file and fixed any errors that arose.

So I’ve tried even the most basic of programs, an I2C Scanner, but still haven’t gotten anywhere. I have a BMP085 barometric sensor handy which also operates over I2C. I tried hooking it up and after running the aforementioned scanner, it was detected. I’m thinking the folks over in TMP102 and Spark might be having a similar problem, but I’m not sure.

I started to look into the firmware itself, trying to find where specifically it was stopping. I changed each of the non-zero return statuses in TwoWire::endTransmission of spark_wiring_i2c.cpp to something unique and it ends up failing on the first timeout at line 253. I don’t really know I2C, but that seems to tell me that for whatever reason, with the HMC5883L, either the I2C_GenerateSTART(I2C1, ENABLE) or the I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) aren’t working as expected. Perhaps something about this particular device is preventing the Spark Core from becoming the Master device? I tried comparing the I2C library from Arduino-land to the Spark library, but there are a lot of constant flags I don’t know the meaning of being passed around making this rather difficult.

Anyway, I leave all of this here in hopes either one of the Spark developers or someone more familiar with things might find it helpful in debugging what’s wrong.

Bumpity bump bump bump.

Hmm… Have you tried this @Shadow6363 since the I2C improvements that got merged in a week or two ago?

I also am not an I2C expert, but maybe someone who knows this better than I do ( cough @mohit ) could guide us in the right direction. :smile:


Hey @Dave. I did try those, but sadly to no avail. I’m currently running a self-built copy of the master branch from GitHub. It seems to pick up some of my other i2c devices fine, but this one just won’t show up. Let me know if you have any other ideas to try or if there’s any sort of debugging information I could provide that might be relevant.


Hmm. Maybe could you post your wiring schematic and the code you’re using? I’m guessing someone here has worked with that Magnetometer and could provide some feedback, maybe something isn’t quite wired up right?


I don’t know if your underlying issue is the same one that I had with the HMC5883L… take a look.

The fact that the SparkCore’s int’s are 32 bit could be causing you some issues like me.


@Dave: Working on getting you a schematic. The code I’m running is the I2C Scanner mentioned in my fourth post although I just tried @cloris’ code and it also didn’t work.

@cloris: Thanks for the link, but sadly it didn’t seem to change anything. How are you wiring your HMC 5883L? SDA and SCL directly to the Spark Core or with pull-up resistors or anything special?

Beginning to wonder if there’s a problem with my Spark Core or HMC5883L, but the latter works fine with all of the Arduinos I’ve tried…

Wired up to SDA and SCL with 4.7k pullups. Wire from SDA to SDA and SCL to SCL with the resistors on the HMC5883L side connecting to 3.3V.

1 Like

You are amazing @cloris!!! I had tried no pull-ups and I’d tried 10k pull-ups and neither worked, but 4.7k did the trick!!!

So…my question to you is how did you come up with 4.7k?

Thanks again!

1 Like

I wish I could give you a solid answer, but they were the resistors I used in a Netduino project with I2C. They were used in the build I was following from a blog article.

1 Like