ESP32 won't connect to Wi-Fi: A full breakdown of errors

The ESP32 microcontroller's failure to connect to a wireless network is one of the most common problems in IoT device development. Users often encounter an endless reconnection loop or an error. WIFI_REASON_AUTH_EXPIRE in the logs, which is puzzling, especially if the code had previously worked reliably. There could be several underlying causes: from a simple typo in the password to problems with the board's power supply or incompatibility with the router's security standards.

Unlike simpler modules, the ESP32 chip has a powerful processor and Wi-Fi module, which consume significant current during peak loads. This is why unstable power supply often becomes the root cause of failures, masquerading as software errors. Furthermore, the Arduino IDE may default to using outdated libraries or incorrect compilation settings, resulting in failure to authenticate in modern WPA3 or even WPA2-PSK networks.

In this article, we'll cover all diagnostic steps in detail, from checking the physical connection to fine-tuning the code. You'll learn how to properly handle connection events, why a static IP address is necessary, and why CPU frequency affects connection stability. We'll also cover specific issues related to SSID name length and hidden characters in the configuration.

Power and physical connection issues

The first thing to check if your ESP32 isn't connecting to Wi-Fi is the quality of the power supply. When sending a data packet over the radio, current consumption can spike to 500 mA or more. If you're using a computer's USB port or a cheap cable longer than a meter, the voltage on the board may drop below the critical 3.0 V level, causing the chip to reboot or the Wi-Fi module to fail. Use a high-quality USB cable with thick wires and an external power supply with a minimum current rating of 1 A.

It's also critical to consider the impact of interference. The ESP32 is sensitive to interference, especially near powerful electromagnetic sources or long wires leading to the sensors. Try disconnecting all peripheral devices (OLED displays, relays, motors) and leaving only the bare board connected to the PC to prevent external electronics from interfering with the radio module.

⚠️ Note: Some Chinese clones of the ESP32 board (for example, those based on CP2102 chips) require driver installation. If the device isn't detected in Device Manager or the COM port disappears immediately after attempting to connect, the issue may lie with the USB-UART bridge driver, not the Wi-Fi itself.

To test power supply stability, you can connect a 10-22 ΞΌF capacitor between the 3.3V and GND pins directly on the board. This will smooth out voltage pulsations during peak consumption. If this procedure increases the number of successful connections, the problem lies in insufficient power supply capacity or filtering.

πŸ“Š How do you power your ESP32?
Via USB PC
Via PowerBank
Via an external 5V unit
Via Li-Ion battery

Errors in the sketch and Arduino IDE settings

The most common software error is using an outdated connection method or ignoring the process state. A common example WiFi.begin(ssid, password) is blocking only in some implementations, but most often it simply initiates the process. If immediately after this line in the loop loop There is no need to check the connection status with a delay; the microcontroller may try to send data even before the link is established, which leads to a timeout.

Be sure to use a connection wait loop with a return status check. The code should look something like this: while WiFi.status() is not equal WL_CONNECTED, we wait and output the point to the port. It's also important to add a delay delay(500) inside the wait loop to give the module time to process the handshake protocols.


WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");

Another important aspect is selecting the board and kernel version in the Arduino IDE menu. For the ESP32, you need to install the latest kernel through the boards manager by entering the repository URL. Older kernel versions may not work correctly with new routers using the 802.11ax (Wi-Fi 6) standard or mixed mode. Make sure your specific model is selected in the boards manager (e.g., DOIT ESP32 DEVKIT V1), since different boards have different pinouts and flash memory settings.

β˜‘οΈ Checking the sketch

Completed: 0 / 4

Problems with the 5 GHz frequency and router channel

Classic ESP32 modules (first generation) only support the 2.4 GHz frequency range. If your router broadcasts a network with the same name (SSID) in both bands (Smart Connect feature), the microcontroller may attempt to connect to a 5 GHz access point even though it's physically unable to operate on that frequency. As a result, you'll see endless connection attempts or an error. WIFI_REASON_NO_AP_FOUND.

The solution is to separate the networks on your router: create a guest network or a separate SSID for the 2.4 GHz band only. Name it, for example, "Home_2.4G" and connect the ESP32 to it. It's also worth paying attention to the channel number: the ESP32 works best on channels 1 through 11. If the router automatically selects channel 12 or 13 (which is allowed in Europe but not in the US), the module may simply not see the network.

Parameter Recommended value Possible problem
Frequency range 2.4 GHz (802.11 b/g/n) ESP32 doesn't see 5 GHz networks
Wi-Fi channel 1, 6 or 11 Unstable signal at the edges of the range
Channel width 20 MHz Interference at 40 MHz width
Security mode WPA2-PSK (AES) Incompatibility with WEP or WPA3
Why is 40 MHz channel width worse?

Using a 40 MHz channel in the congested airwaves of an apartment building often leads to collisions. The ESP32, with its single antenna, copes worse with interference on wide channels than on narrow 20 MHz, where the signal is cleaner and has a longer range.

Static IP vs. DHCP: Network Setup

The DHCP protocol, which automatically assigns IP addresses to devices, sometimes performs poorly in networks with a large number of clients or on specific routers (such as Keenetic or Mikrotik routers with complex settings). If the ESP32 is unable to obtain an address for a long time, it aborts the connection attempt. In such cases, manually assigning a static IP address can help.

To use a static IP, you need to create an object IPAddress and call the method config() before launch begin()This ensures that the device receives the required address immediately, avoiding a lengthy request to the DHCP server. It's important that the selected IP address be in the same subnet as the router and not conflict with other devices.


IPAddress local_IP(192, 168, 1, 150);

IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 255, 0);

IPAddress primaryDNS(8, 8, 8, 8);

if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) {

Serial.println("STA Failed to configure");

}

Using a static address also speeds up device startup, as it eliminates the router negotiation step. This is especially useful for devices that need to send data quickly after being turned on, such as leak detectors or security systems. However, keep in mind that if you change your router, you'll have to adjust the settings manually.

Features of working with the WiFi.h library

Library WiFi.h for ESP32 has its own peculiarities compared to Arduino WiFi. For example, it uses the default mode WIFI_MODE_STA (client), but if you were previously using access point (AP) mode, you need to explicitly switch. You can reset Wi-Fi settings with the command WiFi.disconnect(true), where the parameter true This also means clearing the NVS (Non-Volatile Storage) memory, where data about past networks is stored.

A common mistake is ignoring events. The library supports an event system that can be used to monitor IP changes, connection losses, or signal strength changes. Adding an event handler allows for more robust systems with auto-reconnect, which is enabled by default but sometimes requires manual activation via WiFi.setAutoReconnect(true).

It's also worth mentioning the issue with long SSID names. Older versions of the ESP32 firmware had a bug where network names longer than 32 characters were truncated or caused a buffer overflow. Although this has been fixed in newer kernels, check the length of your network name. If the name is too long, try shortening it in your router settings.

⚠️ Attention: When using the function WiFi.disconnect(true) All saved networks are deleted. If your device needs to remember multiple networks, use this method with caution or implement password storage logic in EEPROM.

Diagnostics using error codes and logs

For in-depth diagnostics, it's necessary to analyze the output in the Serial Monitor. The ESP32 returns specific error codes when the connection is lost. For example, the code 2 (WIFI_REASON_AUTH_EXPIRE) often indicates an incorrect password or incompatible encryption method. Code 201 (WIFI_REASON_CONNECTION_FAIL) may mean that the router simply rejected the connection due to client limit or MAC filtering.

Enable verbose logging at the kernel level by adding the following line to the beginning of the sketch: esp_log_level_set("*", ESP_LOG_DEBUG);This will allow you to see detailed messages from the Wi-Fi driver, including the scanning, authentication, and IP acquisition stages. This data often contains the exact reason for the failure, which is not visible in the standard output.

If you see the message "Brownout detector was triggered" in the logs, this clearly indicates a power supply issue, as discussed in the first section. No software hacks will help hereβ€”you'll need to replace the cable, power supply, or reduce the load on the GPIO pins.

Why does ESP32 connect but immediately disconnect?

This is most often caused by the watchdog timer (WDT). If the connection cycle has too much latency or heavy calculations without resetting the timer, the system reboots the module. IP address conflicts on the network can also be the cause.

How to reset Wi-Fi settings on ESP32?

Use the function WiFi.disconnect(true) in setup. The true parameter is importantβ€”it erases data from the non-volatile memory. After this, the device will behave as if it were new and will not attempt to connect to the old network.

Can antivirus block ESP32?

Not directly, but Windows Firewall or antivirus software may block incoming connections from the ESP32 if you're using it as a server. For debugging, try temporarily disabling the firewall or adding a rule for your IDE.

Does the Arduino IDE version affect the connection?

Yes, older versions (1.8.x) may have bugs in the board manager. It is recommended to use Arduino IDE 2.x or PlatformIO, as they work better with modern versions of the ESP32 core and debugging tools.