7

I want to connect Ethernet board (W5100 HR911105A) to Arduino Leonardo with SPI and I also want to connect SD reader. When I test these boards separately everything working. But when I connect them on the same board the program fails at Ethernet.begin();

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

#define SS_SD_CARD  4
#define SS_ETHERNET 10

const char CONFIG_FILE[] = "system.cfg";

byte C_MAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress S_IP(192,168,0,19);
IPAddress C_IP(192, 168, 0, 177);
const uint16_t  S_PORT = 8090;

EthernetClient client;

void setup() {
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
    pinMode(SS_SD_CARD, OUTPUT);
    pinMode(SS_ETHERNET, OUTPUT);

    digitalWrite(SS_SD_CARD, HIGH);
    digitalWrite(SS_ETHERNET, HIGH);

    digitalWrite(SS_SD_CARD, LOW);
    SD.begin(SS_SD_CARD);
    digitalWrite(SS_SD_CARD, HIGH);

    Serial.println("Setup ethernet");
    digitalWrite(SS_ETHERNET, LOW);
    if (Ethernet.begin(C_MAC) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      Ethernet.begin(C_MAC, C_IP);
    }
    delay(100);
    Serial.println("connectin...");
    bool result = client.connect(S_IP, S_PORT);
    Serial.println("connected");
    digitalWrite(SS_ETHERNET, HIGH);
}

void loop() {
  Serial.println("loop...");
  delay(1000);
}

I tried on Leonardo and Mega 2560.

eszik.k
  • 121
  • 5

3 Answers3

3

Some card reaaders (as well as display boards with built-in sd socket) use 10-kOhm resistors in series to the CS, DIN and CLK lines, to perform a cheap 5V to 3.3V level translation. This is really not the best solution, especially in terms of speed (we verified that typically this limits the maximum SPI speed to about 1 MHz).

Other cards readers (such as this one http://modtronix.com/mod-sdcard-l5.html), just use 10 k-Ohm resistors as pull-up to 3.3V. Therefore you're connecting all the SD card lines to the Leonardo board, which is 5V level. This will cause serious issues to the SD card or to the leonardo board. If your card reader falls in this second case, I suggest you to insert a resistor divider (1k + 1.5k) in series to each input lines of the SD card: DIN, CLK, CS. In the following schematics I just show for the CLK line, of course you have to repeat this three times. A much better idea would be a level translator IC.

schematic

simulate this circuit – Schematic created using CircuitLab

next-hack
  • 411
  • 3
  • 10
2

If you have a similar SD card reader like below (or same but with micro SD card), than I had exactly the same problem.

It works as single device on SPI, but not together with another SPI device.

You can either use a SD recorder proto board, or use a microcontroller that has multiple SPIs.

SD breakout

An example of the SD recorder I used and works with another SPI device on one SPI port.

Cons: bigger, more expensive Pro: has a RTC and proto space.

SD Recorder

Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58
1

i suspect the reason is due to how the inputs/outputs are designed, the W5100 appears to be a 3v3 board with 5V compatible inputs, as does the link you posted to the SD reader, however depending on how these inputs/outputs are designed can mean they won't "play nice" with other boards on the same bus even though SPI is designed to do that.

depending on where the problem lies there are a few potential solutions, if (as i suspect) the problem is the output from the devices back to the arduino (ie you can write to them but not read from them) which you could confirm by connecting all lines in parallel, but only connecting one of the serial lines back to the arduino and check that it works, then swap the serial to the other device and check that. assuming that both devices work in this configuration you only need to fix the return signal. the simplest way of doing this would be to use two tri state buffers.

so the return signal is only connected to the arduino when that devices select line is active. and high impedance (disconnected) otherwise, programatically disconnecting one device from the bus.

James Kent
  • 274
  • 2
  • 9