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

This guide shows how to get the ESP32 or ESP8266 NodeMCU 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.

Table of Contents

Get ESP32 MAC Address

To get your ESP32 board MAC Address, upload the following code.

/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <esp_wifi.h>

void readMacAddress(){
  uint8_t baseMac[6];
  esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
  if (ret == ESP_OK) {
    Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                  baseMac[0], baseMac[1], baseMac[2],
                  baseMac[3], baseMac[4], baseMac[5]);
  } else {
    Serial.println("Failed to read MAC address");
  }
}

void setup(){
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.STA.begin();

  Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
  readMacAddress();
}
 
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 MAC Address.

Set a Custom MAC Address for the ESP32

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.

/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <esp_wifi.h>

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

// Replace with your network credentials
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

void readMacAddress(){
  uint8_t baseMac[6];
  esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
  if (ret == ESP_OK) {
    Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
                  baseMac[0], baseMac[1], baseMac[2],
                  baseMac[3], baseMac[4], baseMac[5]);
  } else {
    Serial.println("Failed to read MAC address");
  }
}

void setup(){
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  WiFi.STA.begin();
  
  Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
  readMacAddress();

  // Change ESP32 Mac Address
  esp_err_t err = esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
  if (err == ESP_OK) {
    Serial.println("Success changing Mac Address");
  }

  // Read the new MAC address
  Serial.print("[NEW] ESP32 Board MAC Address: ");
  readMacAddress();

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("Connected");
}
 
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};

Attention: the bit 0 of the first byte of MAC address can not be 1. For example, the MAC address can set to be “1a:XX:XX:XX:XX:XX”, but can not be “15:XX:XX:XX:XX:XX”.

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

Change and Set ESP32 MAC Address with Arduino IDE

Get ESP8266 NodeMCU MAC Address

To get your ESP8266 board Mac Address, use the following code.

/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>

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.

ESP8266 Get Default Mac Address

Change the ESP8266 NodeMCU 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 default 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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

41 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
    • Download the binary and edit it as a binary in a hex editor, but they will have to have set the mac themselves, you just need to find it and change it.

      Reply
  5. Thanks, Sara!
    I believe something has broken in the ESP32 code since it was published.


    esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);

    Now causes the error:

    cannot convert ‘esp_interface_t’ to ‘wifi_interface_t’ for argument ‘1’ to ‘esp_err_t esp_wifi_set_mac(wifi_interface_t, const uint8_t*)’


    This used to work for me, hope there’s a quick solution!

    Reply
    • Hi.
      This also used to work for me.
      I also get the same error. I’ll try to investigate and see how to solve the issue.
      I’ll update the tutorial as soon as I can find the issue.
      Regards,
      Sara

      Reply
    • Hi.
      Just replace
      esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);
      with
      esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
      Regards,
      Sara

      Reply
  6. Exellent article.

    Is there any criteria to assign a custom MAC address?

    I was trying to assign the address “0x53, 0x6D, 0x54, 0x00, 0x00, 0x01” to my board, but i noticed that the change only takes effect if i write “0x32, 0x6D, 0x54, 0x00, 0x00, 0x01”

    Is there “a range” of valid values for the first digit?

    Reply
    • Morning Diego

      Had a similar problem, but not only with the first hex values – also with subsequent values. Some new mac addresses work and some don’t. Eventually I found a series of addresses that worked and tested them on several ESP32 boards. Sticking to slight changes to the address given by RNT in their example or sticking close to mac addresses on the ESP32 boards that I have usually works. Creating mac addresses from scratch didn’t work so well.

      I want to set a static IP in my WiFi router for a project, but want to switch the physical ESP32 boards from time to time and retain a common IP address. Still work in progress.

      Reply
    • According to the ESP WiFi API reference for the esp_wifi_set_mac() function @ docs.espressif.com:

      The bit 0 of the first byte of ESP32 MAC address can not be 1. For example, the MAC address can set to be “1a:XX:XX:XX:XX:XX”, but can not be “15:XX:XX:XX:XX:XX”.

      Reply
  7. Hello,
    thanks for perfect tutorial.
    Please is there any way to change MAC on ethernet interface?
    Thanks for help.

    typedef enum {
    WIFI_IF_STA = ESP_IF_WIFI_STA,
    WIFI_IF_AP = ESP_IF_WIFI_AP,
    } wifi_interface_t;

    typedef enum {
    ESP_IF_WIFI_STA = 0, /< ESP32 station interface */
    ESP_IF_WIFI_AP, /
    < ESP32 soft-AP interface */
    ESP_IF_ETH, /**< ESP32 ethernet interface */ <– Need change MAC on this interface
    ESP_IF_MAX
    } esp_interface_t;

    Reply
  8. Hallo Sara
    Ich habe neuerdings ein Problem mit dem Wechseln der MAC-Adresse meiner ESP32 Module. Ich möchte die Bestehende MAC-Adresse (c8:28:96:bd:ed:18) in (aa:bb:cc:dd:ee:ff) wechseln.
    Nach dem Hochladen des Sketches erscheint nach einem Reset:

    [OLD] ESP32 Board MAC Address: C8:28:96:BD:ED:18
    [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

    Das ist gut so.

    Nach einem erneuten Reset erwarte ich:
    [OLD] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF
    [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

    es erscheint aber wieder

    [OLD] ESP32 Board MAC Address: C8:28:96:BD:ED:18
    [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

    Wo ist da der Fehler?

    Vielen Dank

    Liebe Grüsse
    Hans

    Reply
    • Sorry,
      Hier in englisch (Google)

      Hi Sara
      I’ve recently had a problem with changing the MAC address of my ESP32 modules. I want to change the existing MAC address (c8:28:96:bd:ed:18) to (aa:bb:cc:dd:ee:ff).
      After uploading the sketch, the following appears after a reset:

      [OLD] ESP32 Board MAC Address: C8:28:96:BD:ED:18
      [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

      That’s good.

      After another reset I expect:
      [OLD] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF
      [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

      but it reappears

      [OLD] ESP32 Board MAC Address: C8:28:96:BD:ED:18
      [NEW] ESP32 Board MAC Address: AA:BB:CC:DD:EE:FF

      Where’s the mistake?

      Many Thanks

      Warm greetings
      Hans

      Reply
      • Hi.
        As mentioned in the tutorial “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.”

        “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.”

        I hope this is clear.
        Regards,
        Sara

        Reply
  9. Looks like
    esp_wifi_set_mac(ESP_IF_WIFI_STA, &newMACAddress[0]);

    Has been broken again. I am getting the “Undeclared” error. I ran a check on the wifi library, looks like the esp_wifi_set_mac has been removed. I am still searching the internet, for a fix. I am using esp_now.h library as well.

    Reply
  10. Great article, thank you.

    Will changing a mac address like this also work for ESP-Now communication as well as WiFi please?

    Thanks.
    C

    Reply
    • Hi.
      I didn’t try using a predefined MAC Address with this method and ESP-NOW simultaneously.
      So, I’m not sure if it will work or not.
      Regards,
      Sara

      Reply
  11. I do the ESP’s device identification just a bit differently. For my home ESP-NOW STA network I first I change the device’s IP address. Replace the device with a new board and the node’s IP stays the same.

    Then I build the node’s WiFi STA MAC address to something like: 0:0:0:0:0:IP.

    The IP is simply the node’s BOARD_ID as set via a compiler directive in my master platformIO.ini file enabling a new device to configure itself within its Arduino’s core setup routine.

    In the platformIO.ini file, other such compiler directives set the WiFi Channel to use and the IP of the HUB node. The HUB is an ESP32 use in a modified HUB’n Spoke network topology. All ESP32 are setup as M2M-TW. The designated HUB acts a Master to ESP8266 Slave nodes as O2O-TW. The HUB also provides an Internet gateway for the entire ESP Now Node Network, ENNN.

    Instead of ‘ESP-NOW Broadcasting’ I use a DIY ‘multitasking’ scheme. Multicasting enables pseudo-broadcasting (i.e. multicast to all ENNN devices) as well as multicasting to specific groups or subsets of network nodes.

    This brings us back to the custom MAC address. We earlier set MAC[5] equal to the board’s IP, now each device can simply set its MAC[4] to an encoded group ID like, 0:0:0:0:ID:IP where ID is a group identification code maintained in the master platformIO.ini file. Now when multitasking messages, I can specify to which group.

    …still a work in-process.

    Reply
  12. Hi guys!
    I have discovered ESP32 world recently, because I have been focused in the Raspberry pi this last years. I have to say that your articles are really good. Well explained, full of examples and very straight forward. I have learned a lot from them and beg you please continue doing them. I am applying esp-nov comm system in a project and I have to say that the results are really amazing. Thank you very much.

    Reply
  13. Hi, Just want to ask if ever an ESP32 is cloned, will the mac address be cloned also ? If 3 pcs. are cloned, will all 3 ESP32 have same mac address ? Thanks.

    Reply
  14. Thanks a lot for this tutorial
    However, it gave me an error : not delcared in this scope

    So this function worked fine for me:

    esp_base_mac_addr_set(newMACAddress);

    Reply
    • Hi.
      It works just fine for me. I just tested it.
      What version of the ESP32 boards do you have installed? Tools > Boards > Boards Manager > ESP32 (it should mention which version is installed.)
      Regards,
      Sara

      Reply
      • Hi why when I’m going to get my esp mac address its’s words and letters are unreadable like squares triangles semicolon and etc?

        Reply
  15. hi @Sara santos
    I tried this code for getting my ESP32 mac address but the words are not readable like belove:
    @⸮⸮I\
    bb⸮x$+⸮A⸮ \⸮B⸮⸮ʬ⸮⸮%⸮⸮$⸮⸮⸮⸮⸮RqzY
    ⸮⸮NbH

    do you know what is the problem?

    Reply
      • Sorry for bothering @Sara santos
        I’m implementing ESP-NOW for my project.
        I want to send 4 analog inputs simultaneously to slave board, does ESP32 master is capable of transmitting this rate of data? According to the 250Mb/s limitation.

        Best wishes

        Reply
  16. The code to get MAC address does not work. This works:-
    #include <WiFi.h>

    void setup(){
    Serial.begin(115200);
    Serial.print(“\Default ESP32 MAC Address: “);
    Serial.println(Network.macAddress());
    }

    void loop(){
    }

    me_no_dev expressif / arduino esp32 github comment April 14

    Reply
  17. Hi Sara,

    I think you do great Work

    I have the Arduino Nano ESP32 for witch I tried to get the MAC Address, It didn’t work with your code so after several tries I changed It a bit.

    Now It works and I’m able to get the MAC Address of my Board, below you can find the changes in the Code (Look for Changed).

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

    void readMacAddress(){
    uint8_t baseMac[6];
    esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
    if (ret == ESP_OK) {
    Serial.printf(“%02x:%02x:%02x:%02x:%02x:%02x\n”,
    baseMac[0], baseMac[1], baseMac[2],
    baseMac[3], baseMac[4], baseMac[5]);
    } else {
    Serial.println(“Failed to read MAC address”);
    }
    }

    void setup(){
    Serial.begin(9600);

    WiFi.mode(WIFI_STA);
    //WiFi.STA.begin();
    WiFi.begin();   //I got an error on WiFi.STA.begin(); so I changed that to WiFi.begin();
    delay(1000);    //Changed
    Serial.print(“[DEFAULT] ESP32 Board MAC Address: “);
    delay(1000);    //Changed
    readMacAddress();
    }

    void loop(){

    }

    Best regards,

    Martin

    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.