Getting the RFID-RC522 to work! [SOLVED]

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

I have a arduino module with antena + mfrc 522 on pcb. Also I build with a microcontroller pic 18f67 an unit drive for mfrc522 in spi mode. But I don’t find an example of source code to activate antenna(tx1, tx2). Can help you with this(but not with arduino!)? Thanks in advance.

Not bumping this thread due to a problem on my end, just wanted to say that thanks to this thread and @peekay123’s library, I was able to get this ebay kit up and running on a Photon in about 4 minutes!

Any future Googlers looking for a quick start, here’s a summary of this thread:

Wiring:

-Function-         Photon Pin        MRFC522 Pin
 Reset             ANY (D2)	     RST
 SPI SS            ANY (A2)	     SDA
 SPI MOSI          A5		     MOSI
 SPI MISO          A4		     MISO
 SPI SCK           A3		     SCK

Code:
This will publish an event to the Particle cloud with the ID of whatever device you scan - kind of a RFID hello world, as it doesn’t really do much beyond let you catalog the cards and fobs you have, and ensure you’ve wired it up right. Make sure you add the MFRC522 library through the IDE, I’m not sure how it works if you just paste this with the include statement.

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>

#define SS_PIN      A2
#define RST_PIN     A1

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.

void setup() {
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    mfrc522.PCD_Init();
}

void loop() {
    // Look for new cards, if nothing found, quit
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    	return;
    
    }
    
    // Select one of the cards, if nothing found, quit
    if ( ! mfrc522.PICC_ReadCardSerial()) {
    	return;
    }
    
    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }
    Particle.publish("rfid_scan", cardID, 60, PRIVATE);
    mfrc522.PICC_HaltA();
}

Now for some RFID fun! Thanks everyone!

5 Likes

This is a great thread and this post really helped me make some progress with my RFID reader project. I’m able to read the tags provided with the RC522 and send the code to my API via a web hook. I’m having a slight issue though and for some reason the reader stops reading after a few minutes. Power cycling the photon resolves the issue and I added some visual confirmation around the Particle.publish by turning on an LED using D0 on the Photon followed by a 1 second delay and then turning it off. The reader is powered by 3 volts power and ground pulled to the left rail with power (D0) and ground for the LED pulled from the Photon. The pins are exactly as they are listed. Does anyone have any ideas on what might by causing this?

Here is my code:

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>

#define SS_PIN      A2
#define RST_PIN     A1

int led1 = D0; 




MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.

void setup() {
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    mfrc522.PCD_Init();
    pinMode(led1, OUTPUT);
    
}

void loop() {
     digitalWrite(led1, LOW);
    
    
    // Look for new cards, if nothing found, quit
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    	return;
    
    }
    
    // Select one of the cards, if nothing found, quit
    if ( ! mfrc522.PICC_ReadCardSerial()) {
    	return;
    }
    
    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }
    digitalWrite(led1, HIGH);
    Particle.publish("MenuItems", cardID, 60, PRIVATE);
    mfrc522.PICC_HaltA();
    delay(1000);
  
  
}

1 Like

The first thing I’d (always) do is to get rid of all that String manipulation stuff and use C strings (char[])

Instead of

    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }

I’d go for something like

  char cardID[32] = "";

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    snprintf(cardID, sizeof(cardID), "%s%02x", cardID, mfrc522.uid.uidByte[i]);
  }
// or
  char cardID[32] = "";

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    char hex[4];
    snprintf(hex, sizeof(hex), "%02x", mfrc522.uid.uidByte[i]);
    strncat(cardID, hex, sizeof(cardID));
  }
3 Likes

Just out of curiosity, why do you avoid the String class?

1 Like

@dougal, the String class uses dynamic memory allocation (on the heap). Using a lot of String operations can cause heap fragmentation and cause difficult to trace crashes. On small systems like Particle, there is no garbage collection like on larger systems.

3 Likes

Hello,

i am trying to use the RC522 and the Library with an Particle Electron but without success.

I don´t get any error messages or compiler Errors but the reader doesn’t work.

Is the pinout of the electron different to the photon pinout?

With the Library and an photon, everything is working fine.

Thanks for your help!!!

Regards

Stivi

How are you powering the Electron and the RFID reader?
The most obvious difference is that the Electron only supplies ~3.7V via Vin if not powered via USB but only the LiPo.

1 Like

Hi,

im powering the Reader and Electron via USB and the LED of the Reader is on…i don´t think its a power problem…

I am using the following pins:

A2 SDA
A3 SCA
A4 MISO
A5 MOSI

D3 RST

and 3V3 and GND

@Stivi, the LED is not a good indicator of power. Can you describe exactly the wiring for the reader. That is, exactly which pins of the Electron you are connecting to which pins of the reader.

What exact board are you using?
If you are using SPI, you should connect SS, SCK, MISO and MOSI
SDA and SCA are for I2C, which are on D0 and D1.

Hence I’m not too positive that this wiring should have worked on the Photon either :wink:
And if so, leading us down a wrong track isn’t very helpful :point_up:

1 Like

@Stivi, you may want to refer to an earlier post on how to wire and use the board:

Hi,

thanks for the ideas…

I am using the following pins:

A2 SDA
A3 SCA
A4 MISO
A5 MOSI

D3 RST

and 3V3 and GND

and the Code:

// This #include statement was automatically added by the Particle IDE.

STARTUP(cellular_credentials_set("internet.t-mobile","t-mobile" ,"tm" , NULL));

// This #include statement was automatically added by the Particle IDE.

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>

#define SS_PIN      A2
#define RST_PIN     D3

int led1 = D7;

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.

void setup() {
    Serial.begin(9600);	// Initialize serial communications with the PC
	Serial.println("Hello!");
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    Particle.keepAlive(1 * 45 );
	pinMode(led1, OUTPUT);
    mfrc522.PCD_Init();
}

void loop() {
    digitalWrite(led1, LOW);
    // Look for new cards, if nothing found, quit
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    	return;
    
    }
    
    // Select one of the cards, if nothing found, quit
    if ( ! mfrc522.PICC_ReadCardSerial()) {
    	return;
    }
    
    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }
    digitalWrite(led1, HIGH);
    Particle.publish("rfid_scan", cardID, 60, PRIVATE);
    Serial.println(cardID);
    mfrc522.PICC_HaltA();
    delay(1000);
}

The same code, without

STARTUP(cellular_credentials_set("internet.t-mobile","t-mobile" ,"tm" , NULL));

and

Particle.keepAlive(1 * 45 );

is working on a photon fine (with the same connections to the reader…)

Thanks…

This is the working Setup with Photon:

// This #include statement was automatically added by the Particle IDE.

//STARTUP(cellular_credentials_set("internet.t-mobile","t-mobile" ,"tm" , NULL));

// This #include statement was automatically added by the Particle IDE.

// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>

#define SS_PIN      A2
#define RST_PIN     D3

int led1 = D7;

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.

void setup() {
    Serial.begin(9600);	// Initialize serial communications with the PC
	Serial.println("Hello!");
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    //Particle.keepAlive(1 * 45 );
	pinMode(led1, OUTPUT);
    mfrc522.PCD_Init();
}

void loop() {
    digitalWrite(led1, LOW);
    // Look for new cards, if nothing found, quit
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
    	return;
    
    }
    
    // Select one of the cards, if nothing found, quit
    if ( ! mfrc522.PICC_ReadCardSerial()) {
    	return;
    }
    
    String cardID = "";
    
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        cardID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        cardID += String(mfrc522.uid.uidByte[i], HEX);
    }
    digitalWrite(led1, HIGH);
    Particle.publish("rfid_scan", cardID, 60, PRIVATE);
    Serial.println(cardID);
    mfrc522.PICC_HaltA();
    delay(1000);
}

Thanks…

@Stivi, you forgot to mention that you have your Electron plugged into an Asset Tracker Shield :wink:
This does change things quite a bit and also counts as leading us on :sunglasses:

Asset Tracker Shield already uses A2 as SPI SS for the accelerometer
https://github.com/spark/shields/blob/master/electron-shields/asset-tracker/pdfs/asset-tracker-sch.pdf

You need to select a free pin and assign that as new SS for the RFID reader in your code.

2 Likes