How do you use the "wire" function?

I was wondering how to communicate with I2C devices with the “wire” library function. For example, how do you know what adress to use to communicate? Is the adress supposed to be typed ON it somewhere? :-? I was also wondering if you can use I2C sensors with an arduino, do you just say it’s a slave? :question

I can’ think of a device that has it’s i2c address written on it, unlike, say,
the Dallas Semi 1-wire devices. The device’s i2c address is more than
likely stated in the spec sheet for the device. Sometimes all the bits are
specified in the device, making it difficult to have multiples of those
devices on the same bus. Other times there are address pins on the
device that you wire to Vcc and/or ground to set the device’s address.
Many devices have a combination of the two – some bits set in hardware,
the lower bits decided by how the address pins are wired.

Ooops, almost forgot. I2C devices that you hoook up to an Arduino will
pretty much be considered “slaves” with the Arduino being the master
of the bus.

That you very much! :sunglasses:

I couldn’t get an I2C device to work so I just wrote a small loop that sent a message to every I2C address in sequence from 1 upwards until I got a response from the device.

After every write to the device, print the address to the serial monitor and it will not take long to find the correct one.

Keep in mind that a command with address zero will be listened to by
all devices on the bus. A zero is a broadcast address and all devices
will listen to that. This is useful for those “hacks” where there’s only
one device on the bus and you don’t feel like trying to figure out it’s
address. :slight_smile:

Oh! That “0 adress” is very useful. :slight_smile: I should try that!

Be aware that not all commands in the slave device will respond to
address zero. Commands that return data, like a memory device that’s
commanded to read ‘n’ bytes of memory and return it to the master.
This could be disasterous if all memory devices start returning ‘n’ byte
values all at once.

It would be best to determine the address of the device(s) and use that
for all commands afterwords.

Good luck!

I was thinking of using a I2C ultrasonic sensor (made for the Mindstorms NXT :)). could you a code example of how I could do that?

I would also like to know how to do the address scan thing. :stuck_out_tongue:

I have done a quick search on these sensors and there appears to be plenty of people trying to connect them to avr’s but most people are reporting that the I2C timing has to be altered from standard.

Try Googling/Yahooing/Binging for ‘NXT ultrasonic sensors’ info and you will have more than enough to read about for a while.

Would a sensor like the ultrasonic sensor have RX-TX?