Arduino mega 2560 + ENC28J60 Ethernet Shield + Sd Card Shield

Hi everyone.
I begin my project for a little home automation.
I want to read a room temp , a motion sensor and to on/off the light.
I made the code for this and works great with this shield ENC28J60 Ethernet LAN / Network Module
ebay.com/itm/NEW-ENC28J60-Ethernet-LAN-Network-Module-For-51-AVR-STM32-LPC-/140717532321?pt=LH_DefaultDomain_0&hash=item20c36b24a1
I use the thiseldo-EtherShield-3793b4c library.

Now i connect the sd card shield
ebay.com/itm/SD-Card-Module-Slot-Socket-Reader-For-Arduino-ARM-MCU-NEW-/140717254516?pt=LH_DefaultDomain_0&hash=item20c366e774
on the arduino at the same pins like enc28j60 shield :
CS 53
MOSI 51
MISO 50
SCK 52

If you see in the image of sd card shield it has a double pins and one part i use to likn the sd card shield to the arduino pins and other part for link the enc shield to sd card shield.

I tested in this configuration the ethernet code and sd code but not in the same code , only one by one and they works separatly.
When i combined code , doesn't work.

So please tell me if i cand use the same pin 53 for many shields, or if i must do something in my code.
I want to load an image from sd card to use it like a backgroun on my web page and open a file where to write all operation who was made on my web page.
This is my code (a part of it for initialisation)

#include <etherShield.h>
#include <SdFat.h>
sdFat sd;
SdFile file;
#define error(s) sd.errorHalt_P(PSTR(s))
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; //Mac address
static uint8_t ip[4] = {192, 168, 1, 15}; //IP address
static uint16_t port = 80; //port 80
ETHER28J60 e;

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

//initializare ethernet
e.setup(mac, ip, port);

//initializare sd card
if (!sd.init(SPI_HALF_SPEED)) sd.initErrorHalt();
char name[] = "WRITE00.TXT";
if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) break;
if (!file.isOpen()) error ("file.create");
}

void loop
{
void loop()
{
char* params;

if (params = e.serviceRequest())
{
e.print("

TAO13 HOME SYSTEM PAGE

");
e.print("<body background="image2.gif">"); \where image2.gif found on my sd card
e.print("");
e.respond();
}
}

Many thanks everyone.

So please tell me if i cand use the same pin 53 for many shields

No, you can not. Each SPI device needs a separate chip/slave select pin. Usually, pin 4 is the SD card select pin.

Hi.
Thanks for your ansewer and help.
I'm a begginer and i don't know many thing about electronics.
So i understand if i have many items who works on spi i must use one pin for one item ?
For my case i must use pin 53 for ethernet and pin 4 for sd card? So i must modify the library for sd or something else?
Thanks again to all.

So i must modify the library for sd or something else?

That depends. I can't see the purpose of having more than one SD reader, for instance. Not that it really matters, because the SD card pin is an input to the class, not hardcoded in a library.

What SPI devices are you trying to talk to? Be specific, and we can help you figure out how to make them play together.

Hi and thanks.
Sorry for the delay.
So i don't have many sd module.I have one module for sd (like in the link) and one enc28j60 shield (without sd module).
I don't know what can i do to work both in the same time.
I use the same pin for sd module and enc28j60.
Many thanks again.

I don't know what can i do to work both in the same time.
I use the same pin for sd module and enc28j60.

Well, you can't do that. You need separate slave select pins to select the two different slaves.

Hello tao13,
Did you solved the problem?

Is there any working solution? I am using ENC28J60 and seprate SD card module on Uno.
I am using Pin 10 for Ethernet and 4 for SD.

My Initialization program is like below

void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");

myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

Ethernet.begin(mac, ip,gateway,gateway,mask);
server.begin();
delay(5000);
Serial.print("DONE Ethernet as well with IP: ");
Serial.println(Ethernet.localIP());

}

Does anyone has sample program for this?

Hi, I know this topic is Old but not solved. Can someone help how the mega, sd and enc28j60 Can work together? I tried to Connect the Cs separate and change the library. But no change, the Ethernet or SD work only on Pin 53.

Kingside:
Hi, I know this topic is Old but not solved. Can someone help how the mega, sd and enc28j60 Can work together? I tried to Connect the Cs separate and change the library. But no change, the Ethernet or SD work only on Pin 53.

SD CS pin can be D4, ethernet use pin 53, if you want to use different pin than 53, then specify it in the uipethernet library if you using that library....

Problem I have is sdcard reader on 1.8''TFT display refuses to read file on my webserver where separate microsd card reader works....

did anyone face with such strange attitude?

I am in a very similar situation, but I am using an Arduino nano clone. When I test the SD card module works , when I test the Enc28j60 OK, but when I try both at the same time SDcard works but Enc28j60 doesn't work. stay for ever in the ether.begin() funcion. The code is as follow:

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

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];

Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = 4;

void setup() {
Serial.begin(9600);
while (!Serial) {
}

Serial.print("\nInitializing SD card...");

if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed.");
return;
}

if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}

uint32_t volumesize;
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 (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);

Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac*, HEX);*

  • if (i < 5)*
  • Serial.print(':');*
    }
    _ /*pinMode(SS, OUTPUT);_
  • digitalWrite(SS, LOW ); *
  • pinMode(4, OUTPUT);*
  • digitalWrite(4, HIGH);*
    _ delay(1000);*/_
    if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0)
  • Serial.println(F("Failed to access Ethernet controller"));*
    Serial.println(F("OK, Setting up DHCP"));
    if (!ether.dhcpSetup())
  • Serial.println(F("DHCP failed"));*

ether.printIp("My IP is: ", ether.myip);
}
void loop(void) {
}
I am using D4 for my SD card and D10 for the ether module as Select device, MISO, MOSI and CLK on parallel to each module
Any idea of how to resolve this or what am I doing wrong?

Hi,

Try to do not use the SS pin on arduino, I had similar problem before.