A doubt about I2C and arduino

Hi,
I have been reading, googling for last 4 days to get a perfect idea about I2C communication among arduinos. But, still i couldn't clarify my doubt. All article say about sending data to slave and requesting data from slave. No one said about slave sending data to master without a request. That's why i am posting this.


Hardware setup:

  • I have 4 arduino duemilanove boards. (1-master and 3-slaves)
  • First one attached to an analog potentiometer. (in analog0 pin, its value varies 0-1023 in serial monitor when resistance varies)
  • 2nd and 3rd also attached to another analog potentiometers in analog0.
  • 4th as the master, connected to 3 LEDs at digital pins 8,9 and 10.

All these connected to an I2C bus. And they are working good.


My question:

  • I want the master should be informed when the potentiometer of the 3rd arduino is at a particular range( say, between 188 and 270), and to whitch on 8th pin attached LED. Rest LEDs are off.
  • Also master should switch on 2nd LED when 2nd pot between 1000 and 1023

Actual problem is, slaves want to inform master when an event occur. But, I read so many articles which all say that SCL is controlled by master, not slaves. But in the above situation what should i do ? Kindly help me..Thanks in advance...

No one said about slave sending data to master without a request.

That's because the I2C standard doesn't contain that kind of transfer. That's why it's called "master" and "slave". The master controls the bus and the communication is always the way that the master requests data from the slaves. The only allowed action for the slaves is clock stretching if the master is too fast for the slaves.

You either have to poll the slaves frequently to ask if they had an event occurring or use another communication interface, like Ethernet between the Arduinos.

If you start the Wire library with "Wire.begin(4)", the number '4' is the Slave address. However, you can use the library just like a Master. So every Slave can also be a Master.

If you start the 'Master' Arduino with "Wire.begin(10)", every Slave (as temporary Master) can send data to address 10.

The problem is that the I2C bus is a single Master bus. If two Masters want the bus, you are in trouble.
In the hardware (inside the microcontroller) is some kind of collision detect, but I don't know how well that is implemented in the library.

Normally an extra line is used for interrupt. If the Master gets an interrupt, it requests data from the Slave. Or the Master starts checking every Slave to see which one has new data.

You could also poll every Slave 10 times a second, or even 100 times a second.

Please keep in mind that I encountered a bug, when using Arduinos as Master and Slaves.

CORRECTED: according to reply #3, I ment "Masters" instead of "Slaves". Thanks Nick :slight_smile:

001neeraj:
Hi,
I have been reading, googling for last 4 days to get a perfect idea about I2C communication among arduinos.

Erdin:
The problem is that the I2C bus is a single Master bus. If two Slaves want the bus, you are in trouble.

You can have multiple masters. Two slaves can't "want" the bus. Masters want it.

If a Slave does not release the bus... everything comes to a grinding halt.

Thanks...I understand what you have posted - master should poll the slaves for the data, if they need to send any. How can i do that??.

Wire.requestFrom(x, y);

Above,

  • how do i know the value 'y' if i poll among the slaves??
  • Also how can i know 'x' ??
    Also if i use "Wire.requestFrom(x, y);" Atmega328 repeat the code 1000 times if i place that in void loop(). Does that mean it is itself a polling among slaves?

how do i know the value 'y' if i poll among the slaves??

You have a list of all the slaves connected and you call this method for all known slaves (given all do the same, else you might have to switch to the appropriate subtask) by iterating over the slave array.

Also how can i know 'x' ??

x is the number of bytes you expect from the slave. You should know that because you designed the "protocol" (or maybe better register table) between the master and the slaves. You might have to study a standard way of doing I2C communication. In most cases you have some register values on the slave that may be addressed by the master and depending on the register type it may be read, written or both operations are possible. Usually you write the register address to the slave and after that you request the number of bytes from the slave that is equal to the size of the register.

pwillard:
If a Slave does not release the bus... everything comes to a grinding halt.

What do you mean by "release the bus"?

Slaves respond by sending data, or it times out.

First, are you using pull-up resistors? If so, what values?

There have been reports of the Wire library hanging under certain circumstances. This library is supposed to work around that:

I would like to see some code now. I don't understand this bit:

Atmega328 repeat the code 1000 times if i place that in void loop(). Does that mean it is itself a polling among slaves?

Sorry for the delay in my response...

Atmega328 repeat the code 1000 times if i place that in void loop(). Does that mean it is itself a polling among slaves?

This means that, micro-controller repeat the content of void loop() at very high speed(about micro sec range).
So if i place Wire.requestFrom(x, y);, it will check if there are any inputs from slaves. Is this process is a polling ?....If my concept is wrong, how can i poll among slaves...

The loop() function is called forever in a loop :wink:

Calling the requestFrom in the loop() is polling.

void loop()
{
  Wire.requestFrom( 4, 2);
  if( Wire.available() 
  ...
}