Electronic – arduino – How to connect multiple of the same device to an Arduino using I2C

arduinoi2cmagnetometer

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?

Best Answer

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!