0

I've been working to a project that uses arduino for RFID but when connected to ethernet shield, the RFID seems to not read any card tapped to sensor but the LCD seems to do fine. Here's the code I use from this page: http://www.instructables.com/id/RFID-CARD-READER-WITH-ARDUINORFID-RC522-and-LCD-16/

#include <EEPROM.h> //Library To read and write PICC's UIDs from/to EEPROM
#include <SPI.h> //Library  RC522 Module uses SPI protocol
#include <MFRC522.h> //Library  RC522 Module
#include <LiquidCrystal.h> //Library  for LCD Display
#include <Ethernet.h>

EthernetServer server(80);
boolean match = false; // initialize card match to false
boolean programMode = false; // initialize programming mode to false
int successRead; // Variable integer to keep if we have Successful Read from Reader
byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Initializing LCD PINS as (RS, EN, D4, D5, D6, D7)

void setup() {
  Serial.begin(9600); // Initialize serial communications with PC
  lcd.begin(16, 2); //Initializing LCD 16x2
  pinMode(8, OUTPUT); //LED and Buzzer PIN OUT
  SPI.begin(); // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
  if (EEPROM.read(1) != 1) {
    // Look EEPROM if Master Card defined, EEPROM address 1 holds if defined
    Serial.println("No Admin Card Defined");
    //When no Master Card in Your EEROM (Serial Display)
    Serial.println("Scan A card to Define as Admin Card");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.println("SET ADMIN CARD   ");
    //When no Master Card in Your EEROM (LCD Display)
    lcd.setCursor(0, 1);
    lcd.println("SCAN A CARD.....");
    //Scan any RFID CARD to set Your Master Card in Your EEROM (LCD Display)
    delay(1500);
    do {
      successRead = getID();
      // sets successRead to 1 when we get read from reader otherwise 0
    }
    while (!successRead);
    //the program will not go further while you not get a successful read
    for (int j = 0; j < 4; j++) {
      // Loop 4 times
      EEPROM.write(2 + j, readCard[j]);
      // Write scanned PICC's UID to EEPROM, start from address 3
    }
    EEPROM.write(1, 1); //Write to EEPROM we defined Master Card.
    Serial.println("Admin Card Defined");
  }
  Serial.println("Administrator Card's UID");
  for (int i = 0; i < 4; i++) { // Read Master Card's UID from EEPROM
    masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
    Serial.print(masterCard[i], HEX); //Master Card only view in serial
    Serial.println("Waiting PICCs to bo scanned :)");
  }
 //WAITING TO SCAN THE RFID CARDS:
  Serial.println("");
  Serial.println("Waiting cards to bo scanned :)");
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.println("WAITING     ");
  lcd.setCursor(3, 1);
  lcd.println("FOR CARD...  ");
  delay(1500);
}

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("Scanning card's UID....");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SCANNING");
  lcd.setCursor(0, 1);
  lcd.print("Card's UID.....");
  delay(2000);
  for (int i = 0; i < 4; i++) {
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}
boolean isMaster(byte test[]) {
  if (checkTwo(test, masterCard))
    return true;
  else
    return false;
}

boolean checkTwo (byte a[], byte b[]) {
  if (a[0] != NULL)
    // Make sure there is something in the array first
    match = true;
    // Assume they match at first
  for (int k = 0; k < 4; k++) {
    // Loop 4 times
    if (a[k] != b[k])
      // IF a != b then set match = false, one fails, all fail
      match = false;
  }
  if (match) {
    // Check to see if if match is still true
    return true;
  } else  {
    return false;
  }
}
boolean findID(byte find[]) {
  int count = EEPROM.read(0);
  // Read the first Byte of EEPROM that
  for (int i = 1; i <= count; i++) {
    // Loop once for each EEPROM entry
    readID(i);
    // Read an ID from EEPROM, it is stored in storedCard[4]
    if (checkTwo(find, storedCard)) {
      // Check to see if the storedCard read from EEPROM
      return true;
      break; // Stop looking we found it
    } else {
      // If not, return false
    }
  }
  return false;
}
void readID(int number) {
  int start = (number * 4) + 2;
  // Figure out starting position
  for (int i = 0; i < 4; i++) {
    // Loop 4 times to get the 4 Bytes
    storedCard[i] = EEPROM.read(start + i);
    // Assign values read from EEPROM to array
  }
}
void deleteID(byte a[]) {
  if (!findID(a)) {
    // Before we delete from the EEPROM, check to see if we have this card!
    failedWrite(); // If not
  } else {
    int num = EEPROM.read(0);
    // Get the numer of used spaces, position 0 stores the number of ID cards
    int slot;
    // Figure out the slot number of the card
    int start;
    // = (num * 4) + 6; // Figure out where the next slot starts
    int looping;
    // The number of times the loop repeats
    int j;
    int count = EEPROM.read(0);
    // Read the first Byte of EEPROM that stores number of cards
    slot = findIDSLOT(a);
    //Figure out the slot number of the card to delete
    start = (slot * 4) + 2;
    looping = ((num - slot) * 4);
    num--; // Decrement the counter by one
    EEPROM.write(0, num); // Write the new count to the counter
    for (j = 0; j < looping; j++) {
      // Loop the card shift times
      EEPROM.write(start + j, EEPROM.read(start + 4 + j));
      // Shift the array values to 4 places earlier in the EEPROM
    }
    for (int k = 0; k < 4; k++) {
      //Shifting loop
      EEPROM.write(start + j + k, 0);
    }
    successDelete();
  }
}
//For Failed to add the card:
void failedWrite() {
  Serial.println("something wrong with Card");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SOMETHING WRONG");
  lcd.setCursor(0, 1);
  lcd.print("WITH CARD");
  delay(2000);
}

//For Sucessfully Deleted:
void successDelete() {
  Serial.println("Succesfully removed");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SUCCESFULLY");
  lcd.setCursor(0, 1);
  lcd.print("REMOVED CARD");
  delay(2000);
}

int findIDSLOT(byte find[]) {
  int count = EEPROM.read(0);
  // Read the first Byte of EEPROM that
  for (int i = 1; i <= count; i++) {
    // Loop once for each EEPROM entry
    readID(i);
    // Read an ID from EEPROM, it is stored in storedCard[4]
    if (checkTwo(find, storedCard)) {
      // Check to see if the storedCard read from EEPROM
      // is the same as the find[] ID card passed
      return i;
      // The slot number of the card
      break;
      // Stop looking we found it
    }
  }
}
//For Sucessfully Added:
void successWrite() {
  Serial.println("Succesfully added");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SUCCESFULLY");
  lcd.setCursor(0, 1);
  lcd.print("ADDED");
  delay(2000);
}

//For Adding card to EEPROM:
void writeID(byte a[]) {
  if (!findID(a)) {
    // Before we write to the EEPROM, check to see if we have seen this card before!
    int num = EEPROM.read(0);
    // Get the numer of used spaces, position 0 stores the number of ID cards
    int start = (num * 4) + 6;
    // Figure out where the next slot starts
    num++;
    // Increment the counter by one
    EEPROM.write(0, num);
    // Write the new count to the counter
    for (int j = 0; j < 4; j++) {
      // Loop 4 times
      EEPROM.write(start + j, a[j]);
      // Write the array values to EEPROM in the right position
    }
    successWrite();
  } else {
    failedWrite();
  }
}
void loop() {
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("TAP YOUR");
  lcd.setCursor(4, 1);
  lcd.print("CARD HERE");
  do {
    successRead = getID(); // sets successRead to 1 when we get read from     reader otherwise 0
    if (programMode) {
      // Program Mode cycles through RGB waiting to read a new card
    } else {
    }
  }
  while (!successRead);
  //the program will not go further while you not get a successful read
  if (programMode) {
    if (isMaster(readCard)) {
      //If master card scanned again exit program mode
      Serial.println("This is ADMIN Card");
      Serial.println("Exiting ADMIN Mode");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("EXITING FROM");
      lcd.setCursor(0, 1);
      lcd.print("ADMINISTRATOR MODE");
      delay(2000);
      programMode = false;
      return;
    } else {
      if (findID(readCard)) {
        //If scanned card is known delete it
        Serial.println("I know this CARD, so removing");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("AVAILABLE!");
        lcd.setCursor(0, 1);
        lcd.print("SO DELETING.....");
        delay(5000);
        deleteID(readCard);
        Serial.println("-----------------------------");
      } else {
        // If scanned card is not known add it
        Serial.println("Adding this card...");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Card no:");
        lcd.setCursor(0, 1);
        lcd.print(readCard[0], HEX);
        lcd.print(readCard[1], HEX);
        lcd.print(readCard[2], HEX);
        lcd.print(readCard[3], HEX);
        lcd.print(readCard[4], HEX);
        delay(4000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("NOT AVAILABLE");
        lcd.setCursor(0, 1);
        lcd.print("SO ADDING...");
        delay(5000);
        writeID(readCard);
        Serial.println("-----------------------------");
      }
    }
  } else {
    if (isMaster(readCard)) {
      // If scanned card's ID matches Master Card's ID enter program mode
      programMode = true;
      Serial.println("Welcome to Administrator Mode");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("WELCOME TO");
      lcd.setCursor(0, 1);
      lcd.print("ADMIN MODE");
      delay(3000);
      int count = EEPROM.read(0);
      // Read the first Byte of EEPROM that
      Serial.print("I have ");
      // stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(" record(s) on EEPROM");
      Serial.println("");
      Serial.println("Scan a card to ADD or REMOVE");
      Serial.println("-----------------------------");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("SCAN CARD TO");
      lcd.setCursor(0, 1);
      lcd.print("ADD OR REMOVE...");
      delay(2500);
    } else {
      if (findID(readCard)) {
        // If not, see if the card is in the EEPROM
        Serial.println("Acces Granted");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("ACCESS GRANTED");
        lcd.setCursor(0, 1);
        lcd.print("YOU MAY ENTER NOW");
        digitalWrite(8, HIGH);
        delay(1500);
        digitalWrite(8, LOW);
        lcd.clear();
      } else {
        // If not, show that the ID was not valid
        Serial.println("Access Denied");
        for (int abcd = 0; abcd < 3; abcd++) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("     SORRY");
          lcd.setCursor(0, 1);
          lcd.print("  ACCESS DENIED");
          digitalWrite(8, HIGH);
          delay(1000);
          digitalWrite(8, LOW);
          lcd.clear();
          lcd.print("   YOU'RE NOT  ");
          lcd.setCursor(0, 1);
          lcd.print("   AUTHORIZED   ");
          delay(1000);
        }
        lcd.clear();
      }
    }
  }
}

Any help would be appreciated!

  • Well, the Ethernet shield uses SPI as well, and pins 10-13. Seems that would be a problem... – KC Tucker Sep 10 '17 at 4:03
  • Thank you kind Sir, I'm fairly new to coding arduino and I just follow instructions so I don't know what to add in the code to add SPI device(Ethernet shield). – Ryan Baronia Sep 10 '17 at 4:15
  • What @KCTucker is saying is that you can not use every optional feature with every other optional feature. – st2000 Sep 10 '17 at 14:41
2

I recently made a similar project with a RFID reader and an SD card shield, and I had the same problem.

The explanation why it doesn't work is that some shields that use SPI communication don't really care about your slave select pin being pulled low, and interfere when Arduino is talking to another SPI device via the MISO pin.

The only way I got it to work is by using a tri-state buffer on the MISO pins of my SPI devices. You can find plenty of info about it on the internet, as well as how to use them in your project.

The idea is simple, it works kind of like a transistor. When you pull the enable pin of the buffer to ground using SS pin, you allow the MISO pin of that device to be connected to the corresponding Arduino input. Otherwise, the MISO pin will be on a state of high impedance and would not interfere on the communication anymore.

Good luck with your project.

1

It may not be possible to use every optional Arduino feature with every other optional Arduino feature. Some features use the same Arduino resources. If there is actually a hardware contention problem, you need to dive deep into the software drivers and hardware to figure out if there is a possible work around. But this is hardly the type of effort new developers would take on. It might be easier for a new developer to use 2 different Arduino's to host the features desired and set up a communication link between them.

  • If you are "following instructions", please post a URL to the project that is using all the hardware you mentioned. – st2000 Sep 10 '17 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.