4

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?

Kadin
  • 217
  • 1
  • 4
  • 10
  • 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 06: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 08: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 08:46
  • @KyranF I've bought this magnetometer from Sparkfun (see link), not sure about address selection sorry. https://www.sparkfun.com/products/10530 – Kadin Sep 22 '14 at 08: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 08:54
  • 1
    Have a look at this [I2C address translator LTC4316](http://www.linear.com/product/LTC4316). It came out fairly recently. – Nick Alexeev Feb 02 '16 at 04:39

4 Answers4

1

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!

KyranF
  • 6,248
  • 16
  • 25
  • 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? – Kadin 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
1

Common ways:

  • Use two i2c channels. Either hardware or bitbanged/software i2c. It's not complicated protocol and there are plenty of libraries for this.

  • Use a hardware i2c buffer/bus switch/multiplexer etc. There are many names for the same thing. Some are controlled by an external gpio, others can be controlled through their own i2c address.

  • As @Jjones mentioned, a bridge would also work. Dedicated or spare micro controllers could be turned into a protocol bridge, like i2c-spi, or serial-i2c, or whatever.

  • Finally, many manufacturers have alternative address versions of the same chips.

Funny enough, that sensor has plenty of unused pins that could have been used for setting an address, but they decided not to for some reason.

Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
Passerby
  • 72,580
  • 7
  • 90
  • 202
1

The LT4316 is your answer. http://cds.linear.com/docs/en/datasheet/4316fa.pdf

This amazing little chip is a nearly passive solution. Just used it on a board (ended up actually setting my shift to zero and using it as a buffer). What I mean by nearly passive is that you can just set your i2c address shift with resistors and the change is invisible to your code.

The shift only applies to the address itself and then it passes through the data unchanged.

enter image description here

scld
  • 2,216
  • 15
  • 17
0

You could use 2 arduino and communicate thru SPI with each one hosting I2C slave device.

jjones
  • 9
  • 1
  • 2
    Hopefully you mean a bare bones arduino instead of a 30 dollar uno or whatever, cause that's gotta be painful on the wallet. – Passerby Feb 02 '16 at 04:16