Arduino Nano - Ethernet
This guide shows you how to connect the Arduino Nano to the internet or your local network using the W5500 Ethernet module. We will learn the following details:
Connecting Arduino Nano to W5500 Ethernet Module
Writing Code for Arduino Nano to Make HTTP Requests via Ethernet
Creating a simple Web Server on Arduino Nano with Ethernet
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand,
DIYables .
The W5500 Ethernet module offers two kinds of connections:
RJ45 interface: Use this to connect to a router or switch with an Ethernet cable.
SPI interface: Use this for connecting to an Arduino Nano board. It includes 10 pins:
NC pin: Leave this pin unconnected.
INT pin: Leave this pin unconnected.
RST pin: This is the reset pin. Connect it to the Arduino Nano's EN pin.
GND pin: Connect this pin to the Arduino Nano's GND pin.
5V pin: Connect this pin to the Arduino Nano's 5V pin.
3.3V pin: Leave this pin unconnected.
MISO pin: Connect this pin to the Arduino Nano's SPI MISO pin.
MOSI pin: Connect this pin to the Arduino Nano's SPI MOSI pin.
SCS pin: Connect this pin to the Arduino Nano's SPI CS pin.
SCLK pin: Connect this pin to the Arduino Nano's SPI SCK pin.
image source: diyables.io
This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
image source: diyables.io
This code works as a web client. It sends HTTP requests to the web server found at http://example.com/.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "example.com";
String PATH_NAME = "/";
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Arduino Nano - Ethernet Tutorial");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address");
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield was not found");
if (Ethernet.linkStatus() == LinkOFF)
Serial.println("Ethernet cable is not connected.");
while (true)
;
}
if (client.connect(HOST_NAME, HTTP_PORT)) {
Serial.println("Connected to server");
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("disconnected");
} else {
Serial.println("connection failed");
}
}
void loop() {
}
Connect the Ethernet module to the Arduino Nano as shown in the wiring diagram.
Connect the Arduino Nano to your computer with a USB cable.
Use an Ethernet cable to connect the Ethernet module to your router or switch.
Open the Arduino IDE on your computer.
Select the right Arduino Nano board and the COM port.
Click to the Libraries icon on the left bar of the Arduino IDE.
Search “Ethernet”, then find the Ethernet library by Various
Click Install button to install Ethernet library.
Open the Serial Monitor in the Arduino IDE.
Copy the code given and paste it into the Arduino IDE.
Press the Upload button in the Arduino IDE to send the code to the ESP25.
Look at the Serial Monitor to see the output, which should show the result as indicated.
Arduino Nano - Ethernet Tutorial
Connected to server
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 208425
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Fri, 12 Jul 2024 07:08:42 GMT
Etag: "3147526947"
Expires: Fri, 19 Jul 2024 07:08:42 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (lac/55B8)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
Connection: close
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
disconnected
※ NOTE THAT:
If another device on the same network uses the same MAC address, it may not function properly.
The code below turns the Arduino Nano into a web server. This server delivers a simple webpage to internet browsers.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
EthernetServer server(80);
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Arduino Nano - Ethernet Tutorial");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address");
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield was not found");
if (Ethernet.linkStatus() == LinkOFF)
Serial.println("Ethernet cable is not connected.");
while (true)
;
}
server.begin();
Serial.print("Arduino Nano - Web Server IP Address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
bool currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");
client.println("<h1>Arduino Nano - Web Server with Ethernet</h1>");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disconnected");
}
}
Copy the above code and paste it into the Arduino IDE
Click the Upload button in the Arduino IDE to send the code to your Arduino Nano
Check the results on the Serial Monitor, it will display as follows:
Arduino Nano - Ethernet Tutorial
Arduino Nano - Web Server IP Address: 192.168.0.2