4
\$\begingroup\$

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?

asked Sep 22, 2014 at 6:29
\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 22, 2014 at 6:57
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Sep 22, 2014 at 8:39
  • \$\begingroup\$ Where did you buy it from? Is it on a breakout board? Are there address selection pins/resistors/pads available? \$\endgroup\$ Commented Sep 22, 2014 at 8:46
  • \$\begingroup\$ @KyranF I've bought this magnetometer from Sparkfun (see link), not sure about address selection sorry. sparkfun.com/products/10530 \$\endgroup\$ Commented Sep 22, 2014 at 8:49
  • 1
    \$\begingroup\$ Have a look at this I2C address translator LTC4316. It came out fairly recently. \$\endgroup\$ Commented Feb 2, 2016 at 4:39

4 Answers 4

1
\$\begingroup\$

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!

answered Sep 22, 2014 at 9:00
\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Sep 22, 2014 at 10:59
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Sep 22, 2014 at 11:34
1
\$\begingroup\$

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
184k17 gold badges248 silver badges431 bronze badges
answered Feb 2, 2016 at 4:29
\$\endgroup\$
1
\$\begingroup\$

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

answered Feb 2, 2016 at 16:27
\$\endgroup\$
0
\$\begingroup\$

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

answered Feb 2, 2016 at 4:11
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Hopefully you mean a bare bones arduino instead of a 30 dollar uno or whatever, cause that's gotta be painful on the wallet. \$\endgroup\$ Commented Feb 2, 2016 at 4:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.