Getting the RFID-RC522 to work! [SOLVED]

@jeremywmccarter, @ScruffR never sleeps! I’ll have to update the library at some point. There is another library in my repo, “MRFC522_RFID_Library” that uses hardware SPI. Did you consider using it as well?

1 Like

@peekay123 I saw that you had another library…is there a benefit to using that newer one instead? The fix @ScruffR suggested worked like a charm, but if you think I should use the newer library I will.

Hardware SPI is fine for me…I’ll be looking to add a touchscreen and some I2C expanders to the project as well, so if that throws up any red flags please let me know.

Thanks guys!
J

Another question @peekay123 and @ScruffR:
I have some RFID tags that use the protocol:
“ISO/IEC 15693-3, ISO/IEC 18000-3”

Is there a way to get the RC522 to read these tags?

Thanks guys-
J

@jeremywmccarter, take a look at the MFRC522 datasheet for those answers:

Thanks @peekay123 From RC522 reader data sheet:
“Supports ISO/IEC 14443 A/MIFARE and NTAG”

But I need to read ISO15693 tags. Is there a way to do that in code, or is it purely a hardware issue?

@jeremywmccarter, I believe the reader hardware has to support it.

#include <MFRC522.h>
//Get library from https://github.com/ljos/MFRC522
//Sketch: gudjonholm@gmail.com
#include <SPI.h>
#include <LCD.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd('0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE');
/*
Pins  SPI    UNO  Mega2560  Leonardo
1 (NSS) SAD (SS)   10     53        10
2       SCK        13     52        SCK1
3       MOSI       11     51        MOSI1
4       MISO       12     50        MISO1
5       IRQ        *      *         *
6       GND       GND     GND       GND
7       RST        5      ?         Reset
8      +3.3V (VCC) 3V3     3V3       3.3V
* Not needed
1 on ICPS header
*/
#define SAD 10
#define RST 5
MFRC522 nfc(SAD, RST);


//#define ledPinOpen    0
//#define ledPinClosed  1
Servo motorku;
int pos = 0;

int prPin = A0;
int buzzPin = A2;
int prState = LOW;
unsigned long TimerC;

void setup() {

  //Serial.begin(9600);
  motorku.attach(7);
  lcd.begin(16, 2);

  SPI.begin();
  //Serial.begin(115200);
  Serial.println("Looking for MFRC522.");
  nfc.begin();
  byte version = nfc.getFirmwareVersion();
  if (! version) {
   Serial.print("Didn't find MFRC522 board.");
    while (1); //halt
  }
  motorku.write(100);
  Serial.print("Found chip MFRC522 ");
  Serial.print("Firmware ver. 0x");
  Serial.print(version, HEX);
  Serial.println(".");

pinMode(buzzPin,OUTPUT);
pinMode(prPin,INPUT);
digitalWrite(buzzPin,LOW);

}//void setup()

#define AUTHORIZED_COUNT 3 /*If you want more Authorized of cards set the count here, and then add the serials below*/
byte Authorized[AUTHORIZED_COUNT][6] = {

  {0xA1, 0x95, 0x4E, 0x25, 0xFF, 0xFF, },
  {0x94, 0xAB, 0x95, 0xF4, 0xFF, 0xFF, }, /*f. example how to add more authorized cards*/
  {0x64, 0x22, 0xDE, 0x13, 0xFF, 0xFF, } 
};

void printSerial(byte *serial);
boolean isSame(byte *key, byte *serial);
boolean isAuthorized(byte *serial);

void loop() {

  prState=analogRead(prPin);
  if(prState>120)
   {TimerC=millis();
   digitalWrite(buzzPin, HIGH);}
   if (millis()-TimerC >=6000UL)
   {digitalWrite(buzzPin, LOW);}

//rfid mulai

              lcd.setCursor(0, 0);
              lcd.print("Scan your Card");
              lcd.setCursor(0, 1);
              lcd.print("Lock System        ");

              byte status;
              byte data[MAX_LEN];
              byte serial[5];
              //boolean Opening = false;
              status = nfc.requestTag(MF1_REQIDL, data);
              motorku. write(110);

            if (status == MI_OK) {
                status = nfc.antiCollision(data);
                memcpy(serial, data, 5);
                motorku.write(35);

                if (isAuthorized(serial))
                {
                  Serial.println("Authenticated");
                  //Opening = true;
                  lcd.setCursor(0, 1);
                  lcd.print("Unlock System      ");

                  //analogWrite(rlspin,HIGH);
                  delay(5000);

                }
                else
                {

                  lcd.setCursor(0, 1);
                  lcd.print("card wrong        ");
                  motorku.write(100);


                  printSerial(serial);
                  Serial.println("is NOT authenticated");
                  //Opening = false;
                }

                nfc.haltTag();
            
        }//if (status == MI_OK)

  
}//loop()
/*coba program ini
- apabila berhasil lanjutkan dengan menambahkan program lain
- apabila tidak berhasil kurangi menjadi dua program saja dulu
- selanjutnya lepas kabel dan berikan label pada kabel
- atur ulang posisi kabel dan sensor 

bag2
- atur ulang posisi rfid dan soundsensor
- copy sedikit-sedikit program rfid ke prog_1
- cari tahu bagian program rfid yang menghalangi 
  eksekusi prog_clapswitch
- cari label

*/

boolean isSame(byte *key, byte *serial)
{
  for (int i = 0; i < 4; i++) {
    if (key[i] != serial[i])
    {
      return false;
    }
  }

  return true;

}

boolean isAuthorized(byte *serial)
{
  for (int i = 0; i < AUTHORIZED_COUNT; i++)
  {
    if (isSame(serial, Authorized[i]))
      return true;
  }
  return false;

}


void printSerial(byte *serial)
{
  // Serial.print("Serial:");
  for (int i = 0; i < 4; i++) {
    Serial.print(serial[i], HEX);
    Serial.print(" ");
  }
}

this my program

and this massage after compile

Arduino: 1.6.11 (Windows 7), Papan:"Arduino/Genuino Uno"

C:\Users\Zulkifli Syamsul\Documents\Arduino\sketch_aug25b\sketch_aug25b.ino: In function 'void setup()':

sketch_aug25b:56: error: 'class MFRC522' has no member named 'begin'

   nfc.begin();

       ^

sketch_aug25b:57: error: 'class MFRC522' has no member named 'getFirmwareVersion'

   byte version = nfc.getFirmwareVersion();

                      ^

C:\Users\Zulkifli Syamsul\Documents\Arduino\sketch_aug25b\sketch_aug25b.ino: In function 'void loop()':

sketch_aug25b:117: error: 'MAX_LEN' was not declared in this scope

               byte data[MAX_LEN];

                         ^

sketch_aug25b:120: error: 'class MFRC522' has no member named 'requestTag'

               status = nfc.requestTag(MF1_REQIDL, data);

                            ^

sketch_aug25b:120: error: 'MF1_REQIDL' was not declared in this scope

               status = nfc.requestTag(MF1_REQIDL, data);

                                       ^

sketch_aug25b:120: error: 'data' was not declared in this scope

               status = nfc.requestTag(MF1_REQIDL, data);

                                                   ^

sketch_aug25b:123: error: 'MI_OK' was not declared in this scope

             if (status == MI_OK) {

                           ^

sketch_aug25b:124: error: 'class MFRC522' has no member named 'antiCollision'

                 status = nfc.antiCollision(data);

                              ^

sketch_aug25b:152: error: 'class MFRC522' has no member named 'haltTag'

                 nfc.haltTag();

                     ^

Multiple libraries were found for "SPI.h"
Digunakan : C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI
Tidak Digunakan: C:\Users\Zulkifli Syamsul\Documents\Arduino\libraries\SPI-master
Multiple libraries were found for "MFRC522.h"
Digunakan : C:\Users\Zulkifli Syamsul\Documents\Arduino\libraries\MFRC522
Tidak Digunakan: C:\Users\Zulkifli Syamsul\Documents\Arduino\libraries\arduino_861007
Tidak Digunakan: C:\Users\Zulkifli Syamsul\Documents\Arduino\libraries\MFRC522-master
exit status 1
'class MFRC522' has no member named 'begin'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

what my problem program??

First, try using the pin labels as printed on the board and not anonymous numbers.
That would also help the second point: You are using #define SAD 10 and int prPin = A0, but that’s the same pin (on Particle devices)! Do you really have two uses for the same pin or are you using non-Particle hardware?

Most importantly: What hardware are you using? This is the :particle: Particle community and these devices are not usually programmed with the Arduino IDE and SoftwareSerial is not supported either.

And being a bit more elaborate in your request for an answer would be nice too.

1 Like

Hello Particle gods, I have been following this thread for a bit and have some successful builds using my Photons and the RC522.
Full Disclosure: I'm using a Redbear Duo.
I'm writing to ask about Software SPI as I have a build that requires it and I can't seem to get the board to read with this configuration. I have a dozen different example going and none seems to be the right combo.

Currently, I'm using GitHub - pkourany/RC522_RFID: RC522_RFID library ported to Spark Core with added support for Software SPI with ScruffR's mod to fix the

has no member named 'BSRR'

The only modification to the example are the pin definitions

#define SS_PIN      D1 
#define RST_PIN     D5
#define MOSI        D3
#define MISO_PIN    D4
#define SCK_PIN     D2

I've also uncommented the Software SPI selection in the RFID.h file as well.

#define _USE_SOFT_SPI_

Are there any blatant errors or reasons I can't get it to read a card?
I'm also getting a Flash/Failed error in the web console but it compiles OK.

Thanks! :+1:

Can you also show your RFID constructor call and setup() - or even better the whole code?
Do you get any serial info about possible causes? (maybe add some debug statements)
Do you get anything of the reader?
Does the reader work properly with HW SPI?
Do you happen to have a Photon to try SoftSPI on that?

1 Like

Thank for the swiftness.
Nothing from serial, maybe I’ll try adding some debug and see what I get.
The reader works with Hardware SPI on another Photon, so I’ll try breadboarding one together to see if it’s a hardware issue with the DUO. Also, I’ll give the SoftSPI a shot on that too.

Here’s a code dump (most comments removed for space):

#include "RFID.h"
#define SS_PIN      D1      // Same pin used as hardware SPI (SS)
#define RST_PIN     D5
#define MOSI_PIN    D3      // hardware SPI: A5
#define MISO_PIN    D4      //     "     " : A4
#define SCK_PIN     D2      //     "     " : A3

#if defined(_USE_SOFT_SPI)
    RFID(int chipSelectPin, int NRSTPD, uint8_t mosiPin, uint8_t misoPin, uint8_t clockPin);    // Software SPI
#else
    RFID RC522(SS_PIN, RST_PIN);    // Hardware SPI
#endif

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

  /* Enable the SPI interface */
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.begin();
  RC522.init();
}

void loop()
{
  uint8_t i;
  if (RC522.isCard())
  {
    RC522.readCardSerial();
    Serial.println("Card detected:");
    for(i = 0; i <= 4; i++)
    {
      Serial.print(RC522.serNum[i],HEX);
      Serial.print(" ");
    }
Serial.println();
  }

  
}

Are you not missing something there?
Also you don't seem to actually use the pin definitions.

I also can't see the #define _USE_SOFT_SPI, if it's actually set I'd be surprised if you didn't get a build error with that.
The line you uncommented in RFID.h sports a trailing underscore #define _USE_SOFT_SPI_, but you check for _USE_SOFT_SPI.

Also this block would need wrapping in a conditional compile block, since it's probably counter productive when not using HW SPI

#if !defined(_USE_SOFT_SPI_)
  /* Enable the SPI interface */
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.begin();
#endif

I do have #define_USE_SOFT_SPI uncommented in RFID.h

Am I missing a few pin definitions there, how would I end up using the pin definitions correctly?
I was a little confused about nomenclature too, is the chipSelectPin the same as the SS or SDA pin?
Hardware SPI was so much easier!

RFID(int chipSelectPin, int NRSTPD, uint8_t mosiPin, uint8_t misoPin, uint8_t clockPin); // Software SPI

Is there a working complete example? These are coming from libraries examples so they should be working at some point, correct?

Have you had a close look :wink: - see my edit above :sunglasses:


If you compare the two lines constructing the RFID object (SoftSPI vs. HW SPI), you'll see that the SoftSPI does not contain an object name (RC522), hence - if that line actually would be active - you'd get an RC522 not defined error.
Also the HW SPI line does not contain data types for the individual parameters, while the SoftSPI does, which would also cause build errors.


I see! But I guess @peekay123 was focusing on getting the actual lib running and might not have tested SoftSPI with the sample.
I've just filed a PR about the BSSR issue, which he's already merged, but I'll also file another PR "correcting" the sample, although I haven't got the hardware to actually test it.

Oh, that was an edit! I definitely needed a close look :eyeglasses:

There we go, now I’m getting some build errors. That’s more like it :grin:
It’s giving me this error now:

expected unqualified-id before 'int'

Referring to line:

    RFID(int chipSelectPin, int NRSTPD, uint8_t mosiPin, uint8_t misoPin, uint8_t clockPin);    // Software SPI

Paul has just merged my new pull request, now you can go back to his repo and see what that sample should look like.

I hope it builds as is - since I’ve just dry-coded this without test-building :blush:

1 Like

Ok, cool! Thanks for facilitating that and helping me out. :+1:

1 Like

Thanks to @ScruffR & @peekay123 for always helping me out. Works perfectly.

2 Likes

@robertcedwards, @ScruffR is de man! :wink:

2 Likes