10 years, 3 months ago.

How do I write using I2C

I am using HMC5883L Compass for a project which used I2C communications, I am trying to communicate with it but I am unable to work out how to speak to it.

I have looked at the I2C library and i understand most of it, but i dont know where to get the address from:

<<code title=I2C example>>i2c.read(address, data, 2);<</code>>

How do I get the address of the device connected in order to speak to it? I understand how to do the rest of it, just not how to get the device address to send the data too.

1 Answer

10 years, 3 months ago.

The device address is given by the datasheet of your IC, it is fixed. Note that mbed uses the 8-bit I2C address, instead of the more common 7-bit address. It is the same, only shifted one to the left (so twice as large). The LSB is irrelevant.

In your case from the datasheet, which in this case is given already as 8-bit address:

Quote:

I2C Address 8-bit read address 0x3D

8-bit write address 0x3C

Since the LSB doesn't matter, you can either use read or write address, the library sets it correctly. Normally you use the write address (where the LSB is zero), but it doesn't really matter.

Accepted Answer