Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I want to use two of the same magnetometer (HMC5883L) with my Arduino, but I cannot figure the code for calling each of them separately.

I have read online that connecting multiple devices is completely doable, as long as you call their respective addresses, the issue that I'm having is that because I'm using two of the same device they each have the same address.

So my questions are, can I change the address I2C address of one of my magnetometers and not have it break, how do I do this, is it a permanent change, and if so can I revert the HMC5883L back to default if need be?

share|improve this question
    
I don't have time to research a proper answer at the moment, but after taking a look at the datasheet, I think that it's possible to change the address of a unit. They explicitly mention default factory set address at one point. –  AndrejaKo Sep 22 '14 at 6:57
    
@AndrejaKo I looked through the manual and I don't see any supported mechanism for changing the I2C address. There are 3 identification registers, but these appear to be unrelated to the I2C read/write addresses. –  helloworld922 Sep 22 '14 at 8:39
    
Where did you buy it from? Is it on a breakout board? Are there address selection pins/resistors/pads available? –  KyranF Sep 22 '14 at 8:46
    
@KyranF I've bought this magnetometer from Sparkfun (see link), not sure about address selection sorry. sparkfun.com/products/10530 –  user2222956 Sep 22 '14 at 8:49
    
The fact that it is not documented does not necessarily mean that it is not implemented. It might be an NDA information. But still there would be a problem, how to change the address of one device if there are two on the same bus? –  venny Sep 22 '14 at 8:54

1 Answer 1

up vote 1 down vote accepted

I just read the datasheet, without some external hardware (like, some kind of multiplexing buffer with channel select and chip enable) you cannot have two of these devices on the same I2C bus.

The device you are using has a fixed, factory set address. There are no ways to change the address by software or even by external pins to adjust it's 7-bit I2C Bus address.

More complex ATMEL 8-Bit AVR like the XMEGAs have multiple I2C interfaces, so with those you could have two devices, one per channel. Same with the simple and smaller ARM Cortex M0 -> M3 for example, they all have multiple bus interfaces that can deal with this issue.

Something can do with a bit of hardware and software is to have an IC which blocks off the I2C Serial Clock (SCL) to either one or the other and alternate which one is receiving the clock signals and therefore able to receive and respond to commands. I guess a simple dual MOSFET with XOR control at the gates could do it, with simple circuitry. Otherwise some kind of line driver/buffer chip with an enable pin and dual channel/multiplexed output will allow you to switch which output gets the SCL signal.

Either way it's not pretty. You can always find a second, but similar IC/module magnetometer that has a different hard-coded I2C address or at least the ability to change it (usually external pin configurations/resistors) to allow multiple on the same bus.

EDIT: Texas Instruments has an I2C Troubleshooting document which on page 8 shows a way to do the multiplexing in a simpler way than I described to split the I2C bus into sub-sections to deal with conflicting slave address issues like what you have.

good luck!

share|improve this answer
    
Thanks for the reply. I'm leaning towards the multiplexing solution as it seems like the simplest to me (without buying a new interface). Would something like the 74HC4051 work fine, or do you have any other recommendations? –  user2222956 Sep 22 '14 at 10:59
    
@user2222956 - search the web for "I2C Multiplexer" or "Bidirectional I2C Buffer" and you'll find a lot of options. The 74HC4051 is an analogue multiplexer, i,e, for switching between audio signals. I skimmed through the datasheet, and it looks like it might work. However, if it didn't it might be hard to debug. –  gbulmer Sep 22 '14 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.