Issue using 2 different shields

Hello i have an issue i am using a NFC shield v2.0 by seeedstudio and an ethernet shield v2. my program is designed to get data from the server to compare NFC cards with. this works fine because i first use the ethernet shield and then the NFC. But as soon as nfc.begin(); is called ethernet crapsout and the connection is disconected and can never be reconnected. even if i close the connection before i call the nfc.begin(); function. i need to reconnect because i want to log NFC id back to the server when somebody used his nfc card. i suspect this has something to do with spi, yet i cant find a way to restore the spi settings or disable the NFC.

This NFC shield ? http://www.seeedstudio.com/wiki/NFC_Shield_V2.0 This Ethernet Shield ? http://www.seeedstudio.com/wiki/Ethernet_Shield_V2.0 They both use pin 10 for chip-select.

I suggest to keep pin 10 for the chip-select for the W5200 on the Ethernet Shield, and use pin 9 for the NFC.

Yes the nfc shield is correct. This is the ethernet shield http://www.arduino.org/products/arduino-ethernet-shield-2.

i already use pin 9 for the nfc. therefore i am clueless and i dont know how to fix this

Check which pins each shield is using. Make sure there are no conflicts.

It may be the case that you have an unavoidable conflict, and you will have to try something completely different.

As far as I can tell, there is no "guarantee" that every possible shield will be able to cohabit with every other possible shield.

People have solved problems like this before, by resorting to measures like cutting off one of the pins on one of the shields.

Another possible problem, is that the two shields are using more power than the arduino can provide. See if you can determine what the current requirement of each shield is.

Sorry, I don't know what is going on. Could it be a power issue ? Do you power both with via the usb cable ?

You are here on the forum of Arduino.cc, and I think the Ethernet library is for W5100 and W5200. You use the Arduino.org board with the W5500 (which is of course a better chip). Does that mean that you use the Arduino.org IDE with the Arduino.org libraries ? Perhaps there is a bug in that library.

Another possibility is that a 5V MISO is connected to a 3.3V MISO. That is something that is hard to detect if it would cause trouble. So I'm hoping that it is a simple power issue.

While I was typing this, michinyon already mentioned the power

How do i test this?

Also i would like to know what the correct procedure is for connecting to the ethernet ( a server) then disconnect and later reconnect the same server but different page

I have checked both schematics and I'm not sure if everything can be combined, but I think it is oaky.

Which Arduino board do you use ? A normal Uno, Mega 2560, Leonardo or other board ?

If you apply 7.5 to 9V to the power jack, then the Arduino board and the two shield should have enough power. Those switching wall wart regulators with selectable voltages are very handy. You can measure the 5V pin with a multimeter, both shields use the 5V.

I am using a mega 2560 from arduino.org and measured the voltage on the 5v pin it is 4.55 volt. and the 3.3volt pin is 3.3 volt.

For some, 4.55V is enough. For me, that is not enough ! Use an external power supply.

Thats genious! Thanks! it now doesnt fail when i call nfc.begin();

now i notice that i have a bug in my code that makes the arduino unable to connect properly. What is the normal protocol for connecting? i will post some code with the working attempt and the second attempt wich doesnt work

the working one:

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledr, OUTPUT);
  digitalWrite(ledr, HIGH);
  pinMode(ledo, OUTPUT);
  pinMode(ledg, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT); 
  pinMode(relai, OUTPUT);
  pinMode(knop, INPUT);
  pinMode(53, OUTPUT);
  Serial.begin(9600);
  //digitalWrite(10, LOW);



  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead() {
  Ethernet.begin(mac, ip);
  //connect to the server

  Serial.println("connecting...");
  counterPogingen++;
  //port 80 is typical of a www page

  while (notConnnected)
  {
    //Ethernet.begin(mac, ip);
    if (client.connect(server, 80)) {
      Serial.println("connected");
      counterPogingen = 0;
      client.print("GET ");
      client.println(location);
      Serial.println(location);
      client.println();
      notConnnected = false;

      //Connected - Read the page
      return readPage(); //go and read the output


    }
    else {
      delay(5000);
      Serial.println("connection failed");
      if (counterPogingen <= 100)
      {
        Serial.println("te veel pogingen");
        bufferId = true;
        counter = 30;
        return nfcBuffer();
      }
    }

  }
}
String readPage() {
  
  idsVanServerPOS = 0;
  memset( &idsVanServer, 0, 2048 );

  while (true) {

    if (client.available()) {
      char c = client.read();
      Serial.print(c);
      if (c == '[' ) {
        startRead = true;


      }
      else if (startRead) {

        if (c != ']') {
          idsVanServer[idsVanServerPOS] = c;
          idsVanServerPOS ++; // lees data per character
          Serial.print(idsVanServer[idsVanServerPOS]);
          dataOpgehaaldVanServer = true;
          if (c == '\n') {
            counter++;
          }
        }
        else {
          Serial.println("disconnecting.");
          //client.stop();
         //client.flush();
          digitalWrite(10, HIGH);
          Serial.print("connection state=");
          Serial.println(client.connected());

          if (!client.connected()) {
            Serial.println();
            Serial.println("disconnecting second attempt.");                
            //client.stop();
            digitalWrite(10, HIGH);
          Serial.print("connection state2=");
          Serial.println(client.connected());
          }
        }
        if (c == ']') {
          SD.begin(4);
          delay(timer);
          SD.remove("IDS.txt");// verwijder oude Id's
          Serial.println("Id naar SD");
          idsVanSD = SD.open("IDS.txt", FILE_WRITE);
          idsVanSD.seek(0); 
          idsVanSD.println(idsVanServer);
          delay(timer);
          idsVanSD.close();
          Serial.println("Id naar Serial");
          Serial.print(idsVanServer);
          bufferId = true;
          
          Serial.println("verbindingcheck1");
          Serial.println(client.connected());
          return idsVanServer;
        }


      }
    }

  }




}

and the not working one:

int returnId() {
 
 // SPI.setDataMode(SPI_MODE2);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);

            Serial.println("verbindingcheckreturnID");
          Serial.println(client.connected());
  
  Ethernet.begin(mac, ip);
  while(counterPogingen <=100)
  {
  Serial.println("connecting...");
  counterPogingen++;
  Serial.println(counterPogingen);
  client.connect({131, 180, 122, 151}, 80);

  while (notConnnected)
  {
    Serial.println("foutmelding");
    Serial.println(client.connected());
    //Ethernet.begin(mac, ip);
    if (client.connect({131, 180, 122, 151}, 80)) {
      Serial.println("connected");
      client.print("GET ");
      Serial.println("GET ");
      client.println("/nfc/rfid_registratie.aspx?id=49");
      Serial.println("/nfc/rfid_registratie.aspx?id=49");
      client.print("&rfid=");
      Serial.print("&rfid=");
      client.print(globalID);
      Serial.println(globalID);
      client.print("&time=");
      client.print(now());
      Serial.println(now());
      client.println();
      notConnnected = false;



    }
    else {
      delay(5000);
      Serial.println("connection failed");
      if (counterPogingen <= 100)
      {
        Serial.println("te veel pogingen");
        digitalWrite(ledr, HIGH);
        delay(300);
        digitalWrite(ledr, LOW);
        delay(300);
        digitalWrite(ledr, HIGH);
        delay(300);
        digitalWrite(ledr, LOW);
        delay(300);
        digitalWrite(ledr, HIGH);
        delay(300);
        digitalWrite(ledr, LOW);
        delay(300);
        digitalWrite(ledr, HIGH);
        delay(300);
        digitalWrite(ledr, LOW);
        return vergelijkID();
      }
    }
    }

  }
}

We need to see the whole sketch and tell us which Arduino IDE and which libraries you use. Pin 10 is the chip select for the W5500 Ethernet chip. If you make it low, it becomes active. You should never do that. The Ethernet.begin() is a library function, and that function activates the chip select and talks to the W5500.

I am using the arduino 1.6.3 and https://github.com/Seeed-Studio/PN532 and https://github.com/don/NDEF and for the ethernet shield i use the ethernet2 library. My sketch is a bit messy and a lot of it is not in english therefore impossible to understand for others that dont understand the dutch langauge.

I will convert it to english so it will be understandable for you all. I will post the sketch tomorrow. Thanks for the help!

The program needs to do the following. first of all it is going to download ID numbers from a server and save it to the SD card if no connection can be made it will use the file from the SD card as backup. Then it will look for nfc tags when one is present and a microswitch is pressed in ( card holder) it will read the tag and compare it with the array and when it is in the array it will grant acces and when it is not in the array it wil not grant acces. when acces is granted and someone pulls his card out. it needs to log who was using the card reader and this part does not work. It is because i cant reconnect. This function is called returnId. The code is to large to post on the forum. I will post it on github

Here is the code: https://github.com/BradleyBut/NFC-ethernet-issue

I see a number of things that can be better. Try to make the code look more "beautiful", that is: Seperate the initialization from the rest; Use indents and brackets in the same way; Add more comment; and so on. It will help to spot a bug.

You still make pin 10 and pin 9 low, that should be done in the library. Please call Ethernet.begin() and SD.begin() just once, that is normally only called from within the setup() function. Trying to connect is something that is done in the loop(), but you do that in setup() as well.

There is no limit by filling idsFromServer[3500], so if the incoming data is longer, there will be an overflow.

This is dangerous:

#define SCK  52
#define MOSI 51
#define MISO 50
#define SS 53
#define ETH_SS 10
#define NFC_SS 9

I prefer this:

const int pinETH_SS = 10;
const int pinNFC_SS = 9;

Initialize everything in setup(), and don't disturb the SPI bus by making devices active. Try to make it work step by step.

So i have to call Ethernet.begin nfc.begin and sd.begin in the setup()? and by doing this it will become more stable?. I understand that the overflow will make things go bad but i never heard about calling those functions must be done in the setup(). What is the reason behind this?

Sometimes an initializating functions can be called twice or more, and sometimes not. So why don’t you do as everything else, and initialize the objects, and hardware in setup().

Check every example here : Arduino - SD
Check also every example here : Arduino - Ethernet
Also every example with nfc.begin() should have that in setup().

I don’t know if it will become more stable, it is about using the .begin() functions as they are intended.

There is an exception. Some objects have also a .end(). Then the object and hardware are closed normally, and often a .begin() can be called after that.

When call the 3 begin functions for the nfc sd and ethernet in the setup the arduino crashes and keeps crashing even with the external powersupply.

Can you show the whole sketch ? Are you still making pin 9 or pin 10 low ? Perhaps there is a bug in a library. Perhaps the libraries need a lot ram memory and you run out of ram, since you have already large buffers.

it is on GitHub - BradleyBut/NFC-ethernet-issue
its called ‘‘2’’.

Please remove this (it is dangerous to redefine things that Arduino already has defined):

#define SCK  52
#define MOSI 51
#define MISO 50
#define SS 53

You call Ethernet.begin() many times. You call SD.begin() many times.