Struggling with these RFID-RC522 sensors for a dozen hours.
First off, I'm having trouble getting these sensors detected at all. But when they do connect, they always detect RFID tags. Until they suddenly stopped.
But in this set up in the photos, I'm able to consistently connect the sensor according to my code. When I succeeded in that before, it would then always be able to detect an RFID tag. Now when it connects it won't read RFID tags. I don't know what changed.
Here are some things I've tried:
- Yes, there are a lot of wires in the images, but I'm showing this version because it's the only set up that consistently shows the sensor connecting correctly. But I have tried other ways of connecting the sensors and when it does connect, it won't detect any RFID tags still.
- I have tried a dozen different sensors..
- I have tried a four Arduino UNOs.
- I have checked the resistance with every wire with the multimeter.
- I have tried a dozen different rfid tags and cards
Here is the code. Most of it is not relevant. It is set up to connect and detect four sensors. But should work and was working with just one. Once the setup shows its connected, it should detect in the loop(). But now it won't. It was, but now it won't.
/**
* "Object Placement" Puzzle
*
* This puzzle requires the player to place one or more items in the
* correct location, at which point an RFID tag embedded in the object
* is detected by a sensor.
* When all sensors read the tag of the correct object, the puzzle is solved.
*
* Demonstrates:
* - SPI interface shared between several devices
* - RFID library
*/
//Updated on 5/14/24
// DEFINES
// Provides debugging information over serial connection if defined
#define DEBUG
// LIBRARIES
// Standard SPI library comes bundled with the Arduino IDE
#include <SPI.h>
// Download and install from https://github.com/miguelbalboa/rfid
#include <MFRC522.h>
// CONSTANTS
// The number of RFID readers
const byte numReaders = 4;
// Each reader has a unique Slave Select pin
const byte ssPins[] = {2, 3, 4, 5};
// They'll share the same reset pin
const byte resetPin = 8;
// This pin will be driven LOW to release a lock when puzzle is solved
const byte lockPin = A0;
// The sequence of NFC tag IDs required to solve the puzzle
//const String correctIDs[] = {"3acbbd49", "2162261d", "c946c06e", "87e0f0a4"};
const String correctIDs[] = {"e946c06e", "2162261d", "c946c06e", "f9c57059"};
const String subcorrectIDs[] = {"11a6ed1d", "211c411d", "d1b02224", "11aa481d"};
const String tokenBackupIDs[] = {"8370cda0", "13e4d1a6", "337cfbb6", "23f6c0a6"};
const String cardIDs[] = {"40f87d1d", "93116aa9", "a3b8c395", "b35f96aa"};
const String cardBackupIDs[] = {"404c661d", "f191b81c", "43c42632", "f398a9aa"};
const String tokenBackupIDs2[] = {"c33495d", "2323b1d", "63b1a4d", "370a2d"};
const String cardBackupIDs2[] = {"5316b10", "23cf2810", "f31a2e11", "837ef311"};
const String tokenBackupIDs3[] = {"d3aabbd", "c3ed19f8", "f3883a13", "b35719"};
const String cardBackupIDs3[] = {"f3f95510", "23de011", "53fee911", "a36e8511"};
//Specify digital pin on the Arduino that the positive lead of piezo buzzer is attached.
int piezoPin = 10;
// GLOBALS
// Initialise an array of MFRC522 instances representing each reader
MFRC522 mfrc522[numReaders];
// The tag IDs currently detected by each reader
String currentIDs[numReaders];
/**
* Initialisation
*/
void (* resetFunc) (void) = 0;
void setup() {
#ifdef DEBUG
// Initialise serial communications channel with the PC
Serial.begin(9600);
Serial.println(F("Serial communication started"));
#endif
// Set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
// We set each reader's select pin as HIGH (i.e. disabled), so
// that they don't cause interference on the SPI bus when
// first initialised
for (uint8_t i=0; i<numReaders; i++) {
pinMode(ssPins[i], OUTPUT);
digitalWrite(ssPins[i], HIGH);
}
// Initialise the SPI bus
SPI.begin();
for (uint8_t i=0; i<numReaders; i++) {
// Initialise the reader
// Note that SPI pins on the reader must always be connected to certain
// Arduino pins (on an Uno, MOSI=> pin11, MISO=> pin12, SCK=>pin13)
// The Slave Select (SS) pin and reset pin can be assigned to any pin
mfrc522[i].PCD_Init(ssPins[i], resetPin);
// Set the gain to max - not sure this makes any difference...
// mfrc522[i].PCD_SetAntennaGain(MFRC522::PCD_RxGain::RxGain_max);
#ifdef DEBUG
// Dump some debug information to the serial monitor
Serial.print(F("Reader #"));
Serial.print(i);
Serial.print(F(" initialised on pin "));
Serial.print(String(ssPins[i]));
Serial.print(F(". Antenna strength: "));
Serial.print(mfrc522[i].PCD_GetAntennaGain());
Serial.print(F(". Version : "));
mfrc522[i].PCD_DumpVersionToSerial();
#endif
// Slight delay before activating next reader
delay(100);
}
#ifdef DEBUG
Serial.println(F("--- END SETUP ---"));
#endif
//set up complete sound
tone(piezoPin, 1000, 500);
}
/**
* Main loop
*/
void loop() {
// Serial.println(F("--- Loop ---"));
// Assume that the puzzle has been solved
boolean puzzleSolved = true;
// Assume that the tags have not changed since last reading
boolean changedValue = false;
// Loop through each reader
for (uint8_t i=0; i<numReaders; i++) {
// Serial.println(String(i));
// Initialise the sensor
mfrc522[i].PCD_Init();
// String to hold the ID detected by each sensor
String readRFID = "";
// If the sensor detects a tag and is able to read it
if(mfrc522[i].PICC_IsNewCardPresent() && mfrc522[i].PICC_ReadCardSerial()) {
// Extract the ID from the tag
readRFID = dump_byte_array(mfrc522[i].uid.uidByte, mfrc522[i].uid.size);
Serial.println(F("--- DETECTED ---"));
Serial.print(String(readRFID));
tone(piezoPin, 1000+ (i * 500), 50);
}
if(mfrc522[i].PICC_IsNewCardPresent())
{
Serial.println("New card");
}
if(mfrc522[i].PICC_ReadCardSerial())
{
Serial.println("Read Serial");
}
// If the current reading is different from the last known reading
if(readRFID != currentIDs[i]){
// Set the flag to show that the puzzle state has changed
changedValue = true;
// Update the stored value for this sensor
currentIDs[i] = readRFID;
// Serial.println(F("--- CHANGED from last reading---"));
}
// If the reading fails to match the correct ID for this sensor
if(currentIDs[i] != correctIDs[i] && currentIDs[i] != subcorrectIDs[i]
&& currentIDs[i] != tokenBackupIDs[i]
&& currentIDs[i] != tokenBackupIDs2[i]
&& currentIDs[i] != tokenBackupIDs3[i]
&& currentIDs[i] != cardIDs[i]
&& currentIDs[i] != cardBackupIDs[i]
&& currentIDs[i] != cardBackupIDs2[i]
&& currentIDs[i] != cardBackupIDs3[i]) {
// The puzzle has not been solved
puzzleSolved = false;
}
// Halt PICC
mfrc522[i].PICC_HaltA();
// Stop encryption on PCD
mfrc522[i].PCD_StopCrypto1();
}
#ifdef DEBUG
// If the changedValue flag has been set, at least one sensor has changed
if(changedValue){
// Dump to serial the current state of all sensors
for (uint8_t i=0; i<numReaders; i++) {
// Serial.print(F("Reader #"));
// Serial.print(String(i));
// Serial.print(F(" on Pin #"));
// Serial.print(String((ssPins[i])));
// Serial.print(F(" detected tag: "));
// Serial.println(currentIDs[i]);
}
// Serial.println(F("-- At least one sensor changed --"));
}
#endif
#ifdef DEBUG
if(currentIDs[3] == correctIDs[3] || currentIDs[3] == subcorrectIDs[3]
|| currentIDs[3] == tokenBackupIDs[3]
|| currentIDs[3] == tokenBackupIDs2[3]
|| currentIDs[3] == tokenBackupIDs3[3]
|| currentIDs[3] == cardIDs[3]
|| currentIDs[3] == cardBackupIDs[3]
|| currentIDs[3] == cardBackupIDs2[3]
|| currentIDs[3] == cardBackupIDs3[3]) {
// The puzzle has been solved
Serial.println(F("--- 3 correct---"));
}
if(currentIDs[2] == correctIDs[2] || currentIDs[2] == subcorrectIDs[2]
|| currentIDs[2] == tokenBackupIDs[2]
|| currentIDs[2] == tokenBackupIDs2[2]
|| currentIDs[2] == tokenBackupIDs3[2]
|| currentIDs[2] == cardIDs[2]
|| currentIDs[2] == cardBackupIDs[2]
|| currentIDs[2] == cardBackupIDs2[2]
|| currentIDs[2] == cardBackupIDs3[2]) {
// The puzzle has been solved
Serial.println(F("--- 2 correct---"));
}
if(currentIDs[1] == correctIDs[1] || currentIDs[1] == subcorrectIDs[1]
|| currentIDs[1] == tokenBackupIDs[1]
|| currentIDs[1] == tokenBackupIDs2[1]
|| currentIDs[1] == tokenBackupIDs3[1]
|| currentIDs[1] == cardIDs[1]
|| currentIDs[1] == cardBackupIDs[1]
|| currentIDs[1] == cardBackupIDs2[1]
|| currentIDs[1] == cardBackupIDs3[1]) {
// The puzzle has been solved
Serial.println(F("--- 1 correct---"));
}
if(currentIDs[0] == correctIDs[0] || currentIDs[0] == subcorrectIDs[0]
|| currentIDs[0] == tokenBackupIDs[0]
|| currentIDs[0] == tokenBackupIDs2[0]
|| currentIDs[0] == tokenBackupIDs3[0]
|| currentIDs[0] == cardIDs[0]
|| currentIDs[0] == cardBackupIDs[0]
|| currentIDs[0] == cardBackupIDs2[0]
|| currentIDs[0] == cardBackupIDs3[0]) {
// The puzzle has been solved
Serial.println(F("--- 0 correct---"));
}
#endif
// if(currentIDs[1] == correctIDs[1]) {
// puzzleSolved = true;
//}
// If the puzzleSolved flag is set, all sensors detected the correct ID
if(puzzleSolved){
onSolve();
}
// Add a short delay before next polling sensors
delay(100);
}
/**
* Called when correct puzzle solution has been entered
*/
void onSolve(){
#ifdef DEBUG
// Print debugging message
Serial.println(F("Puzzle Solved!"));
#endif
// Release the lock
digitalWrite(lockPin, LOW);
tone(piezoPin, 440);
delay(700);
tone(piezoPin, 550);
delay(700);
tone(piezoPin, 660);
delay(700);
tone(piezoPin, 440);
delay(700);
noTone(piezoPin);
digitalWrite(lockPin, LOW);
delay(10000);
resetFunc();
//while(true) {
// delay(1000);
//}
}
/**
* Helper function to return a string ID from byte array
*/
String dump_byte_array(byte *buffer, byte bufferSize) {
String read_rfid = "";
for (byte i=0; i<bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
return read_rfid;
}