What is a good way to get serial number into my boards

We are building several 2560 boards. Now people here suggested serial numbers in them, that is, each board should have an individual number somewhere where the SW could read it.

2560 has some EEPROM, why it is almost hidden. Software interface for the EEPROM was hard to find.
I know that each STM32 ARMs have an unique number in them. There is no such in ATMEL CPUs I think?
What about serial number chip, like an ROM or PROM with only unique number stored in them.
I could store the number in program memory too, I guess.

A chip with serial number doesn't need any extra program to put that number into it, but EEPROM or program memory would need it.

The EEPROM is not hidden - your sketch can write it freely. You do need to either upload separate f/w to write to it initially, or when you're programming the boards via ICSP before you ship them out, you can have it write the EEPROM (the bootloader can't IIRC, at least on the Uno/Nano/ProMini and I think the Mega as well - this is to reduce the flash used by the bootloader - at least on Optiboot (used on Uno), you can't get it to write the EEPROM through the bootloader and have the bootloader fit in 512b - you need to step up to the next size, 1024b).

You can also put the serial number into the flash directly and read it with the appropriate function (depending on the archtecture, the details are different - classic AVRs use the pgm_read_x_near function if it's below 64K, pgm_read_x_far if it's above 64K, where x corresponds to how much data you're reading at a time)- if you're programming these things automatically, you can make your upload scripts modify the hex file before upload (this is what has always been done when i've been working on something that's done as a commercial product; the case that I can remember off the top of my head, we stuck it in the gap between the end of the bootloader and the end of the flash - the guy who was actually doing the f/w upload used a tool to write the flash which could be configured to put a sequential serial number into a specified location in the flash automatically. Not sure if that was a commercial program (he had no qualms about splashing out cash for special build/upload tools), or just a python script - there's a python library that makes it super easy to work with hex files for things like this).

Also, one trick I have heard of, but not used myself, to give a board a unique number is to add a DS18B20 one-wire environment sensor to it. These have a unique 64 bit serial number which can be easily read.

Hi,
What is wrong with;

void setup()
{
  Serial.begin(115200);
  Serial.println("filename");
  Serial.println("serialnumber");
}

void loop()
{
  // put your main code here, to run repeatedly:


}

I do this, without serial number, to all my code.
So I just have to connect USB and reset the controller, and the filename and serial number pops up.

Tom.... :slight_smile:

Of course, it all depends on what the purpose of the unique number is.

If the application consists of a number of distributed devices (say fire detectors) it can be useful to know which one triggered the alarm. Of course there are many different ways on injecting such a code into the device but having a ready made one could be convenient.

A unique number could also be used for a security code (pipe address) in radio device such as an NRF24L01.

It could also be used as a "dongle" to prevent some compiled code being used (easily) in an "unlicensed" device.

etc. etc.

You can use a DS18B20 (if you use one to measure temperature with your board: there's your serial number!), you can also opt for a DS2401, the only thing that part does is offer a guaranteed unique serial number. It's read over the same OneWire bus as the temperature probe.

The ATmega328PB (an upgrade to the ATmega328P used the Uno/Nano/ProMini Arduinos) and ATmega324PB come with serial number (aka unique ID) built in. I don't know if any other AVRs have this feature. The old ATMega2560 which is used in the Arduino Mega definitely doesn't.

Watch for upgrades to the 2560 chip. MicroChip is adding features to the old Atmel stuff to bring it in line with their way of doing chips. The 328pb has a unique address

and this guy says there is one in the 2560 SerialNumber
[edit but the comments say it is not globally unique]

Many devices already have unique numbers. it may be that some peripheral you are using will have one.

Each ESP8266 has a unique MAC address

You can buy tiny chips from MicroChip that are unique serial numbers
the 24AA02E48(and it's brothers) is I2C and cost is minimal. Less than a DS18B20

But the easiest would be if the chip you have does not have one, the DS18B20 and it only uses 1 pin.

Many of my projects have several (nominally) identical pieces of hardware. But they aren't identical. Serial number 1 has a fault on the PCB fixed by jumpering across to a different Arduino pin and Serial number 2 changed one switch from active-HIGH to active-LOW.

I don't want to edit those places in the software each time I upload code to the device. Serial #1 is still useful as a bench-test unit even if it can never leave my bench due to that fault. So those "configurations" are stored in the EEPROM, which isn't erased when new code is uploaded.

Since I already have an EEPROM configuration system in the software, it's trivial to put the serial number in the EEPROM. Then I can get each device to print out its current configuration every time it starts up. I can go to a device I installed years ago and see what it is configured for.

Another useful trick is the DATE and TIME macros. These show you the exact date and time the code was compiled.

void setup() {
  Serial.begin(9600);
  while(!Serial && millis() < 2000) {} //wait up to 2 seconds for Serial to connect on native-USB boards
  Serial.println("Name of Project");
  Serial.print("Compiled on ");
  Serial.print(__DATE__);
  Serial.print(" at ");
  Serial.print(__TIME__);
  Serial.println();
}

void loop() {
}

It happens that we already have one DS18B20 on the system. This is good. Let's see if that's enough.

Regards
LM