0
0
AutocadHow-ToBeginner · 4 min read

How to Use WiFi Module ESP8266 with Arduino: Simple Guide

To use the ESP8266 WiFi module with Arduino, connect its pins to the Arduino's serial pins and power it properly. Then, use the SoftwareSerial library to communicate and send AT commands or program it directly with Arduino IDE for WiFi control.
📐

Syntax

Here is the basic syntax to communicate with the ESP8266 module using Arduino's SoftwareSerial library:

  • SoftwareSerial espSerial(RX, TX); - Creates a serial connection on specified pins.
  • espSerial.begin(baudRate); - Starts serial communication at the given speed.
  • espSerial.println("AT"); - Sends an AT command to the ESP8266.
  • espSerial.read() - Reads data sent back from the ESP8266.

This setup allows Arduino to send commands and receive responses from the ESP8266 module.

arduino
#include <SoftwareSerial.h>

SoftwareSerial espSerial(3, 2); // RX, TX pins

void setup() {
  Serial.begin(9600);         // Arduino serial monitor
  espSerial.begin(115200);   // ESP8266 default baud rate
  espSerial.println("AT");  // Send AT command
}

void loop() {
  if (espSerial.available()) {
    Serial.write(espSerial.read()); // Show ESP8266 response
  }
}
Output
OK
💻

Example

This example connects the ESP8266 to a WiFi network and prints the connection status to the Serial Monitor.

arduino
#include <SoftwareSerial.h>

SoftwareSerial espSerial(3, 2); // RX, TX

void setup() {
  Serial.begin(9600);
  espSerial.begin(115200);
  delay(1000);

  espSerial.println("AT");
  delay(1000);

  espSerial.println("AT+CWJAP=\"YourSSID\",\"YourPassword\"");
  delay(5000);
}

void loop() {
  while (espSerial.available()) {
    String response = espSerial.readString();
    Serial.print(response);
  }
}
Output
OK WIFI CONNECTED WIFI GOT IP
⚠️

Common Pitfalls

Common mistakes when using ESP8266 with Arduino include:

  • Not powering the ESP8266 with a stable 3.3V supply, which can cause resets.
  • Connecting ESP8266 TX/RX pins directly to Arduino 5V pins without level shifting, risking damage.
  • Using wrong baud rates; ESP8266 often defaults to 115200 baud.
  • Not adding delays after sending commands, causing missed responses.

Always check wiring and power before troubleshooting code.

autocad
/* Wrong way: Directly connecting ESP8266 TX to Arduino RX without level shifting */
// This can damage the ESP8266

/* Right way: Use a voltage divider or level shifter on Arduino TX to ESP8266 RX pin */
// For example, two resistors (1k and 2k) to drop 5V to 3.3V

// Also, use 3.3V power supply for ESP8266, NOT 5V
📊

Quick Reference

Summary tips for using ESP8266 with Arduino:

  • Use SoftwareSerial on pins 2 (RX) and 3 (TX) or hardware serial if available.
  • Power ESP8266 with 3.3V and ensure stable current (at least 300mA).
  • Use correct baud rate (usually 115200) for communication.
  • Send AT commands with proper delays and read responses carefully.
  • Consider programming ESP8266 directly with Arduino IDE for advanced projects.

Key Takeaways

Always power ESP8266 with a stable 3.3V supply to avoid resets.
Use SoftwareSerial to communicate with ESP8266 on Arduino pins other than hardware serial.
Send AT commands with proper delays and read responses to confirm actions.
Avoid connecting 5V Arduino pins directly to ESP8266 RX pin; use level shifting.
Test with simple AT commands before building complex WiFi projects.