ESP32 MAC Address: How To Read & Change It Using the Arduino IDE

The ESP32 microcontroller is widely used in IoT and embedded projects thanks to its powerful features and built-in Wi-Fi capabilities. Every ESP32 device has a unique MAC address that identifies it on the network. But did you know that you can read and even change the MAC address of your ESP32?

In this guide, you’ll learn how to read the ESP32’s MAC address and change it to a custom MAC address using the Arduino IDE.

Table of Contents

    What is a MAC Address?

    A MAC (Media Access Control) Address is a unique hardware identifier assigned to every network interface card (NIC). It consists of 6 pairs of hexadecimal numbers and is used to clearly identify devices on a network. Here’s an example of what a MAC address looks like:

    A0:A3:B3:2C:77:CC

    How Each MAC Address is Derived

    Technically, the ESP32 has four MAC addresses, one for Wi-Fi station mode, one for Wi-Fi AP mode, one for Bluetooth, and one for Ethernet. The last three, however, are derived from the Wi-Fi station mode address.

    For example:

    InterfaceExampleDerivation Rule
    Wi-Fi StationA0:A3:B3:2C:77:CCBase Address
    Wi-Fi APA0:A3:B3:2C:77:CDBase Address + 1
    BluetoothA0:A3:B3:2C:77:CEBase Address + 2
    EthernetA0:A3:B3:2C:77:CFBase Address + 3

    As you can see, the last three addresses are derived from the first one by increasing the previous address by one. However, only the last three bytes of a MAC address are allowed to change as the first three bytes are an identifier for the manufacturer.

    If, for some reason, you’ve got a Base Address similar to A0:A3:B3:FF:FF:FF, and you increment it by one, the last three bytes will overflow, resulting in the following MAC address:

    A0:A3:B3:00:00:01

    Why Would You Need to Read or Change the ESP32 MAC Address?

    Reading the MAC address of an ESP32 is useful when establishing a connection between devices. For example, the wireless protocol ESP-NOW uses MAC addresses for communication between ESP microcontrollers.

    Changing the MAC address can also have various use cases. For example, it can help when simulating different devices during testing. Also, it is used for bypassing MAC-based filtering and avoiding device blocking in certain networks.

    However, it’s important to use this capability ethically, as changing the MAC address of a device can be used for performing Man-In-The-Middle (MITM) Attacks and other malicious attacks.

    Important Note:
    Changing the ESP32’s MAC address is only temporary, the custom MAC address will reset after restarting the device without the code for changing the address.

    How to Read the ESP32 MAC Addresses (Code Example)

    You can use the following sketch to find out the base MAC address (Wi-Fi Station Mode interface) of your ESP32.
    To find out the derived addresses, simply add 1 (for Wi-Fi AP), 2 (for Bluetooth), and 3 (for Ethernet) to the last three bytes of the base MAC.

    #include <WiFi.h>
    #include <esp_wifi.h>
    
    void setup(){
      Serial.begin(115200);
    
      // initalize Wi-Fi to be able to read MAC address
      WiFi.mode(WIFI_STA);
      WiFi.STA.begin();
    
      uint8_t mac[6];
    
      // read base address
      esp_wifi_get_mac(WIFI_IF_STA, mac);
      Serial.printf("Base MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
                    mac[0], mac[1], mac[2],
                    mac[3], mac[4], mac[5]);
    }
     
    void loop(){}

    After uploading the code, watch your serial monitor for the output.

    ESP32 Get Mac Address sketch output in serial monitor

    How to Change the MAC Address of Your ESP32

    To change the MAC address of your ESP32, you’ll need to use the esp_wifi_set_mac() function from the esp_wifi library.

    #include <WiFi.h>
    #include <esp_wifi.h>
    
    uint8_t newMAC[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};
    
    void setup(){
      Serial.begin(115200);
      Serial.println("");
    
      uint8_t helperMAC[6];
    
      // initialize Wi-Fi
      WiFi.mode(WIFI_STA);
      WiFi.begin();
      
      // read original MAC address
      esp_wifi_get_mac(WIFI_IF_STA, helperMAC);
      Serial.printf("Original MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
                    helperMAC[0], helperMAC[1], helperMAC[2],
                    helperMAC[3], helperMAC[4], helperMAC[5]);
      
      // set new MAC address
      esp_err_t err = esp_wifi_set_mac(WIFI_IF_STA, newMAC);
      if (err == ESP_OK) {
        Serial.println("Success changing Mac Address");
      }
      else {
        Serial.println(err);
      }
      
      // read custom MAC address
      esp_wifi_get_mac(WIFI_IF_STA, helperMAC);
      Serial.printf("Custom MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
                    helperMAC[0], helperMAC[1], helperMAC[2],
                    helperMAC[3], helperMAC[4], helperMAC[5]);
    }
    
    void loop() {}

    All the derived MAC addresses for the WiFi AP, Bluetooth, and Ethernet interfaces are updated automatically.

    Which MAC addresses are not allowed?

    You can’t set the MAC address to anything you want; certain types of MAC addresses are not allowed. The ESP32 will only accept unicast addresses.

    You can determine whether a MAC address is a unicast address, by looking at the least significant bit (LSB) of the first byte of the address. If the LSB of the first byte is 1, the MAC address is a unicast address. Hence, it is invalid. Here are two examples:

    • Unicast (valid): 02:AA:BB:CC:DD:EE -> first byte: 000000010
    • Multicast (invalid): 03:AA:BB:CC:DD:EE -> first byte: 00000011

    Also not allowed are addresses reserved by specific protocols:

    • 00:00:5E:xx:xx:xx -> VRRP protocol
    • 01:80:C2:xx:xx:xx -> IEEE 802

    In case the address you want to set is invalid, the ESP32 simply won’t change it.

    Conclusion

    Reading and temporarily changing the ESP32’s MAC address is a powerful feature for testing custom network applications. With just a few lines of code, you can easily manipulate the ESP32’s MAC address in the Arduino IDE.

    However, remember that this method is only temporary and should be used responsibly.


    Do you have any questions or ideas for using custom MAC addresses in your projects? Let me know in the comments!

    Thanks for reading!

    Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.

    Share this article

    Leave a Reply

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