How do I connect ESP32 to WiFi?

Most “ESP32 won't connect” problems are either credentials (SSID/password), 2.4GHz vs 5GHz, or signal quality. Start with a tiny sketch that prints what's happening.

Minimal WiFi connect sketch (good baseline)

esp32_wifi_connect.ino
cpp
#include <WiFi.h>

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

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

  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false); // helps some boards stay connected
  WiFi.begin(ssid, password);

  Serial.print("Connecting");
  unsigned long start = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - start < 20000) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("Connected. IP: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("Failed to connect (timeout).");
    Serial.print("Status: ");
    Serial.println((int)WiFi.status());
  }
}

void loop() {}

The 5 gotchas that bite beginners

  1. ESP32 is 2.4GHz. If your router has separate names (SSIDs) for 2.4 and 5, pick the 2.4 one.
  2. SSID and password are case-sensitive. Copy/paste carefully.
  3. Special characters in the password can trip you up if you accidentally add spaces or wrong quotes.
  4. Captive portals / “guest” WiFi sometimes blocks IoT devices.
  5. Weak signal looks like “random disconnects” later. Test close to the router first.

Board/IDE sanity checks

  • Start with a basic board profile like ESP32 Dev Module.
  • Use Serial Monitor at 115200 (matches the sketch).
  • If nothing prints, you're on the wrong port or baud rate.
Bottom line

Get one clean connection with the minimal sketch, close to your router, on a 2.4GHz SSID. Once that works, build your project on top of it.

Related: Why won't my ESP32 connect or stay connected? · Fix “connection failed” / weak signal · ESP32 WiFi scan