Arduino - Ethernet Shield 2

Using Arduino Ethernet Shield is one of the easiest ways to connect Arduino to the Internet. You can see other options to connect Arduino to Internet via Ethernet in Arduino - Ethernet tutorial

In this tutorial, we are going to learn:

Arduino Ethernet Shield

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Arduino Ethernet Shield
1×Ethernet Cable
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

Basic Networking Knowledge for Beginners

This part provides basic knowledge for beginners, who do not know anything about networking.

When a device connects to the Internet (via Ethernet or WiFi), it MUST connect to a router or an access point. Router and access point are devices that keep a role as the gateway to the Internet. All data between a device and the Internet is passed through the router or access point.

When a device connects to the Internet, it MUST:

  • Have a MAC address
  • Have an IP address
  • Know network's subnet mask
  • Know gateway's IP address (IP address of router or access point)
  • Know DNS server's IP address (in case of connecting to a domain name)

The device does not work if either:

  • There are one or more other devices in the same network uses the same MAC address of the device (called conflict MAC address)
  • There are one or more other devices in the same network uses the same IP address of the device (called a conflict IP address)

This tutorial does not overload you with the knowledge of networking. Therefore, the tutorial does not explain these concepts. It is out of the scope of this tutorial. Instead, the tutorial shows how to use them.

The MAC address is usually built-in on devices. We do not need to care about it. However, Arduino Ethernet Shield does not have a built-in MAC address. The MAC address is printed on a sticker stuck on the shield. We need to get it and set the MAC address for Arduino Shield in the Arduino code.

There are two methods to assign the IP address, subnet mask, gateway's IP address, and DNS server's IP address to a device (including Arduino):

  1. Use the static IP address:
    • Need to manage the IP address, subnet mask, gateway's IP address, and DNS server's IP address
    • Need to set the IP address, subnet mask, gateway's IP address, and DNS server's IP address manually (for Arduino Ethernet Shield, set this information using Arduino code)
    • It will NOT work if:
      • Set an IP address that is being used by other devices on the same LAN network → IP conflict
      • Set wrong subnet mask, gateway's IP address, and DNS server's IP address
  • Obtain an IP address automatically(DHCP), also called the dynamic IP address
    • The device automatically obtains the IP address, subnet mask, gateway's IP address, and DNS server's IP address from the router or access point using DHCP. We do not need to manage the IP address, subnet mask, gateway's IP address, and DNS server's IP address. Router/access point does this work for us.

    When you connect your smartphone to your WiFi home network, the second method is used.

    When using Arduino, The first method is hard for beginners. The second method is easy and recommended for beginners who do NOT know about networking.

    How to use Arduino Ethernet Shield

    Arduino communicates with Ethernet Shield via the SPI interface. The following shows how to use Arduino Ethernet Shield.

    • Stack Ethernet Shield on Arduino Uno or Mega
    • Connect PC to Arduino Uno/Mega via USB cable
    • Connect Ethernet Shield to router/access point via Ethernet cable
    • Open Arduino IDE and write code
    • Upload the code to Arduino Uno/Mega

    It is completely simple.

    Skeleton Arduino code for Arduino Ethernet Shield when using the dynamic IP address

    Prerequisite

    • We need to know MAC address of Ethernet Shield (get from a sticker provided by manufacturer)

    How to program step by step

    • Include library
    #include <SPI.h> #include <Ethernet.h>
    • Declare MAC address
    // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    • Declare other objects depending on your application.
    • Start the Ethernet Shield
    // initialize the Ethernet shield using DHCP: Ethernet.begin(mac);
    • (Optional) Print out Arduino's IP address, subnet mask, gateway's IP address, and DNS server's IP address
    Serial.print("Arduino's IP Address: "); Serial.println(Ethernet.localIP()); Serial.print("DNS Server's IP Address: "); Serial.println(Ethernet.dnsServerIP()); Serial.print("Gateway's IP Address: "); Serial.println(Ethernet.gatewayIP()); Serial.print("Network's Subnet Mask: "); Serial.println(Ethernet.subnetMask());

    Note that, Arduino can not get an IP address in the following cases:

    • The router or Access Point does not support DHCP
    • Ethernet cable is not plugged properly or broken

    To dectect these probem, we can modify the Ethernet.begin(mac); to:

    if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true); }

    ※ NOTE THAT:

    If you do NOT change the MAC address, it may still work. However, It may NOT work if it conflicts with the MAC address of another device in the same LAN network. In the case of losing the MAC address sticker, just use the above the MAC address for testing.

    The complete skeleton code when using the dynamic IP address

    /* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ethernet-shield-2 */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // TODO: Declare something depending on your application void setup() { Serial.begin(9600); // initialize the Ethernet shield using DHCP: Serial.println("Obtaining an IP address using DHCP"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to obtaining an IP address"); // check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("Ethernet shield was not found"); // check for Ethernet cable if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected."); while (true); } // print out Arduino's IP address, subnet mask, gateway's IP address, and DNS server's IP address Serial.print("- Arduino's IP address : "); Serial.println(Ethernet.localIP()); Serial.print("- Gateway's IP address : "); Serial.println(Ethernet.gatewayIP()); Serial.print("- Network's subnet mask : "); Serial.println(Ethernet.subnetMask()); Serial.print("- DNS server's IP address: "); Serial.println(Ethernet.dnsServerIP()); // TODO: initialize something depending on your application } void loop() { // TODO: do something depending on your application }

    Upload the above code to Arduino. If succeeded, Serial Monitor shows something like below:

    COM6
    Send
    Obtaining an IP address using DHCP - Arduino's IP address : 192.168.0.5 - Gateway's IP address : 192.168.0.1 - Network's subnet mask : 255.255.255.0 - DNS server's IP address: 8.8.8.8
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

    If the Serial Monitor does NOT show like above, check your Ethernet cable. If it still does NOT work, go to the next part to try using the static IP address.

    Skeleton Arduino code for Arduino Ethernet Shield when using the static IP address

    Prerequisite

    How to program step by step

    • Include library
    #include <SPI.h> #include <Ethernet.h>
    • Declare MAC address
    // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    • Declare an unused IP address, subnet mask, gateway's IP address, and DNS server's IP address
    // change the IP address, subnet mask, gateway's IP address, and DNS server's IP address IPAddress ip(192, 168, 0, 5); IPAddress gateway(192, 168, 0, 1); IPAddress subnet(255, 255, 255, 0); IPAddress myDns(8, 8, 8, 8);
    • Declare other objects depending on your application.
    • Start the Ethernet Shield
    // initialize the Ethernet shield using the static IP address: Ethernet.begin(mac, ip, myDns, gateway, subnet);
    • Optional, check network link status
    if (Ethernet.linkStatus() == LinkON) Serial.println("Link status: On"); else Serial.println("Link status: Off");

    ※ NOTE THAT:

    If you do NOT change the MAC address, it may still work. However, It may NOT work if it conflicts with the MAC address of another device in the same LAN network. In the case of losing the MAC address sticker, just use the above the MAC address for testing.

    The complete skeleton code when using the static IP address

    /* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ethernet-shield-2 */ #include <SPI.h> #include <Ethernet.h> // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // change the IP address, subnet mask, gateway's IP address, and DNS server's IP address depending on your network IPAddress ip(192, 168, 0, 5); IPAddress gateway(192, 168, 0, 1); IPAddress subnet(255, 255, 255, 0); IPAddress myDns(8, 8, 8, 8); // TODO: Declare something depending on your application void setup() { Serial.begin(9600); // initialize the Ethernet shield using the static IP address: Ethernet.begin(mac, ip, myDns, gateway, subnet); // TODO: initialize something depending on your application } void loop() { // optional, check link status if (Ethernet.linkStatus() == LinkON) Serial.println("Link status: On"); else Serial.println("Link status: Off"); // TODO: do something depending on your application }

    Upload the above code to Arduino. If succeeded, Serial Monitor shows something like below:

    COM6
    Send
    Link status: On Link status: On Link status: On
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

    If the Serial Monitor does NOT show like above:

    • Check you Ethernet cable
    • Make sure that the IP address belongs to your network
    • Make sure that IP address does NOT conflict with other devices on the same network
    • Make sure subnet mask, gateway's IP address, and DNS server's IP address is set correctly

    Additional Knowledge of Networking for Beginners

    In the case of using the static IP, you need to get an unused IP address, subnet mask, gateway's IP address, and DNS server's IP address. The below shows how to get them.

    If Arduino and your PC connect to the same network

    • Open Command Prompt on your PC
    • Type the following command on Command Prompt:
    ipconfig
    Command Prompt
    C:\Windows\system32>ipconfig Ethernet adapter Ethernet 2: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Wireless LAN adapter Wi-Fi: Connection-specific DNS Suffix . : Link-local IPv6 Address . . . . . : fe80::d559:ae92:8dff:a604%20 IPv4 Address. . . . . . . . . . . : 192.168.0.14 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.0.1
    • From above, you can get the subnet mask and gateway's IP address
    • You can see your PC's IP address from the above output. We will try to find an unused IP address based on PC's IP address
      • Increase or decrease one by one, start from the PC's IP address.
      • Send ping command to that IP address on Command Prompt. Repeat these two steps until the IP is unreachable. If the address is unreachable, It is an unused IP address and we can use it for Arduino. For example:
      ping 192.168.0.16
      Command Prompt
      C:\Users\youruser>ping 192.168.0.16 Pinging 192.168.0.16 with 32 bytes of data: Reply from 192.168.0.14: Destination host unreachable. Reply from 192.168.0.14: Destination host unreachable. Reply from 192.168.0.14: Destination host unreachable. Reply from 192.168.0.14: Destination host unreachable. Ping statistics for 192.168.0.16: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

      ※ NOTE THAT:

      In some case, even IP address is unreachable, it may being used by another device. However, this case usually does not happen in your home network.

      • For DNS server's IP address , we can simply use 8.8.8.8

      Now you can use this shields to connect Arduino to Internet. See other example on See Also part.

      WARNING

      Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

    The Best Arduino Starter Kit

    ※ OUR MESSAGES