but you would still need to program each slave with a unique identifier. This could be in EEPROM however and so separate from the application
BenF: thanks for the heads up. I looked at the SMBus and ARP is exactly what I need. From the SMBus protocol spec it seems that it is possible to get an address even without a DUID or I could generate one randomly. In any case, I couldn't find an SMBus library for the Arduino however. Do you happen to have any links?This post http://arduino.cc/forum/index.php?topic=61520.0 claims to implement an "I2C address scanner". The idea is that the master goes through all 120 addresses and either gets a response (and therefore an existing address) or doesn't and therefore that address is free. Essentially this gives me device enumeration like the SMBus has. A scanner can only tell you for sure that no one responded to a specific address, but it won't be able to tell if two or more I2C slaves are responding to the same address, there is no collision avoidance protocol in the I2C standard.Another thought is: what if all the slave Arduino's initially start off with a known address (say 01). Then the master periodically checks if any devices are at address 01. The first one to respond is allocated the next free address. There is not 'first one to respond', all slaves with starting address of 01 will try and respond at the very same time, results in garbage data as the slaves step all over each other.I have seen a lot of sites explaining that all devices have to have a unique address but it is unclear exactly what happens if (by accident say) two devices have the same address.The slaves step on each other and garbage data results and the master has no idea why.One way around this is to have all of the devices with address 01 do a random delay before responding. The likelihood of more than one device being the first to respond is very low; generally only one device will win and be first. It seems from the scanner code, that this is not possible but I thought I'd ask anyway.Your trying to wish that I2C was a more powerful protocol then it is. By some method you have to make sure each slave starts up life with a unique address, It can't be orchestrated at start-up by the master and the slaves can't talk to each other to negotiate unique address assignments. You need to do it with either separate sketches for each slave, or some hardware method on each slave board or have a unique address pre written into its EEPROM, etc.JohnA
what if all the slave Arduino's initially start off with a known address (say 01). Then the master periodically checks if any devices are at address 01. The first one to respond is allocated the next free address.
Your trying to wish that I2C was a more powerful protocol then it is.
master is also a slave at reserved address 01 all slaves start using reserved address 02, or the address saved in its EEPROM (03 to 120) // see if any slaves are unallocated master: send a broadcast - Start Allocation message all slaves with address 02: delay for random number of microseconds slaveA (as master): creates a 32-bit random number and sends it to master 01 master 01: associates the random number with next free address slaveB (as master): creates a 32-bit random number and sends it to master 01 master 01: associates the random number with next free address ... repeat until some_magic_condition occurs... // Assign the addresses master: send a broadcast - Start Assignment message master: broadcasts slaveA's random number and address slaveA: sets it's address to the given address; saves the address in its EEPROM master: broadcasts slaveB's random number and address slaveB: sets it's address to the given address; saves the address in its EEPROM ... repeat ... // do it again, just in case master: send a broadcast - Start Allocation no slaves with address 02 should remain if there are any remaining, then this step should allocate them ... repeat until some_magic_condition occurs... // enumerate the slaves master: this should catch both the newly allocated addresses plus the previously saved addresses.
all slaves with address 02: delay for random number of microseconds
Is there a mechanism in I2C to handle that?
ALL nodes running exactly the same software...
How do they know another slave/master has not already started transmitting? Is there a mechanism in I2C to handle that?
The OP seems supremely confident that he can hack the I2C bus protocol. All attempts to discourage / enlighten him seem to fail. He is bringing out all the old bus ideas that were rejected long ago. I don't know maybe he is young and doesn't realise many people have been round that block many times before.
Simply the wrong protocol for the job
He could use I2C to have master assigned slave addresses, by controlled sequential startup of the slaves, as example.
For most practical purposes (as opposed to academic), storing a unique address in EEPROM is a good option.
This generally works when arbitration is only required once every blue moon, but it will not work when two or more devices try to claim the bus at the exact same clock edge (such as may be the case when multiple identical devices are powered on at the same time).
For this some software retry scheme will be required.