Wire(I2C) , Can the slave decide when to pass data?

I v been working with I2C for a while now. Can the slave decide when to pass data? Obviously the master is the device that normally decides when to request data but shouldnt it be the other way around? If a slave device is typically used to collect data, then when that data is available, send it to the master.

So as usual with programming i v tried to work around this. I have tried setting a boolean variable on the slave to only allow the data to send when i want. So the master is set to constantly request data from the slave but in the slave method "onRequest" i only allow data to send when a boolean variable is true. This sometimes has unexpected results which I tried setting the boolean to volatile as its a changing variable in an interrupt but still get strange results.

How are people generaling sending from a slave WHEN you want to? I thought there would be an easier way of doing such a basic task?

I have also tried mulitmaster but my application needs mutiple slaves to send data to a master.

Usually slaves have a flag in one of their register to indicate that data is ready. The master can read this flag to know it.

Or, if your system is simple (only one master and one or two device), you can add a line which is pulled high by the slave when data is available. This line would be hooked up to an interrupt pin on the master and thus trigger it when data is available.

But often, you can't modify the slave if it's a sensor bought ready to use, for example. But many sensors already have one or two interrupts output for such purpose.

You're calling it a "basic task" but it's totally in opposition with the concept of I2C, which is a MASTER/SLAVE bus. The names speak for themselves: the master requests, the slaves execute.

Why exactly do you need to send data when it's available? Why can't you request data from slaves based on a sampling time?

I have also tried mulitmaster but my application needs mutiple slaves to send data to a master.

So treat the slave Arduinos as I2C multiple masters and the central Arduino as a single I2C slave?

Sacha22: Usually slaves have a flag in one of their register to indicate that data is ready. The master can read this flag to know it.

Where can i find this? How do i access the register?

Sacha22: Why exactly do you need to send data when it's available? Why can't you request data from slaves based on a sampling time?

If you were to use an application where for example a distance sensor came in a range, you may want to trigger something else over I2C. Or if you had a value from a sensor that you wanted to tell another microcontroller what the value was when you got it.

Hackscribble: So treat the slave Arduinos as I2C multiple masters and the central Arduino as a single I2C slave?

The way i have my application set up is suited for multiple slaves.

romarshmallow: Where can i find this? How do i access the register?

What is your slave exactly? An IC? A MCU with a code you made? If it's an IC, communicating with I2C is basically accessing registers and you can find it in the datasheet.

romarshmallow: If you were to use an application where for example a distance sensor came in a range, you may want to trigger something else over I2C. Or if you had a value from a sensor that you wanted to tell another microcontroller what the value was when you got it.

You can trigger something or sample the value and trigger an event in the master after processing the value. But that's more time consumming. Once again, what is your slave exactly?

The way i have my application set up is suited for multiple slaves.

I don't think you can have it both ways :)

Either the central device is the I2C master and polls the multiple I2C slaves (so the centre determines when data is sent to or requested from the other devices).

Or the central device is the I2C slave and the multiple devices decide when they send to or request from the centre.

And in support of what Sacha22 said ...

Why can't you request data from slaves based on a sampling time?

How frequently do you need to be able to get data from the slave devices? There is an overhead of polling the slave devices when they don't have new data, but it may not be material.

I have tried setting a boolean variable on the slave to only allow the data to send when i want. So the master is set to constantly request data from the slave but in the slave method "onRequest" i only allow data to send when a boolean variable is true. This sometimes has unexpected results which I tried setting the boolean to volatile as its a changing variable in an interrupt but still get strange results.

This sounds like a reasonable approach, so the problem could just be a bug in how it's implemented. Could you post your code (master and slave) so that people could help with it?

So i figured out what i was doing wrong. It turned out that it was a silly assumption i made about a timer in my code. I assumed that the millis() function started when called as opposed to it recording since the program started. So I2C values were being sent at a weird time.

So, yes you can use a boolean value to decide when you want the slave values to be sent by having the master constantly request from the slave. :)

Thank you for the responses.

romarshmallow: If you were to use an application where for example a distance sensor came in a range, you may want to trigger something else over I2C. Or if you had a value from a sensor that you wanted to tell another microcontroller what the value was when you got it.

Before adding complexity, think long and hard about whether you really NEED to get "instant" response. People tend to think they need to react very quickly to things when, in fact, they don't. If you're sensing motion, and want to do something based on reaching a specific position, unless it's moving at very high speed, a 10mSec, or even 100mSec delay in sensing the event will make absolutely no difference in system performance. In the case of distance measurement, the error inherent in the distance measurement itself likely FAR exceeds any error introduced by a small delay in detecting or reporting the event. Just because a sensor reports some parameter to three decimal places, doesn't mean the reading is accurate to three decimal places.

One thing I've learned in 35 years of engineering - unnecessary complexity kills more projects than anything else. If a particular approach required extreme precision or extremely high performance, it's likely the wrong appraoch to being with. My philosophy has always been: If it's complicated, you haven't done it right. The simplest solution is pretty much always the best solution.

Regards, Ray L.