Sub topic 4
Connecting to Your Network
This chapter assumes you know basic Arduino sketches, can upload code with the Arduino IDE or Arduino CLI, and understand SSID, IP address, and router. You also need a 2.4 GHz Wi-Fi network that you are allowed to use.
Choose the Matching Wi-Fi Library
Arduino boards connect to Wi-Fi in different ways. The library must match the board or its Wi-Fi hardware. This chapter uses the WiFiNINA library with boards such as the Arduino MKR WiFi 1010 and Arduino Nano 33 IoT.
Install the library through the Arduino IDE's Library Manager if it is not already installed. Select the correct board under the Board menu before compiling.
The connection process is:
Keep Credentials Separate
Do not put your network password directly in a sketch that you plan to share. Create a file named arduino_secrets.h in the same folder as the sketch:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"Add this file to your ignore rules, such as .gitignore, so version-control software does not upload it. Replace both placeholder values with your own network details.
Connect and Inspect the Result
The following sketch checks that the Wi-Fi module is available. It then calls WiFi.begin() until the board joins the network. The function returns a status value, such as WL_CONNECTED or WL_NO_MODULE.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(SECRET_SSID);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(10000);
}
Serial.println("Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Signal strength (RSSI): ");
Serial.println(WiFi.RSSI());
}
void loop() {}Open the Serial Monitor at 115200 baud. When the connection succeeds, the board prints an IP address such as 192.168.1.42. This address was usually assigned by your router using DHCP, the Dynamic Host Configuration Protocol.
RSSI means received signal strength indicator. It is measured in decibels relative to one milliwatt, written as dBm. Wi-Fi values are negative. A value near -40 is generally strong, while -80 may be unreliable. The exact result depends on walls, interference, antenna design, and the access point.
The loop retries every ten seconds. This is useful during setup, but it blocks all other sketch work until the network becomes available. A later chapter will replace this pattern with non-blocking reconnection.
Common Connection States
| Observation | Likely cause |
|---|---|
| `Wi-Fi module not found` | The selected board, firmware, or module is not detected |
| Repeated connection attempts | Wrong SSID, password, band, or network availability |
| Connected with a weak RSSI | Distance, obstacles, or radio interference |
| Connected but no internet access | Router, DNS, firewall, or upstream service problem |
If the board cannot connect, verify the spelling and capitalization of the SSID and password. Confirm that the network provides 2.4 GHz access. Also check the board's Wi-Fi firmware with the board vendor's update tool.
Exercise
What does a successful connection provide besides the message "Connected", and why are those values useful?
Show answer
It provides a local IP address and an RSSI value. The IP address identifies the board on the local network, while RSSI helps diagnose weak or unstable radio communication.