eelcor

[Energia Library] NFC card reading with Energia - MF RC 522

66 posts in this topic

Hi everybody,

 

>>> UPDATE - Made "proper" Energia Library out of it. Now with proper named methods/class members<<<

 

>>> UPDATE 2 - Added "keywords.txt" file for syntax highlighting, added comments to "Mfrc522.cpp" file explaining methods (thanks to Grant) - Added example to read cards contents of block 1 <<<

 

>>> UPDATE 3 - You need to apply the USCI SPI fix proposed by Spirilis (post #22) in the following thread: http://forum.43oh.com/topic/3237-energia-library-nordic-nrf24l01-library/page-2 , otherwise the card reader won't work! <<<<

 

I wanted to share my results with the MF RC522 RFID reader. I found an article from Grant Gibson (http://www.grantgibson.co.uk/blog/2012/04/how-to-get-started-with-the-mifare-mf522-an-and-arduino/#commentswhere he used a cheap reader from eBay to read Mifare cards.  (http://www.ebay.com/itm/Mifare-RC522-RFID-13-56Mhz-Module-SPI-Interface-with-a-IC-Card-/370690680474?pt=LH_DefaultDomain_0&hash=item564ee2e69a)

 

The command set and technical aspects of the MFRC522 chip can be found in the datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf

 

I have ported the example to Energia. Very simple by removing all Chinese comments and replacing uchar with unsigned chars. 

 

After these changes it works like a charm. 

 

Just connect the following lines:

MOSI LP -> Pin 3 of the module

MISO LP -> Pin 4 of the module

PIn 1.3 LP -> Pin 1 of the module (SS)

Pin 2.2 LP -> Pin 7 of the module (RST)

Pin 1.5 LP -> Pin 2 of the module (SCK)

GND LP -> Pin 6 of the module

VCC LP -> Pin 8 of the module

Pin 5 is not connected.

 

Included is the sketch (Mfrc522.zip) I used. Now onto opening doors and paying electronically!

 

Kind regards, 

 

Eelco

post-298-0-75634400-1372001881_thumb.jpg

post-298-0-50916500-1372001891_thumb.jpg

Share this post


Link to post
Share on other sites

Well,

 

It is actually not a library, but a simple program using the SPI library. It should be possible to change it into a library though.

 

Quite strange that my source code hasn't been included. I will make it into a zip file and include it in the top post.

 

Kind regards,

 

Eelco

Share this post


Link to post
Share on other sites

Okay guys,

 

Did some work this morning to transform the test code into a proper library. It should be more usable and generic. I haven't made a "keywords.txt". The methods should be self explanatory when consulting the datasheet. There is only one example that reads cards and recognizes specific cards. 

 

I works like a charm however and can be added to the Energia libraries.

 

I have replaced the original file with the library zip in the top post. I think the library also works with the stellaris launchpad.

 

Kind regards, Eelco

bluehash likes this

Share this post


Link to post
Share on other sites

Hi Eelcor

Not working for me. The sketch (Block1) compiles and uploads but it's not reading cards.

I've tested the reader on an Uno and it works fine using Grant's sketch.

I'm not getting anything at all in the serial display.

I've setup the Launchpad (G2553) and Duino as below:

RC522 MSP430 Arduino Colour
SAD   1.3   10      Purple
SCK   1.5   13      Blue
MOSI  1.7   11     Green
MISO  1.6   12     Yellow
IRQ     NA   NA
GND   GND GND Orange
RST    2.2   5       Red
3.3V   VCC 3.3V  Brown
 
Do I need To make any changes to the launchpad?
Any help appreciated

Share this post


Link to post
Share on other sites

Whoa,

 

I haven't been checking this library lately because it has been very busy at work. And now I have a news item...how cool!!! (Sorry kinda new to these things).

 

I am going to check your setup now and compare it to mine. Another detail that might work is to correct the SPI bug. @bluhash I forgot where the fix is, can you remember. I needed it to get the nrf24l01 working and this could be also the solution for D69.

 

Greetings, Eelco

Share this post


Link to post
Share on other sites

Your setup seems to be okay... Try to fix the spi bug, that could be the problem. What isn't working exactly?

Share this post


Link to post
Share on other sites

Yup,

 

That should do the trick. I think I have reproduced your error. The card is detected 2 times in my case, but the data is corrupted, so it gives the wrong output. I applied the usci spi fix from my earlier post et voila, it works like charm! Can you try it as well?

 

Kind regards, Eelco

D69 likes this

Share this post


Link to post
Share on other sites

Yes! That sorted it (well that and me stopping being a window-licker and changing the TX/RX jumpers, but did that 1st and got corrupted output) thanks ever so much. Now I just need to get it to recognise my phone :D

Fantastic work!

 

Cheers

Dean

Share this post


Link to post
Share on other sites

Good to hear! As you can see I also put it in the update message on the first post.

 

By the way, this fix really helps with all SPI related stuff..

 

Kind regards,

 

Eelco

Share this post


Link to post
Share on other sites

Just a quick note, Pin1 - SS (SDA on mine) goes to 2.2 and Pin7 - RST goes to 1.3 :D

Looking at your pic it's setup the same as mine. I figured it out from the code before I checked the pic of your setup  :shock:

 

Cheers

Dean

Share this post


Link to post
Share on other sites

OK, spent a bit of time on this this evening.

Tried knocking together some code to allow hard coding of tag IDs which works OK. Might need to change the for loop though as anything in else just gets repeated, a switch might do the trick but it's late.

For anyone interested, here's my code, would appreciate any improvements that could be made, including an scenario where the tag's not recognized as valid.

/*
	Example file for communicating with the NFRC522. The program prints the card data.
	Created by Eelco Rouw - Originally adapted by Grant Gibson. 
*/

// Pinout
// SDA  - 2.2
// SCK  - 1.5
// Mosi - 1.7
// Miso - 1.6
// IRQ  - NC
// GND  - GND
// RST  - 1.3
// VCC  - VCC

#include <Mfrc522.h>

// the sensor communicates using SPI, so include the library:
#include <SPI.h>

int chipSelectPin = 10;
int NRSTDP = 5;
int ledpin1 = 3;
int ledpin2 = 6;
int cardint = 5;

Mfrc522 Mfrc522(chipSelectPin,NRSTDP);
unsigned char serNum[5];
unsigned char sectorKey[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char readData[16];

void setup() 
{                
	Serial.begin(9600);                       // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
	// Start the SPI library:
	SPI.begin();
	// Initialize the Card Reader
	digitalWrite(chipSelectPin, LOW);
	pinMode(RED_LED, OUTPUT);
        pinMode(ledpin1, OUTPUT);
        pinMode(ledpin2, OUTPUT);
	Mfrc522.Init();  
}

void loop()
{
	unsigned char i,tmp;
	unsigned char status;
	unsigned char str[MAX_LEN];
	unsigned char RC_size;
	unsigned char blockAddr;
	//String mynum = "";
        String mycardno;
        char* myarray[2][5]={  //Array of tags and related names, tag numbers copied from serial console after 1s
          {"205928944228","1251291514471","7748944415","157172894468","611918944247"},
          {"Dad","Mum","Baby","Dog","Cat"}
          };
  	
	status = Mfrc522.Request(PICC_REQIDL, str);	
	if (status == MI_OK)
	{
		Serial.println("Card detected");
		Serial.print(str[0],BIN);
		Serial.print(" , ");
		Serial.print(str[1],BIN);
		Serial.println(" ");
	}

	status = Mfrc522.Anticoll(str);
	memcpy(serNum, str, 5);
	if (status == MI_OK)
	{
		Serial.println("The card's number is  : ");
		Serial.print(serNum[0], DEC);
		Serial.print(" , ");
		Serial.print(serNum[1], DEC);
		Serial.print(" , ");
		Serial.print(serNum[2], DEC);
		Serial.print(" , ");
		Serial.print(serNum[3], DEC);
		Serial.print(" , ");
		Serial.print(serNum[4], DEC);
		Serial.println(" ");
                mycardno = String(serNum[0]) += String(serNum[1]) += String(serNum[2]) += String(serNum[3]) += String(serNum[4]); // Appends the content of the serNum array to give a unique card no
                Serial.println(mycardno);
                Mfrc522.SelectTag(serNum);
                status = Mfrc522.Auth(PICC_AUTHENT1A,1,sectorKey,serNum);
                if (status == MI_OK)
                {
                  Serial.println("Authenticated...\r\n");
                } else
                {
                  Serial.println("Error authenticating...\r\n");
                }
                
                status = Mfrc522.ReadBlock(1, readData);
                if (status == MI_OK)
                {
                  for(i=0; i<16; i++)
                  {                  
                    Serial.write(readData[i]);
                    delay(10);
                  }                  
                } else {
                  Serial.println("Error reading.");
                }
                int i;
                for(i = 0; i < 5; i = i + 1)
                {
                  if (mycardno == myarray[0][i])
                  {
                  Serial.print("Hello ");
                  Serial.println(myarray[1][i]);        // Should Print out name matching tag
                }
                /*else {
                  Serial.println("Bugger Off");
                }*/
                }
  
		delay(1000);
                Mfrc522.Init();	
	}
	//Serial.println(" ");
	Mfrc522.Halt();	                        
}

Share this post


Link to post
Share on other sites

Today I got RF522 module, works fine with Energia library - THANKS.

What do You think about small pcb module RFID-Door Control (MSP430G2+Relay+Buzzer+LED) connected directly to RC522 board?

Share this post


Link to post
Share on other sites

I think that would be a great idea. You should check the possibilities with the available smart cards, you can do some really neat stuff with it!

 

I could envision you should add a small serial eeprom to the board as you can store an access table and do something with challenge/handshake.

 

I haven't tried the above but it would help to make a solid door mechanism.

 

Kind regards, Eelco

Share this post


Link to post
Share on other sites

Today I got RF522 module, works fine with Energia library - THANKS.

What do You think about small pcb module RFID-Door Control (MSP430G2+Relay+Buzzer+LED) connected directly to RC522 board?

Sounds similar to what I'm working on, adding a uln2003 to help with the mixed power of the leds and relays.

Serial eeprom sounds like a good idea too, I was going to try with an SD card but it would be overkill!

Share this post


Link to post
Share on other sites

That looks good Traxman

What's the idea behind your MOSI/MISO setup, I've not seen it done like that before?

The ULN with relay combo is working fine at the minute, are there any equivalents arrays with FETs?

Just migrated my setup to a breadboard and it works perfectly, ready to stick it onto protoboard now.

post-33474-0-24152400-1377848558_thumb.jpg

Share this post


Link to post
Share on other sites

Could you give me your  RFID-RC522 on lanchpad  source codes ?

Because I can't run your source code 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now