0

I want to connect a W5500 ethernet Module and an SD card module to arduino Uno using SPI (with different CS pins of course). When connected separately they work fine but when connected in the same circuit the ethernet module doesn't get a correct ip address and always gets an address 255.255.255.255. To me the ethernet module is the likely culprit as the SD card module works without problem even when the ethernet module is also connected.

Tried using Ethernet & Ethernet2 libraries but no difference. Tried manually assigning the IP address but it won't work. I have even set the CS pins HIGH in the setup before calling SD.begin & Ethernet.begin() to avoid both components "listening" at the same time but it still doesn't work. How can I fix it?

Here is the sketch I am working with. Although it doesn't use SD module but as mentioned above if SD module is connected in the circuit, ethernet module doesn't get incorrect ip address

#include <SPI.h>
#include <Ethernet2.h>

//IPAddress ip(192, 168, 1, 22);


// MAC address just has to be unique. This should work
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetServer server(80);

void setup()
{

  pinMode(8, OUTPUT); // SD module CS
  digitalWrite(8, HIGH);
  pinMode(10, OUTPUT); // Ethernet module CS
  digitalWrite(10, HIGH);
  delay(1);

  Serial.begin(9600);
  Ethernet.begin(mac);
  server.begin();
  Serial.print("Server started on: ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)
  {
    while (client.connected())
    {
      // send a standard http response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();

      // send the body
      client.println("<html><body>");
      client.println("<h1>Arduino Server</h1>");
      client.print("<p>A0=");
      client.print(analogRead(0));
      client.println("</p>");
      client.print("<p>millis=");
      client.print(millis());
      client.println("</p>");
      client.println("</body></html>");
      client.stop();
    }
    delay(1);
  }
}

Another sketch with reference to both SD and Ethernet Libraries

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

byte mac[] = { 0x00, 0xC3, 0xA2, 0xE6, 0x3D, 0x57 };
byte LED = 2;
char wServer[] = "media.wizards.com";
char wFile[] = "/images/magic/tcg/resources/rules/MagicCompRules_20140601.pdf";

EthernetClient myClient;

void setup() {
  //D53 on an Arduino Mega must be an output.
  pinMode(53, OUTPUT);

  pinMode(LED, OUTPUT);

  Serial.begin(9600);
  while (!Serial);

  Serial.print("Establishing network connection... ");

  if (Ethernet.begin(mac) == 0) {
    Serial.println("FAILED!");
    while (true) {
      digitalWrite(LED, HIGH);
      delay(500);
      digitalWrite(LED, LOW);
      delay(500);
    }
  } 
  else {
    Serial.println("OK!");
  }
}

void loop() {

  Serial.print("Connecting to ");
  Serial.print(wServer);
  Serial.print("... ");

  if (myClient.connect(wServer, 80) == 1) {
    Serial.println("OK!");

    myClient.print("GET ");
    myClient.print(wFile);
    myClient.println(" HTTP/1.0");
    myClient.print("Host: ");
    myClient.println(wServer);
    myClient.println();

    if (SD.begin(8)) {
      if (SD.exists("MAGIC.PDF"))
        SD.remove("MAGIC.PDF");

      Serial.print("Saving response... ");
      digitalWrite(LED, HIGH);

      File dd = SD.open("MAGIC.PDF", FILE_WRITE);

      char lc;
      while (myClient.connected()) {
        if (myClient.available()) {
          char nc = myClient.read();
          if ((lc == 10) && (nc == 13)) {
            while (myClient.available() == 0);
            myClient.read();
            break;
          }
          else
            lc = nc;
        }
      }

      while (myClient.connected()) {
        if (myClient.available() > 0) {
          dd.write(myClient.read());
        }
      }

      dd.close();
      delay(500);
      Serial.println("OK");
      digitalWrite(LED, LOW);
    }
    else
      Serial.println("No SD card detected!");

    myClient.stop();
  } 
  else
    Serial.println("FAILED!");

  delay(3600000);
  asm volatile(" jmp 0");
  • 1
    Where did you tell the SD library and the Ethernet library which CS they should use? – theSealion May 7 at 6:08
  • In additional to @theSealion very valid question. This article should be able to help you to troubleshoot and narrow down the cause. – hcheung May 7 at 8:39
  • ethernet2.h is deprecated. – hcheung May 7 at 8:44
  • ethernet2 by default using pin 10 as cs (github.com/adafruit/Ethernet2/blob/master/src/utility/…), however, your code has no reference to SD card library or initialisation of SD card. – hcheung May 7 at 9:03
  • I have experimented with a few different sketches but it's not working. I have uploaded another sketch which I tried but it doesn't work. – Zaffresky May 7 at 18:36
3

I had the same problem with my SD card module.

I don't have too much knowledge about all details, but it is related that most SD card modules (the simple ones) do not have tri-state functionality, meaning they cannot cope with multiple SPI devices on the same bus.

Sadly, the Arduino has only one SPI connection, but there are solutions:

enter image description here

| improve this answer | |
  • 1
    I think you are referring to 74AHC125 to be used a MISO tri-state buffer. – hcheung May 7 at 8:36
  • 1
    The problem with SD cards is that the SD card doesn't strictly adhere to the SPI protocol standards. The CS pin doesn't cause the SD card to be isolated from the bus as it should, and there are specific operations that you perform on a SD card through the SPI bus while the CS pin is held high. – Majenko May 7 at 9:56
  • 1
    Also the order of initialization is critical. You must start the SD card before any other SPI device to ensure that every device on the bus is SPI, since the SD card defaults to SDIO mode and that can interfere with the rest of the bus. – Majenko May 7 at 9:59
  • 1
    It certainly helps matters, yes. When the SD is isolated from the bus then no matter what it does it can't interfere with the rest of the bus. – Majenko May 7 at 10:02
  • 1
    Personally I never share the SPI bus with anything else when using an SD card (but then I use PIC32 with multiple SPI busses). If needed you can always bit-bang SPI for other devices that don't need high speed. – Majenko May 7 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.