I'm using the ELEGOO MEGA 2560 that I bought off amazon. I'm having some issues where when I take the RFID fob and place it against the RFID module a bunch of numbers are suppose to show up according to instructions that came with it, nothing shows up for me. I triple checked my wiring and I believe everything is correct, I'm completely stumped. I also installed the library that it said to install
This is my code for reference:
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <deprecated.h>
#include <require_cpp11.h>
//www.elegoo.com
//2016.12.09
/*
* --------------------------------------------------------------------------------------------------------------------
* Example to change UID of changeable MIFARE card.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* This sample shows how to set the UID on a UID changeable MIFARE card.
* NOTE: for more informations read the README.rst
*
* @author Tom Clement
* @license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
/* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}
// Setting the UID can be as simple as this:
//void loop() {
// byte newUid[] = NEW_UID;
// if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
// Serial.println("Wrote new UID to card.");
// }
// delay(1000);
//}
// But of course this is a more proper approach
void loop() {
// Look for new cards, and select one if present
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
// Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
// Dump PICC type
// MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Serial.print(F("PICC type: "));
// Serial.print(mfrc522.PICC_GetTypeName(piccType));
// Serial.print(F(" (SAK "));
// Serial.print(mfrc522.uid.sak);
// Serial.print(")\r\n");
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
// Serial.println(F("This sample only works with MIFARE Classic cards."));
// return;
// }
// Set new UID
byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println(F("Wrote new UID to card."));
}
// Halt PICC and re-select it so DumpToSerial doesn't get confused
mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return;
}
// Dump the new memory contents
Serial.println(F("New UID and contents:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000);
}
This is the wiring diagram provided by the instructions:
using Arduino_MFRC522v2 and running file File/Examples/RFID_MFRC522v2/CheckFirmware with ss_pin setting
MFRC522DriverPinSimple ss_pin(53);
I get
*****************************
MFRC522 Digital self test
*****************************
Firmware Version: 0x92 = v2.0
-----------------------------
Only known versions supported
-----------------------------
Performing test...
-----------------------------
Result: OK
*****************************
MFRC522 Digital self test
*****************************
Firmware Version: 0x18 = (unknown)
-----------------------------
Only known versions supported
-----------------------------
Performing test...
-----------------------------
Result: DEFECT or UNKNOWN
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program to test your firmware.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* This example test the firmware of your MFRC522 reader module, only known version can be checked. If the test passed
* it do not mean that your module is faultless! Some modules have bad or broken antennas or the PICC is broken.
*
* @author Rotzbua
* @license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
/**
* Check firmware only once at startup
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 module
Serial.println(F("*****************************"));
Serial.println(F("MFRC522 Digital self test"));
Serial.println(F("*****************************"));
mfrc522.PCD_DumpVersionToSerial(); // Show version of PCD - MFRC522 Card Reader
Serial.println(F("-----------------------------"));
Serial.println(F("Only known versions supported"));
Serial.println(F("-----------------------------"));
Serial.println(F("Performing test..."));
bool result = mfrc522.PCD_PerformSelfTest(); // perform the test
Serial.println(F("-----------------------------"));
Serial.print(F("Result: "));
if (result)
Serial.println(F("OK"));
else
Serial.println(F("DEFECT or UNKNOWN"));
Serial.println();
}
void loop() {} // nothing to do
*****************************
MFRC522 Digital self test
*****************************
Firmware Version: 0x92 = v2.0
-----------------------------
Only known versions supported
-----------------------------
Performing test...
-----------------------------
Result: OK
if I try a different MFRC522 I get
*****************************
MFRC522 Digital self test
*****************************
Firmware Version: 0xB2 = (unknown)
-----------------------------
Only known versions supported
-----------------------------
Performing test...
-----------------------------
Result: DEFECT or UNKNOWN
clearly program only works for certain MFRC522 modules
however, if I run File>Examples>MRFC522>ReadNUID on the same MFRC522 it works
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program showing how to read new NUID from a PICC to serial.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
* will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* @license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] ||
rfid.uid.uidByte[3] != nuidPICC[3] ) {
Serial.println(F("A new card has been detected."));
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
}
else Serial.println(F("Card read previously."));
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(' ');
Serial.print(buffer[i], DEC);
}
}
serial monitor displays
This code scan the MIFARE Classsic NUID.
Using the following key: FF FF FF FF FF FFPICC type: MIFARE 1KB
A new card has been detected.
The NUID tag is:
In hex: 15 64 FD C2
In dec: 21 100 253 194
PICC type: MIFARE 1KB
A new card has been detected.
The NUID tag is:
In hex: C3 65 94 95
In dec: 195 101 148 149
PICC type: MIFARE 1KB
A new card has been detected.
The NUID tag is:
In hex: BF EC 0C 04
In dec: 191 236 12 4
Sadly this is incorrect, but most of the internet thinks this is how you do it.
The problem is that the Arduino Mega2560 is a 5V device and so gives 5V signals out. While the RFID reader is a 3V3 device and so a 5V signal is too much for it, and will damage it. Therefore you need to cut down any signal dent from the Mega to the reader needs to be cut down to 3V3 to avoid damage. This can be simply done by using a potential divider on each pin. This means the correct wiring should be:-
Arduino UNO/Nano/Mega operates at 5V logic. However, the MFRC522 works at 3.3V logic level, and is not 5V tolerant. If using one of these microprocessors:
You must use a 5V <-> 3.3V level shifter on any lines that send HIGH output from the Arduino to the MFRC522. These are RST, NSS, MOSI, and SCK marked with an asterisk in the list above.
You may use a 3.3V <-> 5V level shifter on those lines that send HIGH output from the MFRC522 to the Arduino (MISO) though this is not generally necessary - a HIGH 3.3V signal will normally be recognised as a HIGH signal on a 5V system too.
But, while logic level shifters solve the problem of interfacing between devices at different voltages, they can introduce other problems... simple bi-directional convertors use transistors that can be slow to react, and this delay damages the timing and quality of the signal. This can be mitigated somewhat by explicitly slowing down the speed of the SPI interface in the Arduino code, by including #define MFRC522_SPICLOCK (400000u) (Sets SPI bus to 400kHz) prior to including the RFID library.
MOSI, MISO, and SCK lines are shared between all readers, and use Arduino SPI pins 11(MOSI), 12(MISO), and 13(SCK).
NSS lines must be assigned to unique GPIO pins for each reader.
That's very odd I would imagine that the instructions that come in the kit would be right. It doesn't have a level shifter either so, because it should come with necessary things that I need to do that. I was watching other youtube videos and I did the same thing as them theres worked and mine didn't.
You are not the first to discover that ELEGOO supplied RFID readers do not actually work. We have had a few reports of this in the forum.
It is amazing how many suppliers of information, especially on You Tube, and the Instructables web site do not know what they are doing. What happens is someone does something and every one just copies that.
If you want to see this in action then search for how fix sticking keys on a TV remote. They all have a line which says something like "remove the batteries and hold down the on key for thirty seconds to allow any residual charge to discharge". This is utter nonsense and it might as well say "throw some salt over your shoulder and turn withershins three times".
This is because :-
If there is a sticking key causing problems that will discharge any current in the system once the battery is removed.
The bit that does the freeing of the keys is an entirely mechanical action so it makes no sense if the remote is powered or not.
Yet all the instructions and videos tell you to do this. So in the world of then internet it becomes a "thing".
This is exactly the same with the myth that you don't need level shifting when interfacing a 5V processor with a 3V3 system. Or the other myth that you don't need some form of current control when connecting an LED to any processor.
But you don't know how long they worked for. Doing this will shorten the life of components and they will fail sooner than they should. How long they will last you can only tell by testing at least 100 such circuits at an elevated temperature for about six months and counting the number that have failed. That is what profession system suppliers do to get the MTF (Mean Time between Failure) of any system. I know, I used to design set top boxes for satellite, terrestrial and cable TVs.
Look at how many threads we get here about stuff suddenly stopping working. We try and ask the people asking this to post a schematic so we can check they are not doing something stupid like ignoring the limits of components but often they say what is the point it used to work?
Conclusion:-
Buy another RFID reader from Ebay, much cheaper than Amazon (most stuff is) and aviod stuff from ELEGOO.
agreed!
on a device which uses 3.3V logic unless pins are specified as 5volt tolerant the device can be damaged if 5V logic is applied
in practice 3.3V devices may work with 5V logic for an minute, an hour, a day, for ever or fail immediately - you don't know - use level converters or even better switch to a microcontroller which uses 3.3V logic
this applies to WiFi modules, displays, sensors, etc
perhaps time to be moving on to use 3.3Vlogic microcontrollers??
They are often more robust with regard current abuse a pin can source or sink.
A lot of sensors, especially older ones, are designed to use 5V.
The same goes for output devices like LED strips based on the WS20812 (Neopixel type). Yes you could run these at 3V but this does redruce drastically the brightness they will run at.
You don't have to use virtual addresses, with most 5V systems, which is something that also confuses beginners.
Their may be some others I can't think of at the moment.
good points
however, on the various industrial and research projects I worked on I cannot remember when I last used a 5V logic micro - maybe a Motorola MC68000 or a MC6800
currently having to use level converters for 3.3V to 1.8V logic