Skip to main content
ESPBoards

Can ESP32 Connect to 5GHz Wi-Fi? Exploring Wi-Fi Connectivity Options with ESP32

Learn if ESP32 can connect to 5GHz WiFi in this comprehensive guide. Find out the limitations and benefits of using this feature in your IoT project


The ESP32 is a popular choice among developers when building IoT projects due to its impressive features, including a built-in Wi-Fi module. However, a frequently asked question is whether the ESP32 can connect to a 5GHz Wi-Fi network. In this blog post, we will examine this topic in detail and provide you with concrete answers. But first, let's take a quick look at the ESP32 Wi-Fi module and its capabilities.

Understanding the Differences between 5GHz and 2.4GHz WiFi #

Wi-Fi 5 GHz is a wireless communication protocol that operates on the 5 GHz frequency band. This band offers faster data transfer speeds and less interference than the more common 2.4 GHz band. The 5 GHz band offers more available channels and less congestion, resulting in better performance and reliability, especially in crowded or congested environments. However, the range may be limited compared to 2.4GHz signals because 5GHz signals typically penetrate solid objects like walls and floors. Knowing the difference between 5 GHz and 2.4 GHz Wi-Fi is important when choosing the right wireless device for your needs.

Understanding the WiFi Capabilities and Limitations of the ESP32 Microcontroller #

5GHz WiFi has not only wider channels but also more channels, which allows faster data transfer rates. In general, you can expect higher maximum speeds from a 5GHz network. Also, as most of the devices nowadays are using 2.4 GHz WiFi, you can expect lower congestion on a 5 GHz network.

However, they have some limitations. The ESP32s 5GHz Wi-Fi support is not as robust as its 2.4GHz counterpart. This is because the 5GHz frequency band has a low bandwidth and is easily blocked by obstacles such as walls and furniture. This means that the ESP32 can technically connect to a 5GHz WiFi network but it's not always the best choice for projects that require long-distance or high-capacity connections.

Not all ESP32 boards are created equal when it comes to WiFi support. Wi-Fi performance may be better or worse depending on certain tablet hardware specifications. Therefore it is important to do your research and choose an ESP32 development board that suits your specific needs.

In summary, even though the 5 GHz sounds promising and good, there are some limitations and considerations to keep in mind when the ESP32 can connect to a 5 GHz Wi-Fi network.

Depending on the needs of your project you might be better off sticking with the 2.4GHz connection or choosing a different microcontroller entirely.

Does ESP32 support 5GHz WiFi? #

Now we know the advantages and disadvantages of 5 GHz WiFi and despite the disadvantages, it can still be a better fit for your next project. So the question now is - can ESP32 connect to 5 GHz WiFi? And the answer is not very straightforward... If you happen to have an older ESP32 like the original ESP32, ESP32S2 or, unfortunately, you will not be able to use the benefits of 5 GHz WiFi.

Even the newer versions of ESP32 like ESP32C3 or ESP32C6, which was released in 2021 April and provides WiFi 6 capabilities do not support the 5GHz WiFi.

But no, it's not all lost for ESP32! On June 2022, Espressif announced the release of a follow-up to the previously mentioned ESP32C6, The ESP32C5, which supports dual-band 2.4 GHz and yes, 5 GHz WiFi! The ESP32-C5 comes with dual-band WiFi 6 (802.11ax) and still has the backward compatible 802.11b/g/n.

As you can see, the 5GHz WiFi for esp boars were introduced only in the latest versions, therefore as you could imagine, the esp8266 does not support 5ghz.

Connecting ESP32 to 5GHz WiFi: Step-by-Step Guide, Code Examples and Troubleshooting Tips #

If you are now wondering how to connect the ESP32 to a 5GHz WiFi network then you are in luck. Here's a step-by-step guide to get you started.

Before diving into the code, make sure that the ESP32 microcontroller you have supports 5 GHz WiFi 6 with the following steps:

  • Check the ESP32s WiFi Capability: Before attempting to connect to a 5GHz network it is important to confirm that the ESP32 supports this frequency band.

  • Firmware Update: Make sure you have the latest firmware installed on your ESP32 as this may affect compatibility with 5GHz WiFi networks.

  • Configuring the ESP32: The ESP32 must be configured to connect to a 5 GHz WiFi network. This can be done using the code examples below.

  • Check the connection: After configuring the ESP32 check the connection to make sure it is working properly.

To get you started, below we will go step by step to connect our ESP32 C5 to 5 GHz WiFi. We assume that you have your development environment ready to work with ESP32 microcontrollers, specifically ESP32-C5 and have the ESP-IDF libraries installed, that provide Wi-Fi API** and ESP Log API for debugging purposes.

  1. Include the required headers
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.h"
  1. Define your WiFi SSID and password, that the ESP will connect to.
#define EXAMPLE_ESP_WIFI_SSID      "YOUR_SSID"
#define EXAMPLE_ESP_WIFI_PASS "YOUR_PASSWORD"
#define EXAMPLE_ESP_WIFI_CHANNEL 36
#define EXAMPLE_MAX_STA_CONN 4

Make sure to change "YOUR_SSID" and "YOUR_PASSWORD" with your WiFi network credentials. If you want to use 5 GHz benefits, make sure that the ESP32 is connecting to 5 GHz WiFi and set the EXAMPLE_ESP_WIFI_CHANNEL to the channel used with 5 GHz WiFi.

In most countries, the 5GHz WiFi frequency range is divided into multiple non-overlapping channels, which are used for wireless networking. These are some common 5GHz WiFi channel numbers: Channel 36 (5180 MHz), Channel 40 (5200 MHz), Channel 44 (5220 MHz), Channel 48 (5240 MHz), Channel 52 (5260 MHz), Channel 56 (5280 MHz), ..., Channel 161 (5805 MHz), Channel 165 (5825 MHz).

These are just some of the channel numbers available for 5 GHz and there are usually more. Please note that channel availability may vary depending on your location regulations and the WiFi standard being used.

  1. define the TAG for WiFi messages logging
static const char *TAG = "wifi softAP";
  1. Define the callback function for handling the WiFi events
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}

This function will be called whenever a WiFi event occurs. For example, connecting to the network or disconnecting from it, obtaining an IP address, etc. The function logs events using ESP Log API and takes appropriate action when an event occurs.

  1. Next, we define the function used to initialize the WiFi in Soft AP mode
void wifi_init_softap(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));

wifi_config_t wifi_config = {
.ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
.password = EXAMPLE_ESP_WIFI_PASS,
.max_connection = EXAMPLE_MAX_STA_CONN,
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
.authmode = WIFI_AUTH_WPA3_PSK,
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
.authmode = WIFI_AUTH_WPA2_PSK,
#endif
.pmf_cfg = {
.required = true,
},
},
};
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());

ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
}

In this function, we first initialize the network interface using the esp_netif_init() function and create a default event loop, by using the esp_event_loop_create_default() function. We then create a default WiFi AP using the esp_netif_create_default_wifi_ap() function.

Next, we initialize the WiFi configuration with default settings using the WIFI_INIT_CONFIG_DEFAULT() function and initialize the WiFi module using the esp_wifi_init() function with the previously set configuration.

We then register a WiFi event handler using the esp_event_handler_instance_register() function to handle any WiFi events that may occur and set it to use the wifi_event_handler() function, that we defined in the previous step.

The wifi_config_t structure is used to configure the WiFi access point with the desired SSID, password, channel, maximum number of connections, and authentication mode. We set the WiFi here to the 5 GHz channel.

Finally, we set the WiFi mode to access point mode using the esp_wifi_set_mode() function and set the access point configuration using the esp_wifi_set_config() function with the WIFI_IF_AP parameter. And lastly, the access point is started using the esp_wifi_start() function.

If the connection to the WiFi network is successful, you should see information about the SSID, password, and channel in the Serial Monitor.

  1. We will next define the main application function app_main()
void app_main(void)
{

}
  1. Inside the app_main() function, initialize the NVS Flash, needed for WiFi and call the wifi_init_softap() function, that we defined earlier.

With this code example, you should be able to connect to the 5 GHz frequency WiFi. If you are still wondering how we configured our ESP32-C5 to connect to 5 GHz WiFi - we specified the 5 GHz frequency band implicitly through the Wi-Fi channel selection. When connecting to a Wi-Fi network, the ESP32-C5 module will automatically scan for available channels and select the one that matches the Wi-Fi network configuration.

When the ESP32-C5 module connects to the Wi-Fi network, it will scan for channels that support these settings, and select the appropriate one based on the available channels and signal strength. This should result in a connection to a 5 GHz Wi-Fi network, assuming that one is available and the network configuration is correct.

Full example #

If you trust yourself without the code explanations and prefer to deep dive straight into the full code, here is the full code of the example above:

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.h"

#define EXAMPLE_ESP_WIFI_SSID YOUR_SSID
#define EXAMPLE_ESP_WIFI_PASS YOUR_PASSWORD
#define EXAMPLE_ESP_WIFI_CHANNEL 36
#define EXAMPLE_MAX_STA_CONN 4

static const char *TAG = "wifi softAP";

static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}

void wifi_init_softap(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));

wifi_config_t wifi_config = {
.ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
.password = EXAMPLE_ESP_WIFI_PASS,
.max_connection = EXAMPLE_MAX_STA_CONN,
#ifdef CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT
.authmode = WIFI_AUTH_WPA3_PSK,
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
#else /* CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT */
.authmode = WIFI_AUTH_WPA2_PSK,
#endif
.pmf_cfg = {
.required = true,
},
},
};
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());

ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
}

void app_main(void)
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
}

In case you do need some explanations, do not be afraid to jump back to the previous section, where you will find step-by-step instructions.

Troubleshooting the ESP32 5 GHz WiFi connection #

Here are some troubleshooting tips if you encounter any problems during the connection process.

  • Double-check the list we provided to make sure your ESP32 is capable of connecting to 5 GHz WiFi
  • Double-check that your configuration settings are correct.
  • Move the ESP32 closer to a router or access point to ensure a strong signal.
  • Make sure your 5 GHz WiFi network is not hidden or placed on a non-standard channel.

If you still have troubles, make sure to check the relevant resources:

Can you use Arduino Core to connect to 5 GHz WiFi #

Unfortunately, as always, the Arduino Core library is a bit lagging behind and currently does not support the ESP32-C5 board, meaning you cannot use the 5 GHz benefits with your ES32 board by using the Arduino Core. We hope Espressif releases the support for ESP32-C5 microcontroller on Arduino core, but for now, you just have to wait or use ESP-IDF, if you really need your project to support 5 GHz WiFi.

If you decide that your project must support 5 GHz WiFi and therefore decide on using the ESP-IDF, you can check our guide on getting started with ESP-IDF, using Visual Studio Code.

If you are familiar with coding with Arduino, you are already familiar with the C programming language, so the switch to ESP-IDF should not be very difficult. The most important thing to remember, if you decide to switch, is that in the ESP-IDF framework, there is no setup function like in the Arduino IDE.

Instead, the app_main function is the entry point for the program and is called automatically by the system when the device is started. All the necessary setup and configuration for the application should be done in the app_main function or called by it, as shown in the example code.

This is because the ESP32 is an embedded system, and the app_main function is the equivalent of the main function in a regular C program. It is the first function that is executed when the device is powered on or reset, and it is responsible for initializing the system, setting up the peripherals, and starting the main program loop.

Summary: Connecting ESP32 to 5GHz WiFi and Considerations #

After exploring the topic of connecting the ESP32 to 5GHz WiFi we found that the ESP32 supports 5GHz WiFi and there are several factors to consider before deciding to use it.

We have discussed the differences between 5GHz and 2.4GHz WiFi in ESP32s Wi-Fi features and provided a step-by-step guide to connect the ESP32 to a 5GHz network. We also explored the pros and cons of using 5GHz WiFi with the ESP32.

Finally, it is important to consider the specific needs of the project before deciding to use 5GHz Wi-Fi with the ESP32. If range and power consumption are not important factors and you need high data rates 5GHz Wi-Fi may be a good choice. However, if range and power consumption are important it may be best to stick to the 2.4GHz band. The best option ultimately depends on the specific needs of your project.

Additional resources #

In addition to the resources mentioned in the article, there are some additional resources for further reading on the topic of connecting the ESP32 to 5GHz WiFi:

Exploring this resource can gain a deeper understanding of the ESP32s Wi-Fi capabilities and how to connect to a 5GHz network as well as learn advanced techniques for working with ESP32.