Go Down

Topic: Connect ethernet shield and SD card module? (Read 1 time) previous topic - next topic

evil_dan2wik

I bought a new experimental DK electronics ethernet shield because it had a place to solder components on. I then bought a separate SD card. Later i realised that i would need to use the SD card with the Ethernet Shield but they conflict on 11, 12 and 13. Is there a way to connect them both at the same time?

zoomkat

Test code for a w5100 shield with SD card.

Code: [Select]


//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");
         
          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");
          if (myFile) {
            //Serial.println("test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              client.write(myFile.read());
            }
            // close the file:
            myFile.close();

          }
            delay(1);
            //stopping client
            client.stop();
            readString="";
          //}
        }
      }
    }
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

evil_dan2wik


Test code for a w5100 shield with SD card.

Code: [Select]


//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");
         
          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");
          if (myFile) {
            //Serial.println("test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              client.write(myFile.read());
            }
            // close the file:
            myFile.close();

          }
            delay(1);
            //stopping client
            client.stop();
            readString="";
          //}
        }
      }
    }
  }
}




No, i know how to use it but the ethernet shield and the SD card are separate.

zoomkat

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)


SurferTim

#5
Jan 12, 2013, 12:17 pm Last Edit: Jan 12, 2013, 12:29 pm by SurferTim Reason: 1
Have you tried to get them to work separately?

edit: They are going to share three lines on the SPI bus.
Line    Uno    Mega
MOSI   D11    D51
MISO   D12    D50
SCK     D13    D52

Leonardo SPI is not on any digital pins. They are on the ICSP connector only.

Your ethernet shield appears to use the SPI lines on the ICSP connector if it has a 6 pin connector connecting to the ICSP pins on the Arduino.

evil_dan2wik


Have you tried to get them to work separately?

edit: They are going to share three lines on the SPI bus.
Line    Uno    Mega
MOSI   D11    D51
MISO   D12    D50
SCK     D13    D52

Leonardo SPI is not on any digital pins. They are on the ICSP connector only.

Your ethernet shield appears to use the SPI lines on the ICSP connector if it has a 6 pin connector connecting to the ICSP pins on the Arduino.



I can get them to work separately. Idk how to wire it up though.

SurferTim

If you got that far, you are almost there. What digital pin are you using for the slave select on the SD module? Digital 10 is the SS for the w5100.


evil_dan2wik

Im using 4 for the CS on the SD card. the ethernet shield uses 2, 10, 11, 12, 13.

SurferTim

Are you sure zoomkat's code above did not work? Those are the correct pins.

The w5100 uses pins D2, D10, D11, D12, and D13.
The SD uses D4, D11, D12, and D13.

Looks good to me.

evil_dan2wik

Im just about to use ribbon cable to connect them my nano board for testing.

SurferTim

Let me know how it does. If they work separately, and all works as normal, you should be ok with zoomkat's code above.  :)

evil_dan2wik

Its still not working. I re-checked my wiring and its all connected with nothing crossed.
With zoomkat's example, the SD card fails and then it prints "Ready" and then there is nothing more printed. With the ethernet webserver example, i get server is at 0.0.0.0
Is there a program i can use to log the digital pins?

SurferTim

Quote
With the ethernet webserver example, i get server is at 0.0.0.0

Then you have a SPI fail between the Arduino and the w5100. I use this for my server code.
http://playground.arduino.cc/Code/WebServerST

evil_dan2wik


Go Up