Go Down

Topic: How to use both Ethernet shield v.2 and RFID-RC522 connected in arduino UNO (Read 2331 times) previous topic - next topic


Need help on my project, I don't know how to interface both ethernet shield v2 and RC522 RF card reader using one arduino UNO.

My project function is; read RF card and send the RFID by LAN connection to the server.

Here's the code for ethernet shield v.2
Code: [Select]

#include <SPI.h>
#include <Ethernet2.h>
byte mac[] = {  0x90, 0xA2, 0xDA, 0x10, 0x8F, 0x12};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
  // Open serial communications and wait for port to open:
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
void loop() {

and here's the code for RC522
Code: [Select]

* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 7
* RST: Pin 9
* Buzzer Pin 5
* LED signa Pin 3
#include <SPI.h>
#include <RFID.h>

byte regUser[] = {0xE7, 0xDD, 0x55 };

#define SS_PIN       7              //RC522 SDA pin
#define RST_PIN      9              //RC522 RST pin
#define Relay1       2              //sets RFID enable pin to pin 2
#define BUZZ_PIN     5              //buzzer speaker output
#define SIG_LED      3               //LED signal during swipe
#define LED_1        8              //LED outpud lock enabled
#define SENSOR_STATUS 0              //input Sensor



// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;
int finalnumber;

void setup()
  // disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  // disable w5100 SPI
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  //Set relay to off
  pinMode(Relay1, OUTPUT);
  pinMode(LED_1, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(Relay1, LOW);
  digitalWrite(LED_1, LOW);
  pinMode(SIG_LED, OUTPUT);
  pinMode(BUZZ_PIN, OUTPUT);




void loop()

  if (rfid.isCard()) {
    if (rfid.readCardSerial()) {
      if (rfid.serNum[0] != serNum0
          && rfid.serNum[1] != serNum1
          && rfid.serNum[2] != serNum2
          && rfid.serNum[3] != serNum3
          && rfid.serNum[4] != serNum4
         ) {
        /* With a new cardnumber, show it.*/
        //Serial.println("Card found");
        serNum0 = rfid.serNum[0];
        serNum1 = rfid.serNum[1];
        serNum2 = rfid.serNum[2];
        serNum3 = rfid.serNum[3];
        serNum4 = rfid.serNum[4];
        finalnumber = serNum0 + serNum1 + serNum2 + serNum3 + serNum4;
        Serial.print(serNum0, HEX);
        Serial.print(serNum1, HEX);
        Serial.print(serNum2, HEX);
        Serial.print(serNum3, HEX);
        Serial.print(serNum4, HEX);
                serNum0 = 0;
                serNum1 = 0;
                serNum2 = 0;
                serNum3 = 0;
                serNum4 = 0;
        digitalWrite(SIG_LED, HIGH);
        digitalWrite(SIG_LED, LOW);
        digitalWrite(SIG_LED, HIGH);
        digitalWrite(SIG_LED, LOW);
        serNum0 = 0;
      } else {
        /* If we have the same ID, just write a dot. */

  } else{

//if Device is off line use this function
void compareID(unsigned char buff) {
  for (int j = 0; j < 3; j++) {
    if (regUser[j] == buff) {
      // Do something
      // Switch_Relay1(true);
  digitalWrite(SIG_LED, HIGH);
  digitalWrite(SIG_LED, LOW);
  digitalWrite(SIG_LED, HIGH);
  digitalWrite(SIG_LED, LOW);

//Buzzer code
void beep(unsigned char delayms) {
  analogWrite(BUZZ_PIN, 100);      // Almost any value can be used except 0 and 255
  // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  analogWrite(BUZZ_PIN, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms



I guess it's not complicated to add the contents of the setup() routine in your ethernet sketch to setup() routine in your second sketch.

Your problem might be that your ethernet sketch doesn't do what you expect it to. It just initializes the WizNet chip. So you should start to implement the part that connects to your server and sends the data.


Thank you for your suggestion, I manage to work both ethernet and RF reader module. Both module are using MISO and MOSI port og arduino UNO.

Now my project is working.  :)  :)


hi all, please radiant23 can you tell me how you fixed it
actually i have RC522 RFID and it works pretty well, but the problem happened when using the ethernet shield
first i don't know should i connect it to a router or to my pc using RJ45 , then which IP address i should use, I usually get it using ipconfig in the command prompt but it changed frequently, also if you can tell how you solve the conflict concerning the SPI between RFID and ethernet shield , I'll be too thankful because i'm about to submit my final year project and i need it urgently


i don't know should i connect it to a router or to my pc using RJ45
Arduino Ethernet Shield 2 uses W5500 Ethernet controller, which doesn't support auto-MDIX. So you would need a crossover cable to connect directly to your PC and I don't know if there would be other complications since I've never done that. You're better off to connect it to the router unless you have a good reason to do otherwise.

Go Up