Why won't my ESP32 connect to WiFi (or it keeps disconnecting)?

If your ESP32 connects sometimes but drops off, don't assume the chip is “bad.” Most of the time it's signal, router settings, or power (especially when motors/LED strips are involved).

Start with the boring checks

  1. 2.4GHz only: make sure you're not trying to join a 5GHz-only SSID.
  2. Test near the router: if it's stable up close but not across the room, it's a signal problem.
  3. Swap the USB cable/power source: brownouts cause “mystery disconnects.”
  4. Disable WiFi sleep: some boards behave better with sleep off.

A simple reconnect pattern

This is not “enterprise grade,” but it's solid for hobby projects.

esp32_wifi_reconnect.ino
cpp
#include <WiFi.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

void connectWifi() {
  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false);
  WiFi.begin(ssid, password);

  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 15000) {
    delay(250);
  }
}

void setup() {
  Serial.begin(115200);
  delay(200);
  connectWifi();
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi lost. Reconnecting...");
    WiFi.disconnect(true);
    delay(250);
    connectWifi();
    if (WiFi.status() == WL_CONNECTED) {
      Serial.print("Back online. IP: ");
      Serial.println(WiFi.localIP());
    } else {
      Serial.println("Reconnect failed (will retry).");
      delay(2000);
    }
  }

  // your normal code here
  delay(250);
}

Router settings that can break IoT

  • Guest network isolation: some guest SSIDs block devices from talking back properly.
  • MAC randomization / access control: if you whitelist devices, make sure the ESP32 is actually allowed.
  • WPA3-only: if your router is set to WPA3-only, try WPA2/WPA3 mixed mode.

When it's power (common in “real projects”)

If your ESP32 disconnects when motors start, relays click, or LEDs brighten: that's usually voltage sag or noise. Power the ESP32 from a stable regulator and share ground properly.

Bottom line

Prove it's stable close to the router on 2.4GHz with WiFi sleep disabled. If disconnects only happen when your hardware “does stuff,” fix power/noise next.

Related: Connect ESP32 to WiFi · Fix “connection failed” / weak signal · External power safety