Question about I2C master and can it keep track of the order in which slaves are attached

I want a Master Arduino Mega to have 8 Arduino Mega slaves.

Can the master Arduino Mega in this case keep track of the order that the slaves are attached? My project requires changing the order that the Arduino Mega 8 slaves are attached all the time but I dont want to reprogram every time. Their addresses dont change.

Iam wondering if the Master Mega can keep track of or detect the order in which the slaves are connected not just the Addresses or do I need more hardware for that? Thank you.

You have 8 I2C Slaves with their own unique addresses. So, put tags on the slave MEGAs.

Please explain.

You poll all the time for newly attached slaves.

1 Like

I am not familiar with the term "poll" can you explain what that means?

Check in every iteration of loop().

You are expecting a guest at your house between 10 am - 11 am. You are checking again again (may be at 2 min interval) if the guest has arrived. This is known as polling. It is fine if you have no other job to do; otherwise, it is a wastage of time.

The alternate way is to allow the guest to inform you about his arrival by pushing the Calling Bell. This is known as Interrupt which is efficient and does not kill your valuable time during which you can do some other jobs.

What would mean that each new station sends on power up a message to the central.

1 Like

That rings alarm bells for me and many other forum members. It sounds crazy. Why are you doing that? When new forum members explain why they want to do similar things, there are usually better, easier, cheaper ways to achieve it.

Usually, components of a circuit should only be added or removed when power is disconnected, for safety reasons. Designing a circuit where components can be "hot" connected or disconnected must be done with great care to avoid damage or unexpected behaviours.

Are you planning to add Megas to a live circuit? Or will you power down before each slave Mega is added, and you need the master Mega to remember what slaves were previously connected when powered up?

The latter can be done using the Mega's EEPROM memory. The master Mega could scan the i2c bus at startup (in a similar way to how the i2c scanner sketch works, but limited to just the 8 expected slave addresses). It could save these results in EEPROM, so that when next started up, it can scan again and detect what new slave has been added and record the sequence they were added.

If the design imposes that Master expects such messages.

Why not?

Then not using I2C bus by the Slave.

Why not?

Does I2C Protocol in Arduino Platform support multi-master scheme? If not, then Slave is unable to bring START Condition on the I2C Bus and hence the generation of SCL pulses.

Yes. Why not?

The IDE examples include both master and slave, so each Arduino can act as either.

It is turned off before they are rearranged so safety should be fine. I have no need to remember what the arrangement was before I turned it off. I only need to know what the new arrangement is upon powering up so I can direct the slaves to do different things based on their new arrangement.

Do you want that each connected Arduino should execute Wire.begin(slaveAddress) and then poll the bus signal (SDA/SCL) for quiet condition (Fig-1) and then make a roll call to the target Arduino prior to data exchange? If yes, I will test this concept.


Figure-1:

I cant be sure what you are suggesting is what I want. You got more advanced in your thinking about this subject than I am. I am not sure what a "roll call" would look like.

Assume that you have a Master MEGA and 8 Slave MEGAs with 7-bit slaveAddresses as: 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27. You communicate with them one-by-one in sequence or in random.

Hi, @ncc1701d
Welcome to the forum.

Please explain you project in full.
What are the slaves, sensors?

Why does your project require that you change the order of the slaves?

Thanks... Tom.. :grinning: :+1: :coffee: :australia:

2 Likes