Arduino Serial Number

Arduino IDE showing the the serial number in the serial console.
Arduino IDE showing the the serial number in the serial console.

When building test equipment it is important to be able to serialise it so that you can keep track of calibration data against a specific serial number (ISO 17025, ISO 9000, ISO 9001). so further to my post about storing firmware version information in the compiled hex, you can also ask an Arduino to give you an (almost) unique ID, this ID is burnt into the silicone during manufacture and can not be changed.

For this, we use the ArduinoUniqueID Arduino library, The Unique ID library is a little out of date, so may not work on the Arduino Uno R4, but since I do most of my development on the Arduino R3, it works nicely to give me the unique serial number.

Additions to the code for the Serial Number

In addition to the last code, we need to add a function called version() that is called in setup(). I have also moved the CompileYear code to give me just the 4-year part of the date string to this function.

void version() {
  CompileYear = CompileYear.substring(7, 11);

  Serial_Number = "";  ///< empties the string removing the -1 value we filled it with,

  for (size_t i = 0; i < UniqueIDsize; i++) {
    Serial_Number += String(UniqueID[i], HEX);
  }
  Serial_Number.toUpperCase();
}

When we initialised the Serial_Number string we initialised it with the value -1, which allows us to know it was taking a value, and returning it when called. the first thing we do is now empty this string, then we step through the UniqueID and add each hex value at a time to the Serial_Number string. The last thing we do is convert the entire string to uppercase.

Putting the code together

We now have two parts of the basic code for allowing traceability of which hardware and firmware are being used in the test fixture.

#include "Arduino.h"
#include "./src/ArduinoUniqueID/ArduinoUniqueID.h"

/// These are found and replaced by the compiler.
#define CompileDate __DATE__
#define CompileTime __TIME__

/// These are for returning copyright information, and storing this information in the code.
const String Copyright = "Philip McGaw"; ///< Copyright company / person's name.
String Product = "ProjectName";          ///< Project name. (Updated when I make a release).
int Hardware_Rev = -1;                   ///< Placeholder - Takes the value of the VERSION_PINS.
String Serial_Number = "-1";             ///< Placeholder - This is the serial number of the microprocessor.
const int Software_Version = 1;          ///< Firmware version. (Updated when I make a release).

String CompileYear = CompileDate;

void setup()
{
  version();                              ///< Moves product population and version into memory.

  Serial.begin(9600);
  Serial.println((String) "© " + Copyright + " " + CompileYear + " - " + Product);
  Serial.println((String) "Software Version: \t\t" + Software_Version + " (" + CompileDate + " " + CompileTime + ")");
  Serial.println((String) "Hardware Version: \t\t" + Hardware_Rev);
  Serial.println((String) "Serial Number: \t\t\t" + Serial_Number);
}

void loop()
{
}

/**
 * Works out the hardware version, and the Serial number of the microcontroller.
 *
 * This code is run once during setup.
 */
void version()
{

  CompileYear = CompileYear.substring(7, 11);

  Serial_Number = "";  ///< empties the string removing the -1 value we filled it with,

  for (size_t i = 0; i < UniqueIDsize; i++) {
    Serial_Number += String(UniqueID[i], HEX);
  }
  Serial_Number.toUpperCase();
}

The output of the above code should look like this:

Arduino IDE showing the the serial number in the serial console.
Arduino IDE shows the serial number in the serial console.

The last post on this three-post series will be the details on how to report the hardware version of the test equipment that the controller is attached to.

Leave a comment

Your email address will not be published. Required fields are marked *

The maximum upload file size: 20 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop files here

4 thoughts on “Arduino Serial Number”