Arduino Ethernet Shield Tutorial

922,027

762

121

About: My name is Randy and I am a Community Manager in these here parts. In a previous life I had founded and run the Instructables Design Studio (RIP) @ Autodesk's Pier 9 Technology Center. I'm also the author ...

The Arduino Ethernet Shield allows you to easily connect your Arduino to the internet. This shield enables your Arduino to send and receive data from anywhere in the world with an internet connection. You can use it to do fun stuff like control robots remotely from a website, or ring a bell every time you get a new twitter message. This shield opens up endless amounts of possibility by allowing you to connect your project to the internet in no-time flat.

Step 1: Setup

Setting it up is as simple as plugging the header pins from the shield into your Arduino.

Note that the Ethernet Shield used here used to be sold at RadioShack. However, since RadioShack is no more, the shield is harder to come by. This generic ethernet shield available on Amazon should also work.* It is meant to be used with the Arduino Uno Rev. 3 boards (or later). It has too many pins to plug into earlier version Arduino boards.

*I have not personally confirmed this. Buyer beware!

Step 2: Shield Features

The Ethernet Shield is based upon the W51000 chip, which has an internal 16K buffer. It has a connection speed of up to 10/100Mb. This is not the fastest connection around, but is also nothing to turn your nose up at.

It relies on the Arduino Ethernet library, which comes bundled with the development environment.

There is also an on-board micro SD slot which enables you to store a heck-of-a-lot of data, and serve up entire websites using just your Arduino. This requires the use of an external SD library, which does not come bundled with the software. Using the SD card is not covered in this Instructable. However, it is covered in the Step 8 of the Wireless SD card instructable.

The board also has space for the addition of a Power over Ethernet (PoE) module, which allows you to power your Arduino over an Ethernet connection.

For a full technical overview, see the official Ethernet Shield page.

Step 3: Get Started

Plug the Arduino into your computer's USB port, and the Ethernet shield into your router (or direct internet connection).

Next, open the Arduino development environment. I highly recommend upgrading to Arduino 1.0 or later (if you have not done so already). This version of the software has built in DHCP support, and does not require manually configuring an IP address.

To figure out what IP address has been assigned to your board, open the DhcpAddressPrinter sketch. This can be found at:

File --> Examples --> Ethernet --> DhcpAddressPrinter

Once open, you may need to change the Mac address. On newer versions of the Ethernet shield, you should see this address on a sticker attached to the board. If you are missing a sticker, simply making up a unique mac address should work. If you are using multiple shields, make sure each has a unique mac address.

Once the mac address is properly configured, upload the sketch to your Arduino, and open the serial monitor. It should print out the IP address in use.

Step 4: Server

You can use the Arduino Ethernet shield as a web server to load an HTML page or function as a chat server. You can also parse requests sent by a client, such as a web browser. The following two examples show how to use it to serve HTML pages, and parse URL strings.

One important thing to keep in mind is that you will have to enter your Arduino's IP address in both of the examples below in order for them to work.

The following code changes the web page served based on a button press:
<pre>/*
  Web Server Demo
  thrown together by Randy Sarafan
 
 A simple web server that changes the page that is served, triggered by a button press.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Connect a button between Pin D2 and 5V
 * Connect a 10K resistor between Pin D2 and ground
 
 Based almost entirely upon Web Server by Tom Igoe and David Mellis
 
 Edit history: 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

int buttonPress = 1;

void setup()
{
  pinMode(2, INPUT);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  buttonPress = digitalRead(2); 
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //serves a different version of a website depending on whether or not the button
          //connected to pin 2 is pressed.
          if (buttonPress == 1) {
            client.println("<cke:html><cke:body bgcolor=#FFFFFF>LIGHT!</cke:body></cke:html>");
          }
          else if (buttonPress == 0){
            client.println("<cke:html><cke:body bgcolor=#000000 text=#FFFFFF>DARK!</cke:body></cke:html>");
          }
          

          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

To make this example code work, simply attach a button between pin D2 and 5V, a 10K resistor between pin D2 and ground, and then load the IP address of your Arduino into your web browser. The page should load with a black background. Press and hold the button, and then refresh the browser page. The site should now load with a white background.


The following code lights up an LED depending on the URL that is sent to the Arduino:
<pre>/*
  Web Server Demo
  thrown together by Randy Sarafan
 
 Allows you to turn on and off an LED by entering different urls.
 
 To turn it on:
 http://your-IP-address/$1
 
 To turn it off:
 http://your-IP-address/$2
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Connect an LED to pin D2 and put it in series with a 220 ohm resistor to ground
 
 Based almost entirely upon Web Server by Tom Igoe and David Mellis
 
 Edit history: 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 
 */

#include <SPI.h>
#include <Ethernet.h>

boolean incoming = 0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
IPAddress ip(191,11,1,1); //<<< ENTER YOUR IP ADDRESS HERE!!!

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  pinMode(2, OUTPUT);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        
        //reads URL string from $ to first blank space
        if(incoming && c == ' '){ 
          incoming = 0;
        }
        if(c == '

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm resistor to ground.

To turn on the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$1

To turn off the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$2

Note: You should obviously replace [YOUR IP ADDRESS HERE] with your IP address.){ incoming = 1; } //Checks for the URL string $1 or $2 if(incoming == 1){ Serial.println(c); if(c == '1'){ Serial.println("ON"); digitalWrite(2, HIGH); } if(c == '2'){ Serial.println("OFF"); digitalWrite(2, LOW); } }

if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } }

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm resistor to ground.

To turn on the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$1

To turn off the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$2

Note: You should obviously replace [YOUR IP ADDRESS HERE] with your IP address.

Step 5: Client

You can also use the Ethernet Shield as a client. In other words, you can use it to read websites like a web browser.

Websites have a lot of text both visible and hidden, which makes programming on the client side very tricky. Reading information from websites typically involves parsing a lot of strings. This is maddening, but worth it, if that is what you intend to do.

I was going to write some code to read Twitter messages, but such a code already exists as an example within the Arduino programmer. Instead, I simply modified it slightly to turn on an LED if a special message is read.

To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220 ohm resistor to ground.

Don't forget to enter your own IP address into the code below, or it will not work.

Here is the code:

Presumably you are going to want to read something other than the recent post on the RandyMcTester Twitter feed.

To read other Twitter feeds, change the following bit of text:
client.println("GET /1/statuses/user_timeline.xml?screen_name=[NEW TWITTER NAME HERE]&count=1 HTTP/1.1");

Share

Recommendations

  • Make it Glow Contest 2018

    Make it Glow Contest 2018
  • First Time Author

    First Time Author
  • PCB Contest

    PCB Contest

121 Discussions

0
None
GauravJ41

Question 10 months ago on Step 3

How I'm to change mac address?
HowOnce the mac address is properly configured, upload the sketch to your Arduino, and open the serial monitor. It should print out the IP address in use?

0
None
itsabhi

1 year ago

Hi, This post is really informative.

I'm facing difficulty connecting arduino ethernet with PC using a switch (Dlink). When both are connected directly(p2p) using ethernet cable, arduino device can be pinged. However, when connected via switch - ping doesn't work, and both devices can't communicate with each other; updated the MAC addr. several times, nothing worked. I'm stuck. Can you suggest something on this?

1 reply
0
None
mystercocoitsabhi

Reply 11 months ago

Hi, your problem could come from the lack of Auto MDI-X. You may have a crossover ethernet cable instead of a straight cable. Try to change your ethernet cable. If not working, try to reboot the switch or change the port.

0
None
MikeT267

1 year ago

I had problems running the sketch until I removed the SD card. It seems my Ethernet shield won’t work of the SD card is plugged in.

Thanks for such a useful forum.

0
None
sayedmosalem

1 year ago

I have connected the eithernet shield directly to my laptop and put them in the same network, after that I was able to switch the led on/off. but when I put my lap top and the shield to the my Home network I can ping the shield from the laptop but I can't switch the led on/off.

0
None
novianiw

1 year ago

from this program i cant get my ip address from ethernet shield..
i only get server is at 0.0.0.0 -> this ip address from web server
what i should do ? i am using ethernet shield HanRun HR911105A 16/02

0
None
RogerioAlencar

2 years ago

Arduino with AJAX

https://www.youtube.com/playlist?list=PLbUAcqHuByzfm9od5kHKArjKIAtfbT0LD

1 reply
0
None
Maruf93RogerioAlencar

Reply 1 year ago

It would be better if you make it in English...I don't understand anything from your video....

0
None
MORACK

1 year ago

I HAVE RECENTLY PURCHASED AN ARDUINO KIT WITH ETHERNET SHIELD AND I AM NOT ABLE TO UPLOAD THE PROGRAM ON THE BOARD.THE ERROR SHOWN IS :

THE I/O OPERATION HAS BEEN ABORTED BECAUSE OF EITHER A THREAD EXIT OR AN APPLICATION REQUEST

0
None
KaushalK

3 years ago on Introduction

I get this message :
Failed to configure Ethernet using DHCP
any idea how to fix this thx ?

ipconfig /all gives me the physical address that i put in mac[].
it also says dhcp enabled : no
even if code is for DHCPaddressPrinter.

in case of ethernet server code :
i cant open my ip address in web browser.
when i ping it says "destination host unreachable".

2 replies
0
None
supul.sapukotanaKaushalK

Reply 2 years ago

Have you set up the ip of the Ethernet shield? the ip should be same range as your PC's ip like

192.168.0.2 for PC

192.168.0.10 for shield.

And also some cheap W5100 shields has a problem with a resistor array situated near the RJ45 socket. It should be 510 instead of 511 (51Ohm instead of 510ohm)

0
None
ScottB308supul.sapukotana

Reply 2 years ago

So how do you know if your buying the good one if there is no version numbers on these board ? Also Are you saying the ones with the Bad Resistor values are NO GOOD at all or flakey ?

0
None
ReinaldN

2 years ago

hi,

i've got the same problem. set a fixed ip.. it doesn't solve the problem but you go further

IPAddress ip(132 ,206 ,95 ,251);
Ethernet.begin(mac, ip);

0
None
Jan LoubelleB

2 years ago

I get this message :
Failed to configure Ethernet using DHCP
any idea how to fix this ?
Pls reply asap.
Thanks, it would be a great help

1 reply
0
None
SarangA1

2 years ago

Hey, I have been using HanRun HR911105A and when I connect it to my Laptop through ethernet cable it doesn't detect the connection and there is no blinking of LED's on the ethernet shield as well as on the Laptop.

Thanks in advance hopefully someone can help!!

3 replies
0
None
Jan LoubelleBSarangA1

Reply 2 years ago

Can i ask if whats your mac adress for your ethernet? We have a project and its the same ethernet shield we use, but we dont know the mac address. Reply asap. Thank you

0
None
DaveB101SarangA1

Reply 2 years ago

The Arduino shield and the computer both have the same RJ45 connections. That is, the send and receive pins are on the same pin for both sender and receiver. If you connect them directly with a cable, the send pins match up and the receive pins match up and therefore you get no communications.

You either need an Ethernet hub/switch or a cross over cable that crosses over the Send and Receive pins. The hub/switch is definetly the easiest way to go and you can pick them up cheaply from amazon.

0
None
simonM90SarangA1

Reply 2 years ago

Just a thought, when you normally plug your Arduino board into your computer the usb cable supplies the power. Most ethernet does not support power over internet (POI), so unless you are also connecting to your PC using usb at the same time you will need to supply external power to your board.

If you already provided external power ignore this comment.

0
None
rajdey

2 years ago

Hi ! Your
instructions are very well managed and self explanatory. But I’m
facing issues at the beginning. Whenever I’m powering up the UNO board, the
chip on the Ethernet shield is getting hit up within 2-3 secs. So, I'm unable to start the shield at all.

Please suggest / help...