กลับไปหน้ารวมไฟล์
esp8266-setup-and-first-wifi-connection-bd42ba-en.md

ESP8266 - Setup and First WiFi Connection

ESP8266 Sunbathing
ESP8266 - Sunbathing

If you've just got an ESP8266 module in your hands but are still confused about how to get started or how to make it work with your projects, don't worry! I've been there before, and today I'm going to share some basic usage techniques that will turn a difficult task into an easy one. This article is a complete guide to help you take your first steps into the world of IoT (Internet of Things) and professionally connect your projects to a WiFi network.

(For anyone interested in exploring more advanced ESP8266 projects, you can visit the blog virginrobotics.blogspot.com)

Why ESP8266?

If you've ever dreamed of controlling your projects over the internet, whether it's operating a servo motor, turning LEDs on/off, or building a small Smart Home system, the ESP8266 is the key to making that dream a reality. It functions similarly to a Bluetooth module like the HC-05 but is much more complex and powerful, as it can take your projects beyond distance limitations via wireless networks.

For beginners, the ESP8266 might seem difficult to use at first (I struggled with it for months myself!). But this article will summarize only the essential content and the fastest way to get it working.


Hardware Requirements

Before you start, you'll need these components for the system to operate stably:

  1. ESP8266 Module (ESP-01 model): The star of the show.
  2. 1k Ohm Resistors (3 pieces): Used to create a Voltage Divider to reduce the TX signal voltage from the Arduino (5V) to 3.3V, preventing module damage.
  3. Breadboard Power Supply Module: (Highly recommended) Because the ESP8266 consumes high current during WiFi data transmission (up to 200-300mA), and the 3.3V pin on an Arduino board often cannot supply enough current, causing the module to reset frequently.
  4. Arduino UNO Board: Used as an intermediary for communication and programming.
  5. Jumper Wires (Male to Male & Female to Male)
  6. Breadboard

Breadboard Power Supply
Separating 5V and 3.3V power rails on a Breadboard for safety.


Circuit Assembly

Important Warning: The ESP8266 operates at 3.3V only. Never connect 5V from the Arduino directly to the VCC pin or send a 5V Logic signal directly to the RX pin, as this will instantly damage the module!

Important Pin Details:

  • VCC: Connect to 3.3V (from a separate power source).
  • GND: Connect all Grounds together.
  • CH_PD (Enable): Must be connected to 3.3V to enable the module.
  • TX: Connect to Arduino Pin 2 (via SoftwareSerial).
  • RX: Connect to Arduino Pin 3 (must go through a Voltage Divider circuit to reduce to 3.3V).

Circuit Diagram

Note: For power supply, the Arduino board can receive power from the Vin pin if you are using an external power source.

Wiring Setup


The Source Code

We will use the Arduino as a "bridge" for communication between the computer's Serial Monitor and the ESP8266, using the SoftwareSerial library to separate the communication pins from the normal Hardware Serial.

#include <SoftwareSerial.h>

// Define RX and TX pins for communication with ESP8266
// Arduino Pin 2 (RX) connects to ESP8266's TX
// Arduino Pin 3 (TX) connects to ESP8266's RX (via Voltage Divider)
SoftwareSerial ESPserial(2, 3); 

void setup() 
{
  // Initialize Serial Monitor at 115200 baud
  Serial.begin(115200);     
  
  // Initialize Software Serial for ESP8266 
  // Factory default is usually 115200 baud
  ESPserial.begin(115200);  
  
  Serial.println("");
  Serial.println("Remember to set Both NL & CR in the serial monitor.");
  Serial.println("Ready");
  Serial.println("");    
}

void loop() 
{
  // Read data from ESP8266 and send to Serial Monitor
  if ( ESPserial.available() )   {  Serial.write( ESPserial.read() );  }
  
  // Read commands from Serial Monitor and send to ESP8266
  if ( Serial.available() )       {  ESPserial.write( Serial.read() );  }
}

Testing with AT Commands

Once the code is uploaded, open the Serial Monitor and set the bottom right to "Both NL & CR" at a speed of 115200 baud.

  1. Type AT and press Enter.
  2. If the module is working correctly, it will respond with OK.

AT Command OK

Step 1: Changing the Baud Rate

Since SoftwareSerial does not work very stably at 115200 baud (which may result in garbage data), we should change the ESP8266's speed to 9600 baud for maximum accuracy.

Send the command:

AT+UART_DEF=9600, 8, 1, 0, 0

Change Baud Rate

If you receive OK, the speed has been changed. Now the data on the screen will become unreadable because the speeds don't match. Close the Serial Monitor and go back to modify the code in the Arduino IDE:

Update Code

Change both Serial.begin(115200) and ESPserial.begin(115200) to 9600. Then upload the code again.

Step 2: Connecting to WiFi

Once communication is re-established at 9600 baud, test by sending AT again to confirm.

AT Test 9600

Then, set the operating mode to Station (Client) mode using the command:

AT+CWMODE=1

CWMODE

Step 3: Scanning for and Connecting to a Network

Try scanning for nearby WiFi networks with the command:

AT+CWLAP

Scan WiFi

Once you find your WiFi name, connect using the command (replace with your WiFi name and password):

AT+CWJAP="Your WiFi Name", "Your Password"

Connect WiFi

If you see WIFI CONNECTED and WIFI GOT IP, congratulations! Your module is now connected to the internet.

Finally, check the module's IP Address using the command:

AT+CIFSR

Check IP

Conclusion and Next Steps

Now you have an ESP8266 module that's ready to use and connected to the outside world. This is the first door to creating more complex IoT projects. In the next article, we will learn how to control a servo motor via a webpage using the ESP8266 and Arduino!

Follow for more cool projects at: virginrobotics.blogspot.com

Final Setup

ข้อมูล Frontmatter ดั้งเดิม

title: "ESP8266 - Setup and First WiFi Connection"
description: "Your first project with the ESP8266: connect to your home's WiFi."
author: "neverofftheinternet"
category: ""
tags:
  - "wifi"
  - "internet of things"
views: 131522
likes: 31
price: 870
difficulty: "Intermediate"
components:
  - "1x SparkFun Breadboard Power Supply Stick 5V/3.3V"
  - "1x Breadboard (generic)"
  - "2x Resistor 1k ohm"
  - "1x ESP8266 ESP-01"
  - "1x Arduino UNO"
  - "1x Male/Female Jumper Wires"
tools: []
apps:
  - "1x Arduino IDE"
  - "1x Windows 10"
downloadableFiles:
  - "https://github.com/imalwaysontheinternet/ESP8266Config"
  - "https://github.com/imalwaysontheinternet/ESP8266Config"
documentationLinks: []
passwordHash: "768f23e538c70dd98f4b1982cf124658772726c3ba661d3d45ab5b7715e69fde"
encryptedPayload: "U2FsdGVkX19zNSMC/RM5m4biW1tQcCytM5u+cPWj74eTZt9x6FF6DOX4hJfdlANOHz0UN7CJ4w4BczoYGU9cRvZoUPJfa2J1ORP7R+Cop2Si6artEvQr78IRKh3Jy3HE"
seoDescription: "Learn how to setup ESP8266 and establish your first WiFi connection with this simple beginner project."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/esp8266-setup-and-first-wifi-connection-bd42ba_cover.jpg"
lang: "en"