- Arduino with WiFi: A How-To Guide for Adding and Using
- How much an Arduino with WiFi Costs
- WiFi Project Examples
- Getting Started with the Arduino WiFi Shield
- Connecting the Shield
- Using the Shield With Older Boards
- Ports on the WiFi Shield
- Network Settings
- Scan for available networks
- Open network example
- WPA network example
- WEP Network example
- SD Card and SPI
- Updating firmware on the shield
- Next steps
Arduino with WiFi: A How-To Guide for Adding and Using
I found the MKR family to be a strong supporter of connectivity, WiFi in particular. This seems to be in-line with their design intent of being for “makers”, IoT applications, sensor networks, cloud connectivity, and similar applications. Particularly if you need more power than the Nano family.
The built-in WiFi supports 802.11 b/g/n and operates at 2.4Ghz. These Arduino can connect to a WiFi network or act as a WiFi access point.
Mega, Due, and other Uno boards do not come with WiFi built in. WiFi can be added to these boards using a shield or via serial/UART communications, which typically cost about $10. I’ve included more detail below on how WiFi can be added to an Arduino.
To identify if an Arduino has WiFi built in, look for the u-blox chip or the ATSAMW25. The u-blox chip is the more common one as the ATSAMW25 is only found on the MKR 1000 (which, for reference, appears to be using an ATWINC1500 to do the WiFi communication).
The WiFiNINA library should be used with the boards listed above, except perhaps the MKR 1000 which uses a different chip (please reach out if you have experience with this board and I’ll update this guide to include the library for the MKR 1000). If adding a WiFi shield, then Arduino’s WiFi library can be used instead.
In this guide I’ve looked at:
- Cost of Arduino boards that have WiFi included
- How to add WiFi to an Arduino (and how much that costs)
- How to use the Arduino WiFi
- Example projects that take advantage of WiFi
How much an Arduino with WiFi Costs
An Arduino with WiFi can cost can cost as little as $18, however the cost can go over $100 depending on other features of the board. For most WiFi-related projects, expect to spend $20 and $45 on an Arduino with WiFi built in.
The table below lists the popular Arduino models that come with WiFi built-in, and how much they typically cost:
The following functions are the typical process I go through when using WiFi on an Arduino:
Task | Useful Functions |
---|---|
To start a client connection (Arduino reads from another computer) | begin() starts the WiFi connection and returns a status if connected to a network. Parameters include the SSID, password, or WEP details (if using WEP). WiFiClient() creates the client that can be used to talk to a server (locally or over the internet). Use this one if you started the connection with begin() . Once connected to a server, you can issue it commands with println() and read the results with read() . |
To start a server for other devices to connect to (other computers can receive data from Arduino) | beginAP() starts the Access Point mode allowing other devices/computers to connect using WiFi broadcast from the Arduino. Parameters include the SSID, password, and channel number (if applicable). WiFiServer() creates the server that can serve data to clients connected via the WiFi (locally, or if your WiFi Access Point allows, over the internet). Use this one with the beginAP() function. Once a client connects to the Arduino, you can issue it commands with println() and read its requests with read() . |
Common Arduino WiFi tasks and the functions that support them.
Full sketch examples and instructions on how to use can be found on Arduino’s website here.
WiFi Project Examples
Some interesting WiFi project examples I’ve found:
- Send an Arduino message to a message to Discord: Send a Message on Discord – Arduino Project Hub
- Tweeting from an Arduino keypad: Tweet using Arduino, ESP and Keypad – Arduino Project Hub
- Using the internet to keep an accurate clock: Self-Setting Super Accurate Clocks – Arduino Project Hub
Getting Started with the Arduino WiFi Shield
The Arduino WiFi shield allows an Arduino board to connect to the internet using the WiFi library and to read and write an SD card using the SD library.
The WiFi Library is included with the most recent version of the Arduino IDE. The firmware for the WiFi shield has changed in Arduino IDE 1.0.4. It is strongly recommended to install this update per these instructions
The WiFI library is similar to the Ethernet library and many of the function calls are the same.
Connecting the Shield
To use the shield, mount it on top of an Arduino board (e.g. the Uno). To upload sketches to the board, connect it to your computer with a USB cable as you normally would. Once the sketch has been uploaded, you can disconnect the board from your computer and power it with an external power supply.
Digital pin 7 is used as a handshake pin between the WiFi shield and the Arduino, and should not be used.
Using the Shield With Older Boards
If you are using the WiFi shield with an Arduino earlier than the Uno rev3, you need to make the connection below for the board to work. The WiFi board uses the IOREF pin on newer Arduino pin layouts (Uno rev3, Mega2560 rev3, and later) to sense the reference voltage for the I/O pins of the board to which it is attached. If you are using the shield with an older board, you need to connect the shield’s IOREF pin to 3.3V. You can do this either with a jumper wire connecting IOREF to 3.3V as shown in the photo below, or by soldering the IOREF jumper on the bottom of the shield, shown below. WARNING: If you use the solder jumper, do not connect the shield to a rev3 or later board. To be safe, remove the IOREF pin on the shield. Otherwise, you will be shorting 3.3V to 5V through the IOREF pin.
Jumping 3.3V to IOREF (recommended)
Ports on the WiFi Shield
There is an onboard micro-USB connector. This is not for programming an attached Arduino, it is for updating the Atmega 32UC3 using the Atmel DFU protocol. The programming jumper adjacent to the power bus and analog inputs should be left unconnected for typical use. It is only used for DFU programming mode.
A FTDI connection enables serial communication with the 32UC3 for debugging purposes. A list of available commands can be found here.
Network Settings
The shield will connect to open networks, as well as those using WEP and WPA2 Personal encryption. The shield will not connect to networks using WPA2 Enterprise encryption.
The SSID (network name) must be broadcast for the shield to connect.
Depending on your wireless router configuration, you need different information.
- For an open (unencrypted) network, you need the SSID.
- For networks using WPA/WPA2 Personal encryption, you need the SSID and password.
- WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a «Key Index» value. For WEP encrypted networks, you need the SSID, the key, and key number.
Scan for available networks
The sketch below is a good one to run the first time you use the board in a new area. This sketch will not connect to a network, but it will show you what networks the shield can view. Your WiFi shield will probably not see as many networks as a computer with a larger WiFi antenna. Once you have downloaded the sketch to your Arduino, open the serial port to see available networks.
1#include 2 #include 3 4 void setup() 5 6 // initialize serial and wait for the port to open: 7 8 Serial.begin(9600); 9 10 while(!Serial) ; 11 12 // attempt to connect using WEP encryption: 13 14 Serial.println("Initializing Wifi. "); 15 16 printMacAddress(); 17 18 // scan for existing networks: 19 20 Serial.println("Scanning available networks. "); 21 22 listNetworks(); 23 > 24 25 void loop() 26 27 delay(10000); 28 29 // scan for existing networks: 30 31 Serial.println("Scanning available networks. "); 32 33 listNetworks(); 34 > 35 36 void printMacAddress() 37 38 // the MAC address of your Wifi shield 39 40 byte mac[6]; 41 42 // print your MAC address: 43 44 WiFi.macAddress(mac); 45 46 Serial.print("MAC: "); 47 48 Serial.print(mac[5],HEX); 49 50 Serial.print(":"); 51 52 Serial.print(mac[4],HEX); 53 54 Serial.print(":"); 55 56 Serial.print(mac[3],HEX); 57 58 Serial.print(":"); 59 60 Serial.print(mac[2],HEX); 61 62 Serial.print(":"); 63 64 Serial.print(mac[1],HEX); 65 66 Serial.print(":"); 67 68 Serial.println(mac[0],HEX); 69 > 70 71 void listNetworks() 72 73 // scan for nearby networks: 74 75 Serial.println("** Scan Networks **"); 76 77 byte numSsid = WiFi.scanNetworks(); 78 79 // print the list of networks seen: 80 81 Serial.print("number of available networks:"); 82 83 Serial.println(numSsid); 84 85 // print the network number and name for each network found: 86 87 for (int thisNet = 0; thisNetnumSsid; thisNet++) 88 89 Serial.print(thisNet); 90 91 Serial.print(") "); 92 93 Serial.print(WiFi.SSID(thisNet)); 94 95 Serial.print("\tSignal: "); 96 97 Serial.print(WiFi.RSSI(thisNet)); 98 99 Serial.print(" dBm"); 100 101 Serial.print("\tEncryption: "); 102 103 Serial.println(WiFi.encryptionType(thisNet)); 104 105 > 106 >
Open network example
The sketch below shows you how to initiate a connection with an open network named «yourNetwork».
1#include 2 3 char ssid[] = "yourNetwork"; // the name of your network 4 int status = WL_IDLE_STATUS; // the Wifi radio's status 5 6 void setup() 7 8 // initialize serial: 9 10 Serial.begin(9600); 11 12 // attempt to connect to an open network: 13 14 Serial.println("Attempting to connect to open network. "); 15 16 status = WiFi.begin(ssid); 17 18 // if you're not connected, stop here: 19 20 if ( status != WL_CONNECTED) 21 22 Serial.println("Couldn't get a wifi connection"); 23 24 while(true); 25 26 > 27 28 // if you are connected : 29 30 else 31 32 Serial.print("Connected to the network"); 33 34 > 35 > 36 37 void loop() 38 39 // do nothing 40 >
WPA network example
The example below shows how to connect to a WPA/WPA2 Personal encrypted network named «yourNetwork» with a password «12345678».
1#include 2 3 char ssid[] = "yourNetwork"; // your network SSID (name) 4 char pass[] = "12345678"; // your network password 5 int status = WL_IDLE_STATUS; // the Wifi radio's status 6 7 void setup() 8 9 // initialize serial: 10 11 Serial.begin(9600); 12 13 // attempt to connect using WPA2 encryption: 14 15 Serial.println("Attempting to connect to WPA network. "); 16 17 status = WiFi.begin(ssid, pass); 18 19 // if you're not connected, stop here: 20 21 if ( status != WL_CONNECTED) 22 23 Serial.println("Couldn't get a wifi connection"); 24 25 while(true); 26 27 > 28 29 // if you are connected, print out info about the connection: 30 31 else 32 33 Serial.println("Connected to network"); 34 35 > 36 > 37 38 void loop() 39 40 // do nothing 41 >
WEP Network example
Your router will most likely have a settings dialog similar to the one below for generating the WEP key based on an ASCII passphrase:
If you do not have access to your router’s administrative tools, consult your network administrator.
Each key is 10 or 26 hexadecimal digits long (40 or 128 bits) and paired with a key number. For example, a 40-bit key, ABBADEAF01 will work, but ABBADEAF won’t work (too short) and ABBAISDEAF won’t work (I and S are not hexadecimal characters).vFor 128-bit, you need a string that is 26 characters long. D0D0DEADF00DABBADEAFBEADED will work because it’s 26 characters, all in the 0-9, A-F range.
NB : WEP provides a basic encryption mechanism, but it can be cracked. If you require strong encryption for your network, it is recommended you use WPA
The example below shows how to connect to a WEP encrypted network named «yourNetwork» with a hex key of «ABBADEAF01», and a key index of 0.
1#include 2 3 char ssid[] = "yourNetwork"; // your network SSID (name) 4 char key[] = "ABBADEAF01"; // your network key 5 int keyIndex = 0; //your network key Index number 6 int status = WL_IDLE_STATUS; // the Wifi radio's status 7 8 void setup() 9 10 // initialize serial: 11 12 Serial.begin(9600); 13 14 // attempt to connect using WEP encryption: 15 16 Serial.println("Attempting to connect to WEP network. "); 17 18 status = WiFi.begin(ssid, keyIndex, key); 19 20 // if you're not connected, stop here: 21 22 if ( status != WL_CONNECTED) 23 24 Serial.println("Couldn't get a wifi connection"); 25 26 while(true); 27 28 > 29 30 // if you are connected, print out info about the connection: 31 32 else 33 34 Serial.println("Connected to network"); 35 36 > 37 > 38 39 void loop() 40 41 // do nothing 42 >
SD Card and SPI
The WiFI Shield includes a micro-SD card slot, which can be interfaced with using the SD library. The SS for the SD card is pin 4.
Arduino communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used, but it must be kept as an output or the SPI interface won’t work.
Updating firmware on the shield
Please follow this guide to update the firmware on your shield.
Next steps
Refer to the WiFi library page for more information on the functionality of the shield, as well as further examples.
The text of the Arduino getting started guide is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the guide are released into the public domain.