How to Connect Arduino to WiFi: Simple Guide with Example
To connect an
Arduino to WiFi, use a WiFi module like ESP8266 and the WiFi.h library. Initialize the WiFi with your network's SSID and password using WiFi.begin(), then check the connection status until connected.Syntax
Here is the basic syntax to connect Arduino to WiFi using the ESP8266 module:
#include <WiFi.h>: Includes the WiFi library.WiFi.begin(ssid, password);: Starts the connection to the WiFi network.WiFi.status(): Checks the current connection status.WiFi.localIP(): Gets the IP address assigned to the Arduino.
arduino
#include <WiFi.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to WiFi"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Your code here }
Output
...
...
...
Connected to WiFi
IP address: 192.168.1.100
Example
This example shows how to connect an Arduino with an ESP8266 WiFi module to a WiFi network and print the IP address to the Serial Monitor.
arduino
#include <WiFi.h> const char* ssid = "HomeNetwork"; const char* password = "MySecretPass"; void setup() { Serial.begin(115200); delay(1000); Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected successfully!"); Serial.print("Arduino IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Nothing here }
Output
Connecting to WiFi...
...
...
WiFi connected successfully!
Arduino IP address: 192.168.1.105
Common Pitfalls
Common mistakes when connecting Arduino to WiFi include:
- Using wrong
SSIDorpasswordcausing connection failure. - Not waiting long enough for connection before checking status.
- Forgetting to include the correct WiFi library for your board.
- Not initializing Serial communication to see debug messages.
Always verify your network credentials and use while (WiFi.status() != WL_CONNECTED) loop to wait for connection.
arduino
# Wrong: Missing WiFi.begin call #include <WiFi.h> void setup() { Serial.begin(115200); // WiFi.begin(ssid, password); // Forgot this line while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } } // Right: #include <WiFi.h> const char* ssid = "MySSID"; const char* password = "MyPass"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected!"); }
Quick Reference
Tips to connect Arduino to WiFi:
- Use
WiFi.hlibrary for ESP8266 or ESP32 boards. - Call
WiFi.begin(ssid, password)to start connection. - Wait in a loop checking
WiFi.status()untilWL_CONNECTED. - Use
Serial.println(WiFi.localIP())to get your device IP. - Check your router settings if connection fails.
Key Takeaways
Use the WiFi library and call WiFi.begin with your network credentials to connect.
Always wait in a loop checking WiFi.status() until the connection is established.
Print the local IP address to confirm successful connection.
Double-check your SSID and password to avoid connection errors.
Ensure your Arduino board supports WiFi or use a compatible WiFi module like ESP8266.