Arduino Ethernet MAC Address Storage Library

Ethernet interfaces must have a unique hardware address to communicate on the local network. When using a Wiznet based Ethernet connection (either bare module, shield or integrated board) as part of the hardware initialization you must provide the hardware address, or MAC (Media Access Control) address.

MAC addresses are 48-bits, most commonly represented as 6 bytes displayed in hexadecimal. Newer Arduino Ethernet Shields and Arduino Ethernet boards ship with a MAC address on a sticker. Easy for you to read, hard for your program to read. If you have multiple boards you wish to deploy the same software on you’ll want a way to keep the code portable between your boards.

While you could just change the address in code, that requires individual configuration and recompiling for each board. Instead, much like other configuration or user preferences you ought to consider storing the MAC address in nonvolatile memory.

The AVR chips the Arduino is based on has a limited amount of EEPROM memory (512 bytes on atmega168, 1k on atmega328 and even more on the atmega1280 and atmega2560 chips). By storing the address in EEPROM and reading the address before initializing the Ethernet interface you can minimize errors later if you need to change software. Best of all, during normal flash programming the EEPROM is left intact so you can change your program but retain the MAC address (and any other configuration you might store).

I have written a library for Arduino which lets you store and retrieve a MAC address in EEPROM. I choose to store the address in the last 8 bytes of EEPROM memory. For data integrity the value is stored with two checksum bytes. During retrieval the checksum is calculated on read and compared with the stored values and an error case returned if there are any mismatches. During storage the MAC address is written with checksums calculated and before signaling success the MAC address is reread and checked against the checksums.

The library can be found on GitHub here: EtherMac

Please see the included examples for usage.

You can read more about MAC addresses on Wikipedia.
Learn more about Arduino Ethernet.

Comments are closed.