Initial Configuration of the Ethernet Shield Arduino Uno

Introduction

We continue our course of the IoT and Arduino. In the previous article, found at this link, it has created an application that uses a sensor TMP36 and a display with which we were able to display the current temperature in the place where you were. The final goals of all is to view and change by phone or tablet thresholds of temperature sensors, all using the services of Azure for the IoT. But to interact with the internet, the Arduino Uno is not enough, you need to equip it with other components. What we will add is called an Ethernet shield, or a card connected to the Arduino Uno and the network via Ethernet cable and RJ45 connectors that will allow access to the Internet. We will see in the order:

  • Key features of the Ethernet card
  • How to connect the Arduino Uno
  • Configuration for Internet access
  • Test the connectivity of Ethernet network
  • Conclusion

Key features of the Ethernet card

  • Supply voltage 5V DC.
  • To operate must be installed above the Arduino Uno, we will see later in the article how.
  • It has an Ethernet controller W5100 with 16k internal memory buffer
  • Connection speed ranging from 10 to 100 mb
  • It must be connected directly with the SPI port of Arduino.
  • It has a slot for SD card.

These are the main features, but for more information, you can see the considerable documentation that already exists here, where we start from the hardware to the final program. The following is the image of the Ethernet Shield.

Ethernet Shield

Ethernet

How to connect Ethernet shield on the Arduino Uno

To connect the Ethernet card and very simple, as you can see in the following pictures, just overlay the Arduino Uno, being careful not to bend or damage the pins. You will see that you replicate the network adapter, all links of the Arduino Uno, then inputs PWM, Analog inputs, inputs of transmission and reception, the output voltage of 5v and zero volts to power the circuits and external components and finally the reset button that will perform the reset, both of the Ethernet card that the Arduino board as in the following:

The two boards after assembly

Arduino Uno Ethernet Shield right and left before being assembled.

two boards

The two boards after assembly. As you can see, the assembly is simple. As you have seen be careful not to damage the pins of the Ethernet card.

Configuration for Internet access

We are past the assembly stage, it's time to configure the adapter so that it can connect to the internet. First we must connect the Arduino Uno to a power supply, or with the power supply that provides an output voltage of 5V DC and at least 2 ampere, or attaching it directly to the PC through a USB port, providing a voltage necessary for the operation of the card. We must have an Ethernet cable with RJ45 connector, to be connected to the router that will be the gateway. Now we start the Arduino IDE and create a new sketch of code called "EthernetTest" as shown and we execute a project save.

The new project EthernetTest
The new project EthernetTest.

Now for part of the code. The first thing to do after you create and save the file sketch is to include the necessary libraries to use the Ethernet Shield, exactly SPI.h and Ethernet.h.

    // Importo le librerie Ethernet
    #include <SPI.h>
    #include <Ethernet.h>

Find these libraries from the menu Sketch -> Include Library -> Ethernet. You will find that more libraries will be added, but we can have a look the two mentioned above.

Adding libraries SPI
Adding libraries SPI and Ethernet.h on project.

Now that the necessary libraries have been added, we can start using them. First we need to define a Media Access Control (MAC) address. It is a unique address for each device that is usually issued by the manufacturer of the network card necessary to identify the device, in our case the Ethernet Shield case if there were more than one. If you do not have this address, we can safely create one, taking care to enter the letter "x" after the first number, because it is an address in hexadecimal numbers. We will enter the MAC address below.

    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address

We define a data type with byte length of six elements, representing the MAC address of the network card. We can still define an Internet Protocol (IP) address. Also in this case it is a unique address that identifies a device in a host network. This step is optional and can directly assign the router an IP address automatically to the Ethernet Shield, this means the DHCP service. The last thing to do and implement the begin() method, passing as arguments the array values mac[], whereas for the IP address of the router will assign one dynamically. We have everything you need to configure the network card, it just must implement the methods setup() and loop(). We insert inside the initialization of the serial port, so you can see on the serial monitor the connection status.

    // Imposto una connessione seriale per verificare i dati.
    Serial.begin(9600);

The following sketch is complete.

  1. /* 
  2.   Name:        EthernetTest.ino 
  3.   Created:    30/05/2015 19:46:36 
  4.   Author:    CARMELO LA MONICA 
  5. */  
  6. // Importo le librerie Ethernet  
  7. #include <SPI.h>  
  8. #include <Ethernet.h>  
  9. // Impostazioni base  
  10. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address  
  11. void setup()  
  12. {  
  13.      // Imposto una connessione seriale per la verifica dei dati.  
  14.      Serial.begin(9600);  
  15.     // Diamo alla Ethernet shield un secondo per inizializzarsi  
  16.      delay(1000);  
  17.     // Connessione Ethernet usando MAC e IP dinamico  
  18.          if(Ethernet.begin(mac) == 0)   
  19.          {  
  20.            Serial.println("Connesso");  
  21.          }  
  22.      
  23.          else  
  24.          {  
  25.            Serial.println("Errore nella connessione");  
  26.          }    
  27. }  
  28. void loop()  
  29. {  
  30. }  
Test the connectivity of Ethernet network

We load the ad code that we can do from the button with an arrow symbol to the right, as shown.

ketch of code in the Arduino

After loading the sketch of code to the Arduino Uno, we connect the entire network. In my case I used an Ethernet output of my router.

Arduino Uno

Arduino Uno and the Ethernet Shield connected to a router.

Now to see if we have actually networked everything we can connect by IP address, to our service providers, in my case Telecom Italia. Again, to get the IP address of a contact where you have a contract ADSL and more.

status of the Ethernet devices before connecting

The status of the Ethernet devices before connecting the Arduino Uno to the router.

status of the Ethernet device

The status of the Ethernet devices after connecting the Arduino Uno to the router.

And as you see, we have a new device connected to the network, it is in fact our Arduino Uno as WINZnetEFFEED, clicking the Details button, we will show all the information, such as IP address that the router has assigned to the board. If we do not trust it yet, we can using the command "CMD.EXE", ping the IP address that the router has assigned.

CMD

Now we type the command: ping and the IP address provided by your router, in my case 192.168.1.4.

Statistics after pinging an IP address

Statistics after pinging an IP address.

And as can be seen, the Arduino board has correctly answered the request.

Conclusion

In this article, we saw the ease with which you can configure an Ethernet shield, assemble it with an Arduino Uno and connect it to a router, then verifying proper operation. In the next article we will start to delve into Microsoft Azure and be sure to communicate with the Arduino Azure services for IoT and the Mobile services.

Up Next
    Ebook Download
    View all
    Learn
    View all