Wifi Guide

How to Connect Esp32 With Wifi? Wireless Board Setup Tutorial Steps

Introduction The ESP32 is one of the most popular microcontrollers for IoT projects, and much of its power comes from built-in WiFi and Bluetooth. Whether you are building a smart home sensor, a wireless weather station, or a remote-controlled device, the very

HowToFixWiFi

Introduction

The ESP32 is one of the most popular microcontrollers for IoT projects, and much of its power comes from built-in WiFi and Bluetooth. Whether you are building a smart home sensor, a wireless weather station, or a remote-controlled device, the very first step is usually the same: you need to know How to Connect Esp32 With Wifi? Without a wireless connection, the board cannot send sensor data to the cloud, receive commands from a smartphone, or host a local web server.

This guide walks you through everything you need to get an ESP32 online. We cover the hardware and software you will need, the three main WiFi modes the ESP32 supports, step-by-step code examples for each mode, and practical tips for keeping your connection stable. By the end, you will be able to connect your ESP32 to a home router, create its own access point, or run both at the same time.

If you are new to the ESP32, do not worry. The instructions are written for beginners, but they also include enough detail to be useful for experienced makers who want a quick reference.

Table of Contents

What You Need Before You Start

Before you write any code, gather the right tools. Trying to connect an ESP32 to WiFi without the correct board, cable, or driver is a common source of frustration.

Hardware Requirements

  • ESP32 development board: Any common ESP32 dev kit will work, such as the DOIT ESP32 DevKit V1, NodeMCU-32S, or ESP-WROOM-32 module. Make sure it has a USB port for programming and power.
  • Micro-USB or USB-C cable: Use a data cable, not a charge-only cable. A bad cable can cause upload failures or intermittent disconnects.
  • Computer: Windows, macOS, or Linux with a working USB port.
  • 2.4 GHz WiFi network: The standard ESP32 supports 2.4 GHz WiFi only. It cannot connect to 5 GHz routers, so make sure your network is broadcasting on 2.4 GHz or is a mixed-mode network.

Software Requirements

  • Arduino IDE: Download the latest version from the official Arduino website. It is the easiest environment for beginners learning How to Connect Esp32 With Wifi?
  • ESP32 board package: You must add the Espressif board manager URL to Arduino IDE so it recognizes ESP32 boards.
  • USB-to-UART drivers: Depending on your board, you may need CP210x or CH340 drivers so your computer can see the ESP32 serial port.

Network Information

Have your WiFi network name (SSID) and password ready. If your router hides its SSID or uses MAC address filtering, you will need to account for that later in the setup process.

Understanding ESP32 WiFi Modes

The ESP32 WiFi library supports three operating modes. Choosing the right one depends on what you want your project to do.

Station Mode (STA)

In Station mode, the ESP32 acts like a laptop or smartphone. It connects to an existing wireless router and receives an IP address from that network. This is the most common mode for IoT devices that need to send data to the internet or communicate with other devices on the same network.

Access Point Mode (AP)

In Access Point mode, the ESP32 creates its own WiFi network. Other devices, such as a phone or laptop, can connect directly to the ESP32. This is useful for initial configuration, direct sensor readouts, or situations where no router is available.

Station + Access Point Mode (STA+AP)

In this dual mode, the ESP32 connects to an existing router while simultaneously broadcasting its own network. This allows the device to stay online and also accept direct local connections, though it uses more memory and processing power.

Installing Arduino IDE and ESP32 Board Support

Follow these steps to prepare your computer for programming the ESP32.

  1. Download and install Arduino IDE from arduino.cc.
  2. Open Arduino IDE and go to File > Preferences.
  3. In the Additional Boards Manager URLs field, paste the Espressif board manager URL. If you are unsure of the exact URL, check the official Espressif documentation.
  4. Go to Tools > Board > Boards Manager, search for esp32, and install the package by Espressif Systems.
  5. Connect your ESP32 board via USB.
  6. Select your board model under Tools > Board.
  7. Select the correct COM port under Tools > Port.

If the board does not appear in the port menu, install the appropriate USB-to-UART driver for your board’s chip (CP2102, CH340, etc.), then restart Arduino IDE.

How to Connect Esp32 With Wifi? Station Mode Step-by-Step

Station mode is the best place to start when learning How to Connect Esp32 With Wifi? because it lets your board join your home or office network.

  1. Open Arduino IDE and create a new sketch.
  2. Include the WiFi library at the top of your code.
  3. Define variables for your network SSID and password.
  4. In the setup() function, start serial communication so you can monitor progress.
  5. Call WiFi.begin(ssid, password) to initiate the connection.
  6. Use a loop to wait until WiFi.status() returns WL_CONNECTED.
  7. Print the local IP address to the Serial Monitor so you know where to find the board on the network.
  8. Upload the sketch and open the Serial Monitor at 115200 baud.
#include <WiFi.h>

const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_PASSWORD";

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

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Your main code goes here
}

When the sketch runs, you should see a series of dots in the Serial Monitor followed by the confirmation message and an IP address such as 192.168.1.15. That IP address is how other devices on your network can reach the ESP32.

How to Connect Esp32 With Wifi? Access Point Mode Step-by-Step

Sometimes you do not have a router nearby, or you want devices to connect directly to the ESP32. In that case, Access Point mode is the answer.

  1. Include the WiFi library.
  2. Choose a name and optional password for the ESP32’s own network.
  3. Call WiFi.softAP(ssid, password) to start the access point.
  4. Retrieve the ESP32’s AP IP address with WiFi.softAPIP().
  5. Upload the code and connect a phone or laptop to the new network.
#include <WiFi.h>

const char* apSsid = "ESP32-Access-Point";
const char* apPassword = "12345678";

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

  Serial.println("Configuring Access Point...");

  WiFi.softAP(apSsid, apPassword);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}

void loop() {
  // Devices can connect to the ESP32 directly
}

If you omit the password argument, the ESP32 creates an open network. That is convenient for testing, but not recommended for production because anyone within range can connect.

How to Connect Esp32 With Wifi? Dual Mode (STA+AP)

Dual mode lets the ESP32 connect to your router while also acting as an access point. This is useful when you want the device to remain online and still accept local configuration from a phone.

  1. Set the WiFi mode to WIFI_AP_STA using WiFi.mode().
  2. Call WiFi.begin() to connect to your router.
  3. Call WiFi.softAP() to start the local access point.
  4. Monitor both connections in your code.
#include <WiFi.h>

const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_PASSWORD";
const char* apSsid = "ESP32-Dual";
const char* apPassword = "12345678";

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

  WiFi.mode(WIFI_AP_STA);

  WiFi.begin(ssid, password);
  WiFi.softAP(apSsid, apPassword);

  Serial.print("Station IP: ");
  Serial.println(WiFi.localIP());
  Serial.print("AP IP: ");
  Serial.println(WiFi.softAPIP());
}

void loop() {
  // Handle both connections
}

Keep in mind that running both modes at once increases power consumption and can reduce available memory for your application.

Using WiFi.begin() and WiFi.config()

The WiFi.begin() function starts the connection process using DHCP by default. That means your router assigns an IP address automatically. For many projects, that is perfectly fine.

However, if you are building a web server or a device that other hardware must reach reliably, a static IP address is better. Use WiFi.config() before calling WiFi.begin() to set a fixed address.

#include <WiFi.h>

const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_PASSWORD";

IPAddress localIP(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);

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

  if (!WiFi.config(localIP, gateway, subnet, dns)) {
    Serial.println("STA Failed to configure");
  }

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected with static IP");
  Serial.println(WiFi.localIP());
}

void loop() {
}

Choose addresses that fit your router’s subnet. If you pick an address already in use by another device, you will cause an IP conflict.

Handling WiFi Connection Events and Automatic Reconnection

WiFi networks are not perfect. Routers reboot, signals fluctuate, and the ESP32 can lose its connection. A robust sketch should detect disconnection and attempt to reconnect automatically.

The simplest approach is to check WiFi.status() inside loop(). If the status is not WL_CONNECTED, call WiFi.reconnect() or WiFi.begin() again.

#include <WiFi.h>

const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_PASSWORD";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connection lost. Reconnecting...");
    WiFi.reconnect();
    delay(5000);
  } else {
    // Do your normal work here
    delay(1000);
  }
}

For more advanced projects, you can use WiFi event handlers to run code the moment a connection is established or lost. This keeps your main loop cleaner and makes the system more responsive.

Scanning for Available WiFi Networks

Before connecting, you may want to see which networks are within range. The ESP32 can scan for nearby access points and report their signal strength and encryption type.

Use WiFi.scanNetworks() to perform a synchronous scan. The function returns the number of networks found. You can then loop through the results and print each SSID and RSSI value.

#include <WiFi.h>

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

  Serial.println("Scanning networks...");
  int n = WiFi.scanNetworks();

  Serial.print("Found ");
  Serial.print(n);
  Serial.println(" networks");

  for (int i = 0; i < n; i++) {
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.print(WiFi.SSID(i));
    Serial.print(" (");
    Serial.print(WiFi.RSSI(i));
    Serial.print(" dBm)");
    Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
  }
}

void loop() {
}

Scanning takes a few seconds and blocks other code while it runs. For time-sensitive applications, consider using asynchronous scanning or limiting how often you scan.

Checking Signal Strength and Connection Status

Once connected, you can monitor the quality of the link using WiFi.RSSI(). This returns the signal strength in dBm. Values closer to zero indicate a stronger signal.

As a general rule:

  • -30 dBm to -50 dBm: Excellent
  • -50 dBm to -60 dBm: Good
  • -60 dBm to -70 dBm: Fair
  • -70 dBm to -80 dBm: Weak
  • Below -80 dBm: Very weak, likely to drop

The WiFi.status() function returns constants such as WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_CONNECTED, and WL_CONNECT_FAILED. Checking these values helps you debug why How to Connect Esp32 With Wifi? might be failing in a particular environment.

Troubleshooting Common WiFi Connection Issues

Even with the right code, things can go wrong. Here are the most common problems and how to fix them.

  • Wrong SSID or password: Double-check capitalization and special characters. WiFi credentials are case-sensitive.
  • 5 GHz network: The ESP32 cannot see 5 GHz networks. Make sure your router has a 2.4 GHz band enabled.
  • Weak signal: Move the ESP32 closer to the router or remove obstructions. Thick walls and metal objects weaken WiFi.
  • Power issues: A poor USB cable or underpowered port can cause brownouts. Try a shorter, high-quality cable or an external 5V power supply.
  • Wrong board or port: Verify that you selected the correct ESP32 model and COM port in Arduino IDE.
  • Serial Monitor baud rate: If you see gibberish, make sure the Serial Monitor is set to 115200 baud.
  • Not waiting for connection: Code that immediately tries to use WiFi before WiFi.status() is WL_CONNECTED will fail. Always wait in setup or check status in loop.
  • Router MAC filtering: If your router only allows known devices, add the ESP32’s MAC address to the allowed list.

Tips and Best Practices for ESP32 WiFi Projects

Getting connected is only the first step. Keeping the connection stable and secure requires good habits.

  • Store credentials safely: Avoid hardcoding passwords in sketches you plan to share publicly. Use the Preferences library to save SSID and password in flash memory.
  • Implement reconnection logic: Always check connection status in loop() and reconnect when needed.
  • Use static IPs for servers: If your ESP32 hosts a web server, assign it a static IP so other devices can find it reliably.
  • Avoid blocking delays: Instead of delay() during connection attempts, consider non-blocking timing with millis() so your sensors keep running.
  • Watch power consumption: WiFi uses significant current. If your project is battery-powered, use sleep modes and wake only when needed.
  • Test signal strength: Use WiFi.RSSI() during development to find the best physical location for your board.
  • Secure your access point: If you use AP mode, always set a password unless you have a specific reason to leave it open.

Frequently Asked Questions

Q: Does the ESP32 support 5 GHz WiFi?
A: No. The standard ESP32 supports 2.4 GHz WiFi only. Make sure your router broadcasts a 2.4 GHz network.

Q: Can I connect the ESP32 to WiFi without Arduino IDE?
A: Yes. You can use ESP-IDF, PlatformIO, MicroPython, or other frameworks. Arduino IDE is simply the most beginner-friendly option for learning How to Connect Esp32 With Wifi?

Q: Why does my ESP32 keep disconnecting from WiFi?
A: Common causes include weak signal, insufficient power, router overload, or missing reconnection code in your sketch.

Q: How do I find my ESP32’s IP address?
A: Open the Serial Monitor after connecting. The IP address prints automatically if you include Serial.println(WiFi.localIP()); in your code.

Q: Can the ESP32 host a web server while connected to WiFi?
A: Yes. In Station mode, the ESP32 can run a web server that other devices on the same network can access using its IP address.

Q: Is it safe to hardcode my WiFi password in the sketch?
A: It is fine for personal projects, but do not upload code with hardcoded passwords to public repositories. Use secure storage instead.

Q: Can I connect to a captive portal network, like hotel WiFi?
A: It is difficult. Captive portals require browser-based authentication, which the ESP32 cannot handle directly without additional libraries or a separate gateway device.

Conclusion

Learning How to Connect Esp32 With Wifi? opens the door to countless IoT possibilities. Whether you choose Station mode to join your home network, Access Point mode to create a standalone network, or dual mode to do both, the ESP32 makes wireless communication straightforward once you understand the basics.

Start with the simple Station mode example, verify your board gets an IP address, and then add reconnection logic and signal monitoring to make your project reliable. From there, you can build web servers, send data to cloud services, or create wireless sensor networks.

Take your time with the wiring and code, keep your credentials secure, and enjoy building connected devices with the ESP32.