Arduino Mega 2560 ethernet/sd shield issue.

Hey this is my first time on form, so I might make a few mistakes, hopefully we can find one and get the board working : ). I have been following many different threads looking to get the Ethernet shield to server up a webpage and/or write to the SD card. I am using the mega 2560 and have followed this website many times looking for a solution. http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/

I have +3.3V,+5V,GND,GND,Vin wired up between the two boards.

MEGA pin 50 (MISO) to Arduino Ethernet Shield pin 12.
MEGA pin 51 (MOSI) to Arduino Ethernet Shield pin 11.
MEGA pin 52 (SCK) to Arduino Ethernet Shield pin 13.

MEGA pin 4 (SS) to Arduino Ethernet Shield pin 4.
*I have also tried MEGA pin 53 to Ethernet Shield pin 4.

When working the SD I am using the example CardInfo and always get this error:
Initializing SD card...initialization failed. Things to check:

  • is a card is inserted?
  • Is your wiring correct?
  • did you change the chipSelect pin to match your shield or module?
/*
  SD card test 
   
 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.
 	
 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module. 
 		Pin 4 used here for consistency with other Arduino examples

 
 created  28 Mar 2011
 by Limor Fried 
 modified 16 Mar 2011
 by Tom Igoe
 */
 // include the SD library:
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;    

void setup()
{
  Serial.begin(9600);
  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(53, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {
  
}

For using the Ethernet to server up a webpage I have been using the following code. I get 100M LED on, Link LED on, and power light on, and the Rx LED will blink when requesting 192.168.1.177.
I use another laptop (with wi-fi off) and ethernet switch to connect to the Ethernet shield.
I get an error "The connection has timed out" on the laptop when 192.169.1.177 is searched for in a mozilla web brower.

/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 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[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);

// 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()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}

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
        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();

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
          }
          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();
  }
}

With same wiring as above for MOSI, MISO, and CLK.
MEGA pin 10 (SS) to Arduino Ethernet Shield pin 10.
*I have also tried MEGA pin 53 to Ethernet Shield pin 10.

I am using this arduino Mega 2560:

And this Ethernet shield:

And lastly using arduino 1.0 on XP

I would like to get both ethernet webserver and SD card to work at the same time but first I just want to get them working independently.

The only thing I haven’t tried which I have seen on forum and additional links, is trying to change a .h file. With arduino 1.0 there is some changes in the libraries file structure since 022,023 and I can’t find what to change. Any suggestions with a .h or anything else would be great. Thanks! Nathan

Hi Nathan. No jumpers required with that shield. That shield should plug in as-is.

The ethernet w5100 SPI Slave Select is digital pin 10. HIGH is disabled. LOW is enabled.
The microSD SPI slave select is digital pin 4. HIGH is disabled. LOW is enabled.

To communicate with the SD card, disable the w5100 SPI (pin 10 HIGH) and enable SD SPI (pin 4 LOW).
To communicate with the w5100 ethernet IC, disable SD SPI (pin 4 HIGH) and enable the w5100 SPI (pin 10 LOW).

I always disable one, then enable the other. That is just me...

Add: If you plan on using the V1.0 ethernet library, take a look at this post.
http://arduino.cc/forum/index.php/topic,86729.0.html
If you are using Windows, just the web server code will apply to you. I think only Linux GCC has problems with the w5100.h bug.

Surfer Tim,

You are the man!!!! :slight_smile: I had originally tried putting the Ethernet/SD shield on top but when it didn't work, I saw all these other websites showing these jumpers so I was trying that.

Both the SD and ethernet work :slight_smile:

Thanks!!!!!
Nathan

I'm trying to do the same thing. Could either of you post a code snippet for this process?

Here is the basic setup routine I use.

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

// put your ethernet variables (mac, ip) here

void setup()
{
   Serial.begin(9600);

   // disable w5100 SPI while setting up SD
   pinMode(10,OUTPUT);
   digitalWrite(10,HIGH);

   // set up SD
   if(SD.begin(4) == 0) 
      Serial.println("SD failed");
   else
      Serial.println("SD ok");

   // set up w5100
   Ethernet.begin(mac,ip);
   // disable w5100 SPI
   digitalWrite(10,HIGH);

   // takes a second for the w5100 to get ready
   delay(2000);

   // rest of your setup
   Serial.println("Setup done");
}

void loop()
{

}

Don't go any farther until you get "SD ok" on the serial monitor. You won't get that until you have a correctly formatted microSD card inserted in the slot.

SurferTim, you just helped me solve this problem I have been dealing with for a week. No one had suggested setting pin 4 to low if I wanted to use the Sd card, but when I did that, it worked. I felt like crying a little. Thanks.

Some details:

BOARD: MEGA 2560
SHIELD: Arduino Ethernet Shield
RESULT: Success!!

i have used below code to check if it does work it didn't, am i doing it right ?

#include <Ethernet.h>
#include <SD.h>  

void setup() {
   Serial.begin(9600);
  
  
  pinMode(10, OUTPUT);
 digitalWrite(10, HIGH);
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  
 Serial.print("Initializing SD card...");

   
 }
  
 void loop(void) {
    Serial.println(SD.begin(4));
  
}

thanks ....

This should be the correct way to check.

#include <SD.h>  

void setup() {
   Serial.begin(9600);
  
  // disable w5100 SPI
  pinMode(10, OUTPUT);
 digitalWrite(10, HIGH);
  
  Serial.print("Initializing SD card...");
  if(SD.begin(4) == 0) Serial.println("failed");
  else Serial.println("ok");
}
  
void loop() {
}

it didnt worked :confused: are you sure there is no wiring needed ,
note that the ethernet part works perfectly . please check attached photos ......

I am certain no wiring is needed. I use exactly that setup you are using. Did you power down the Mega before trying the new code? Sometimes the SD card doesn't reset right without a power cycle.

edit: Well, not exactly the same. Your ethernet shield is not the same as mine. The IC layout on your board is different. Mine looks like this.

I tried a Google search for that type shield, and could not find an image of that shield without a solder bridge on the w5100 IC. Here is a pic on this forum with several solder bridges.
http://forum.arduino.cc/index.php?topic=178633.0

Success!

Hello everyone.
I was experiencing an SD failure for a LOooooNG time.
I am utilizing [Discontinued] SainSmart Mega2560 R3+Ethernet Shield Kit for Arduino A – SainSmart.com

The shield worked on Sainsmart UNO but i ran out of memory and had to switch to Mega and that is where i hit the trouble.

Long story short.
It seems like one of the Ethernet shields I have is not working. And this was the shield i was trying to use... of course. I swapped libraries, Mega boards, code, pins... to no avail, i even tried jumpers to 50 51 52 pins. The last thing i decided to change was the Ethernet shield! And that new shield WORKED!

I still do not know why the heck it did work with UNO and failed on all my Megas which are all from Sainsmart.

I do not want to blame Sainsmart because all their boards worked fine so far. And i got a few.
Maybe i zapped it somehow.
There is something going on with that Ethernet Shield that i do not understand.

I would like to thank SurferTim for clear and concise comment #4 that works.
I would also like to warn everyone that you may have a faulty Ethernet shield.
And if you have my IQ it will kill a week worth of your evenings!

Have a great Arduino time and thank you all!