Esp32

Get ESP32 MAC Address and Change It Using Arduino IDE

ESP32 is a prototyping microcontroller-based board that is a perfect choice for IoT projects. ESP32 features Wi-Fi driver and dual Bluetooth for wireless. Due to its wireless capabilities ESP32 is widely used in different networks to interface multiple things together and for that MAC addresses plays a major role. Let’s discuss ESP32 MAC addresses.

What is MAC Address

MAC or Media Access Control Address is a unique identifier ID assigned to each device inside a network. By default, MAC addresses are defined by the manufacturer; they can be found over the network interface controller (NIC) card.

A picture containing text, electronics Description automatically generated

MAC addresses consist of six hexadecimal digits groups. For example, the MAC address of the ESP32 board we are currently using is: 7C:9E:BD:4B:3B:20.

This is the default MAC address defined by the manufacturer to our ESP32 board, but we can set any MAC address. However, an important thing to note is that the custom MAC address resets every time we reset the ESP32 board, and it will set to its default MAC address. So, we have to include a custom MAC address every time we upload a code.

How to Get ESP32 MAC Address

Connect the ESP32 board with the PC and select the COM port. Upload the code in ESP32 board using Arduino IDE.

Code

To get the MAC address of the ESP32 board we need to run the code below:

#include <WiFi.h>

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

Output

Once the sketch is uploaded press the EN/Boot button on ESP32 board to display the default MAC address:

How to Set a Custom MAC Address for ESP32 Using Arduino IDE

In some network applications we need a custom MAC address. Below code can be used to set any MAC address. However, the MAC address set by us will not overwrite the default MAC address.

Code

The code given will change the default MAC address with a custom defined MAC address.

#include <WiFi.h>

#include <esp_wifi.h>

uint8_t CustomMACaddress[] = {0xCC,0xBE,0xD9,0x01,0x00,0x12};/*Custom MAC address defined*/

void setup(){
  Serial.begin(115200);
  Serial.println();
  WiFi.mode(WIFI_STA);  /*ESP32 in Station Mode*/
  Serial.print("Default ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());  /*Prints default MAC address*/
  esp_wifi_set_mac(WIFI_IF_STA, &CustomMACaddress[0]);
  Serial.print("Custom MAC Address for ESP32:  ");
  Serial.println(WiFi.macAddress());  /*Prints Custom MAC address*/
}
void loop(){
}

The below line represents the new MAC address.

uint8_t newMACAddress[] = {0xCC,0xBE,0xD9,0x01,0x00,0x12};

Output

Following output appears which shows the default MAC address set by the manufacturer and the custom MAC address set by us inside the code:

Why is a MAC Address Important

  • MAC address helps to find a specific device inside a network using its unique MAC ID.
  • MAC address prevents unwanted network access.
  • As the MAC address is unique it can track the device.

Conclusion

Here in this article, we changed the MAC address of ESP32 to a new random generated MAC address. However, one thing to keep in mind is that this custom set MAC address is temporary and will reset when the new code is uploaded, or the board is reset.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.