Giving a unique ID to identical devices

Hi. I’m working on a project which involves eight devices, each controlled by its own Nano, which will communicate via RF with a base station.

The code deployed on each device will be identical, save for a unique numerical ID which will allow each to identify itself.

It’s going to be messy maintaining eight copies of the code as I develop and test. Is there something simple I can do electronically / in hardware to allow each device to identify its own ID, without eating up several inputs? They don’t need to be switchable - device 3 will always be device 3, for example.

Thanks in advance!

  • if you have three free digital inputs, you could wire up a number from 0 - 7 for each Nano. or
  • write an ID code into the EEPROM for each device.

Pete

I am doing something similar with a radio control system for my model trains. As I have only a few locomotives I decided it is easier to enter the ID code within the program code. I don't keep several copies of the code. I simply have the ID value (byte locoId = 66) near the top of the code and I change it before I upload the code to the relevant device.

The reason I have a code of 66 (rather than values below 48) is so that it produces a readable character for debugging purposes.

Another option for uniqueIDs would be to include the ability to add a code (perhaps from the Serial Monitor) and have the device save the value in its EEprom. In my case my PC sends wireless commands to the locos for direction and speed. I could have a special command code that causes it to change its ID code. Something like this would be essential if you were distributing devices to others so that they could input their own IDs.

But there is a risk that you would lose control of the device if the ID got corrupted so some form of "hardware" reset to an known value would be desirable.

...R

Robin2 has the right idea. I've implemented this sort of thing several times. Here's what I do..

Have an ID that is a default ID. Every once in a while, (seconds, minutes?) send out a query to see if that default ID is on line. If it is, send a command and a new ID to that address. The new modules should be added one at a time, so they don't all answer at once.

Alternatively, you can have a new module that does not yet have its permanent ID, send in an "I'm here and I need an address" packet, sending until a response gives the new ID. If you have predictable minimum times of no transmission, you can use the quiet times to send the "I'm here" packets. With this methodology, there is no need to poll from the PC.

For eight devices, I’d go simple and store the ID in EEPROM with a sketch that does nothing else. Tweak it to change the ID, upload & execute eight times, job done. As far as I can see, no need in this case for anything more sophisticated.

lar3ry: Robin2 has the right idea. I've implemented this sort of thing several times. Here's what I do..

Strictly speaking, the preferred idea in my post was just to incorporate the ID into the code before uploading the code to a device. The other options are far more complicated when there are only a few devices involved.

...R

My projects inevitably contain a simple serial debug interface that accepts one-character commands to turn on/off debugging details and to perform diagnostic tests and the like. Where a post-programming configuration is needed, I do this in the serial interface.

Thanks for your replies, folks.

Robin2 - I take your point about simply adjusting one variable before uploading my code. That would be pretty easy to manage.

However, I'm always keen to push the envelope so do find the eeprom suggestion intriguing! I didn't realise there was permanent storage in the Arduino which could be addressed by my code.

So if I understand correctly, I write a very simple program which writes an ID to the eeprom and run it once on each device to set the ID. I then upload the same code to each device, which contains a line to read that ID on startup and set a constant which is used throughout the rest of the program?

So if I understand correctly, I write a very simple program which writes an ID to the eeprom and run it once on each device to set the ID. I then upload the same code to each device, which contains a line to read that ID on startup and set a constant which is used throughout the rest of the program?

That's it.