Connecting Arduino to the Internet with the W5100 Ethernet module
IoT devices are connected to the Internet using Wi-Fi. Although a Wi-Fi connection enables remote access to the Internet, it has its own l
3
Project description
the Internet, it has its own limitations. For example, in most industrial applications for a fixed device, Ethernet is usually chosen over Wi-Fi, because it is more reliable and secure. Also, since Ethernet is a wired connection, it offers better speed and lower latency. If you are working with an Arduino UNO or Nano board and want to connect your Arduino to the Internet, you have two popular options. One option is to use ESP8266 with Arduino and the second option is to connect Ethernet module with Arduino .
In this project, we want to use the second option to control an LED with Arduino and W5100 Ethernet Module. The Ethernet module is used to communicate between the computer and the Arduino in a LAN or Wired network.
Ethernet module W5100
We use W5100 Ethernet module. Simply put, this module enables the Arduino board to connect to the Internet. Using this module, we can complete the Internet connection without the support of an operating system. It also supports Hardwired TCP/IP protocols such as TCP, PPPoE, Ethernet, UDP, ICMP, IPv4, etc. It supports duplex and half-duplex operation modes. Supports ADSL connections and up to four simultaneous socket connections.

The Ethernet module uses the Arduino Ethernet library to write codes that connect to the Internet using this module. Compatible with all Arduino versions such as UNO, NANO, mega 1280, mega 2560 and other versions. The functionality and performance of the W5100 Ethernet module is very similar to the Arduino Ethernet Shield, and its price is much lower.

Arduino connection circuit schematic to W5100 Ethernet module

This circuit consists of Arduino Nano and Ethernet Module W5100 (this project is also possible with Ethernet Arduino Shield and Arduino UNO). The connection between the Arduino module and Ethernet is very simple as shown in the circuit diagram.
Connect the Arduino Nano's 5V and GND pins to +5 and G of the Ethernet module, respectively (these connections provide power to the Ethernet module).
Connect Arduino pins 9, 10, 11,12 and 13 to R, SS, MO, MI, CK respectively of the Ethernet module (this establishes communication between Arduino and Ethernet via SPI ).
Arduino Code to receive data from W5100 Ethernet Module
The complete code of the project is placed at the end of the page in the downloadable file. Here we review the important parts of the code. First, we call the SPI library, which is used to communicate SPI with the Ethernet module.
#include <SPI.h>
The Ethernet library has functions that simplify the interaction with the Ethernet module.
#include <Ethernet.h>
We set the physical Mac address of the Ethernet module.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };The number 192.168.0.12 is the IP address. To check the IP address, just type ipconfig in CMD.

The code creates a local server on port 80 that can later be accessed by clients.
EthernetServer server(80);
It reads the user response when the user accesses the web server.
String readString;
In the setup, we set the LED pin as the output and configure the Ethernet module with the desired parameters (Mac address, IP address, gateway, subnet mask) and finally the "server.begin()" function of the server starts.
int ledPin = 2;
void setup(){
pinMode(ledPin, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
}In the void loop, we create a client connection and check if someone is trying to access the assigned IP address through a browser.
void loop(){
EthernetClient client = server.available();
if (client) {
while (client. connected()) {
if (client.available()) {In case of connection, checking whether the client sends a message to the server or not.
char c = client.read();
Then we request the HTTP response character by character and store the characters in the readString variable defined by the user.
if (readString.length() < 100) {
readString += c;
}After receiving the confirmation response, we start displaying the web page and the HTML code is below.
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE> ARDUINO ETHERNET</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<hr>");
client.println("<H1 style=\"color:green;text-align:center\">ARDUINO ETHERNET LED CONTROL</H1>");
client.println("<hr>");
client.println("<br>");When the user clicks on the TURN ON LED link, he is redirected to \?LEDON, which turns on the LED.
client.println("<H2 style=\"text-align:center\"><a href=\"/?LEDON\"\">Turn On LED</a><br></H2>");Similar to the above code, the following code directs the user to the TURN OFF LED address.
client.println("<H2 style=\"text-align:center\"><a href=\"/?LEDOFF\"\">Turn Off LED</a><br></H2>");Depending on the address the user is directed to, control the Arduino pin to turn the LED on and off.
if(readString.indexOf("?LEDON") > -1)
{
digitalWrite(ledPin, HIGH);
}
else{
if(readString.indexOf("?LEDOFF") > -1)
{
digitalWrite(ledPin, LOW);
}
}Clear string for next read:
readString="";
}
}
}
}
}Connect Arduino to computer or Ethernet port
To connect these two (PC and Arduino) to each other, if the Ethernet port of the computer does not have auto-direction sensing, we need a suitable cable (CAT-6 crossover cable). If your PC supports auto-direction sensing, just connect a regular cable that comes with the router.

If your computer does not have an Ethernet port, you can use the following method to connect.

LED flashing over the Internet using an Ethernet module
After making the above connections, connect to the router using your PC or mobile phone. Then open the web browser and enter the IP address. Now you can control the LED through the web page. When the user clicks on “TURN ON LED” on the web page, the LED in the circuit turns on. When “LED TURN OFF” is clicked, the LED in the circuit will turn off. The web page is as follows:

I hope you enjoyed this project and learned how to use the W5100 Ethernet module with Arduino to send and receive data over the Interne
