Problem when connecting rc522 to arduino mega with etihernet sheild.

I am working on a project that I hope someone can help me with. I also need to say that i am new with this.

The full project is a time watch were i have one rtc 3231, arduino mega, eithernet sheild with sd card (that works with mega). And one rfid rc522.

but in this case i have reduced the project to:

setup: the same code as i will have on the full project.

void loop: the project is reduced to read the rfid tag and write the tag id to an sd card
(i have skipped the webserver for now).

The RC522 don’t read the tag.

Different things that i have tested. (I always have same code)

When i connect my project to an arduino uno it works fine. Problem is the memory when everything is included (full project)

Connect it to an arduino mega, i have connect the rc522 to the spi ports on the sheild.(13,11,12) .
Problem: I think the setup works fine here but when we are in the loop the RC522 dont read the tag.

Connect it to an arduino mega but used the spi ports on the mega instead (for rc522) 50,51,52. (but i think this is not the way to do it right?, because i can not have to SPI ports?)
Problem: in the setup i got problem with initialization the sd card.

(The SS port for the rtc is defined to 7 and rst to 6 )

Do some one have any idea what can be wrong?

here is my code

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <Wire.h> //include Wire.h library
#include "RTClib.h" //include Adafruit RTC library
#include "MFRC522.h"

#define SS_PIN 7
#define RST_PIN 6

String readTag = ""; 
String datastring = "";
int readCard[4];

MFRC522 mfrc522(SS_PIN, RST_PIN);  // define RFID reader class
RTC_DS3231 rtc; //Make a RTC DS3231 object

MFRC522::MIFARE_Key key;

// 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, 50);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

File myFile;

void setup() {
      Serial.begin(9600);    // Open serial communications and wait for port to open:
      Wire.begin();  // start i2c library; if not, RTC will not work
       //>>>>>>>>>>>>>>>>>>>>>>RTC SETUP>>>>>>>>>>>>>>>>>>
  //Print the message if RTC is not available
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

  //Setup of time if RTC lost power or time is not set
  if (rtc.lostPower()) {
    //Sets the code compilation time to RTC DS3231
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // >>>>>>start the Ethernet connection and the server:>>>>>
  Ethernet.begin(mac, ip);
  Serial.print("server is at ");


  //>>>>>>>>>>>>>>><reading sd card>>>>>>>>>>>>>>

     Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  Serial.println("initialization done.");


 Serial.println("I am waiting for card..."); // rfid

void loop() {

  int RFIDread = getID();
   if(RFIDread==1){ // if RFID read was succesful



int getID(){

      // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
    return 0;

    // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  // I think we should assume every PICC as they have 4 byte UID
  // Until we support 7 byte PICCs
 // Serial.println(F("Scanned PICC's UID:"));
  readTag = "";
  for (int i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
   //Serial.print(readCard[i], HEX);
    readTag +=String(readCard[i], HEX);
//  Serial.println("");

    DateTime now =;

String dataString = "";
dataString += String(now.year());
dataString += String("-");
dataString += String(now.month());
dataString += String("-");
dataString += String(;
dataString += String(" ");
dataString += String(now.hour());
dataString += String(".");
dataString += String(now.minute());
dataString += String(".");
dataString += String(now.second());
dataString += String(";");
dataString += String(readTag);
dataString += String("
Serial.println (dataString);

myFile ="datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (myFile) {
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");

  mfrc522.PICC_HaltA(); // Stop reading

  return 1;

A Mega does not have SPI on pins 11, 12 and 13. See the schematic; so you need to use 50, 51 and 52.

Hi. Thanx for your replay. But the sheild is connected to 11,12,13 Right?

Abbemar: Hi. Thanx for your replay. But the sheild is connected to 11,12,13 Right?

It is "connected" to those pins (and others) but it does not USE those pins.

The ethernet shield has a ICSP connector that connects to the ICSP port on the UNO or Mega. Some of the pins in the ICSP port are the SPI pins, different pins depending on the Arduino model.

Thank you.

Now it works. Pin 50,51,52 are now used.. I also had power problem. the usb from comptuer did't enough ampere to have all thing upp and running. But with external adapter it works.

Thank you both for the help.