Get ESP32/ESP8266 MAC Address and Change It (Arduino IDE)

This guide shows how to get the ESP32 or ESP8266 boards MAC Address using Arduino IDE. We also show how to change your board’s MAC Address.

Get ESP32 or ESP8266 MAC Address and Change It (Arduino IDE)

What’s a MAC Address?

MAC Address stands for Media Access Control Address and it is a hardware unique identifier that identifies each device on a network.

MAC Addresses are made up of six groups of two hexadecimal digits, separated by colons, for example: 30:AE:A4:07:0D:64.

MAC Addresses are assigned by manufacturers, but you can also give a custom MAC Address to your board. However, every time the board resets, it will return to its original MAC Address. So, you need to include the code to set a custom MAC Address in every sketch.

Get ESP32 or ESP8266 MAC Address

To get your board MAC Address, simply upload the following code to the ESP32 or ESP8266. The code is compatible with both boards.

// Complete Instructions to Get and Change ESP MAC Address: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

View raw code

Demonstration

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the on-board RESET or EN button.

The MAC Address should be printed in the Serial Monitor as shown in the following figure.

Get Obtain ESP32 or ESP8266 MAC Address Physical using Arduino IDE

That’s it! Now, you know how to get your ESP32 or ESP8266 board MAC Address.

Set a Custom MAC Address for ESP32 and ESP8266

In some applications, it might be useful to give your boards a custom MAC Address. However, as explained previously, this doesn’t overwrite the MAC Address set by the manufacturer. So, every time you reset the board, or upload a new code, it will get back to its default MAC Address.

Change ESP32 MAC Address (Arduino IDE)

The following code sets a custom MAC Address for the ESP32 board.

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <WiFi.h>
#include <esp_wifi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  
  esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

View raw code

You can set a custom MAC Address in the following line:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

After uploading the code, open the Serial Monitor at a baud rate of 115200. Restart the ESP32 and you should get its old and new MAC Address.

Change and Set ESP32 MAC Address with Arduino IDE

Change ESP8266 MAC Address (Arduino IDE)

The following code sets a custom MAC Address for the ESP8266 board.

// Complete Instructions: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#include <ESP8266WiFi.h>

// Set your new MAC Address
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  
  Serial.print("[OLD] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  // For Soft Access Point (AP) Mode
  //wifi_set_macaddr(SOFTAP_IF, &newMACAddress[0]);
  // For Station Mode
  wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
  
  Serial.print("[NEW] ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

View raw code

Set your custom MAC Address on the following line:

uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};

After uploading the code, open the Serial Monitor at a baud rate of 115200. Restart the ESP8266 and you should get its old and new MAC Address.

Change and Set ESP8266 MAC Address with Arduino IDE

Wrapping Up

In this quick guide, we’ve shown you how to get your ESP32 and ESP8266 manufacturer MAC Address with Arduino IDE. You’ve also learned how to set a custom MAC Address for your boards.

Learn more about the ESP32 and ESP8266 boards:

Thanks for reading.



Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

6 thoughts on “Get ESP32/ESP8266 MAC Address and Change It (Arduino IDE)”

  1. Thank you very much Sara.
    A very interesting idea and i personally intend to use it in my projects.
    As some of my projects are MAC address dependent, in the unfortunate case of a chip failure, i have to go and change the code again for the new chip and put the new MAC address of the chip.
    With your solutions, i can replace 25 chips or more in the same projects, without modifying the new code.
    Great idea and i love it.
    Let’s say you make a product and sell it to 100 or thousands , or more customers all over the world using Ebay, Amazon, KickStarter or other mass selling companii. You do not have always the user address.
    Then …. the accident has happened and the board is destroyed.
    You are asked to provide a new board, which will not work because it has a new MAC.
    With your suggestion, , you are safe and whatever board you send will always work.
    You look on your database, see what was the MAC address used for that customer/board and program it on the replacement.
    You guys are genius.

    Reply
  2. Hello,
    I have a question.
    I used an example from the web and,
    First, they have a soft to get the MAC, but they GET 2 MAC
    one SOFT and one AP
    Which is the one to use?
    On their application i had to use the SOFT which is 2 bits higher than AP
    Let’s say AP MAC start with 60, then SOFT is 62.
    The rest is the same, but it is confusing.
    On your example it looks like i get the AP MAC address .
    I got lost here and i need some help.
    Thank you

    Reply
  3. Hello,
    i am building a “digital twin” of an LEGO technic Hub, by using an ESP32.
    Target is that ESP32 can be connected to LEGO BLE Apps.

    At least i have concerns, that one reason i did not get a connecteion request is because LEGO Apps using Address Filters .
    An Example of a LEGO HUB Adress is like:Advertising Address: LegoSyst_4a:3a:0c (90:84:2b:4a:3a:0c) ( a hub owned by me)
    As you may know ours is like: Advertising Address: Espressi_cb:69:fa (bc:dd:c2:cb:69:fa)

    Question is, how to change Advertising address is scetch to be shown as “LegoSyste” like : 90:84:2b:aa:bb:cc for example

    Thanks in advance

    Marc

    Reply
  4. Thank you very much Sara.
    I have a project that needs to permanently change the MAC address of the equipment vendor. I don’t know how to do it. Can you help me?

    Reply

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.