
Description:
This tutorial explains about the wiring and testing the different boards of arduino with the Ethernet shield.We will focus on how to wire Arduino UNO R3 to Arduino LAN Ethernet Shield R3 and Arduino MEGA R2 to Arduino LAN Ethernet Shield, then test whether it works or not.
Hardware:
Arduino Uno
Arduin Mega
Ethernet Shield W5100
Notice that, I will only use Serial Cable, Arduino UNO R3, Arduino MEGA R2, and Arduino LAN Ethernet Shield R3 as the model, because there comes a lot of problem when deals with them, so I hope this guidance will help you. Here, the good idea is we do not need to use any ethernet cable.
In order to make sure the devices, here I attached the image:
Arduino UNO R3 with ATMega 328,:
Arduino MEGA R2 with ATMega 2560:
Arduino LAN Ethernet Shield with chip w5100:
Here is the wiring from UNO to Ethernet Shield:
Wiring UNO R3 to Ethernet Shield R3, it is better wiring them than piling them up in order to keep the feet strong (not bent).
Connect ICSP (UNO) to ICSP Header (Eth Shield) with common arrangement.Then, pin 4 to pin 4 and pin 10 to pin 10.
Here is the wiring from Mega to Ethernet Shield:
Wiring them, from ICSP to ICSP header with common pin arrangement, then pin 4 to pin 4 and pin 10 to pin 10,
#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[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFE, 0xAC }; //this is only valid for my Eth Shield, check yours at the back of Eth Shield IPAddress ip(192,168,1,177);// this is the address assigned to my eth shield, so you may create on your own, could be 192,168,1,102, etc // 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() { // Open serial communications and wait for port to open: Serial.begin(9600); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // 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("Connnection: close"); client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); // add a meta refresh tag, so the browser pulls again every 5 seconds: client.println("<meta http-equiv=\"refresh\" content=\"5\">"); // output the value of each analog input pin for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); server.print("analog input "); client.print(analogChannel); client.print(" is "); client.print(sensorReading); client.println("<br />"); } client.println("</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(); Serial.println("client disonnected"); } }
If your uploading seems freezing (often happen to Mega R2, press reset, then try to unplug, plug, then press reset,then upload again). After finish uploading, then open serial monitor:
COMMENTS