I have the following code for an MFRC522 RFID reader:
#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
#define SIGNAL_PIN 2 // Pin connected to the Central Arduino's A0
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
bool isCorrectCard = false; // Boolean to track if the correct card was read
// UID of the correct card
byte correctUID[] = {0xA3, 0x0F, 0x98, 0xE4};
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay
pinMode(SIGNAL_PIN, OUTPUT); // Set SIGNAL_PIN as output
digitalWrite(SIGNAL_PIN, LOW); // Start with the signal pin low
delay(1000);
Serial.println(F("Scan PICC to see UID..."));
}
void loop() {
// Reset the loop if no new card is present on the sensor/reader
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Print the UID of the card with spaces
Serial.print(F("UID: "));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); // Add leading zero if needed
Serial.print(mfrc522.uid.uidByte[i], HEX); // Print each byte in hexadecimal
if (i < mfrc522.uid.size - 1) {
Serial.print(" "); // Separate bytes with a space
}
}
Serial.println();
// Track the previous state of isCorrectCard
bool previousIsCorrectCard = isCorrectCard;
// Check if the read UID matches the correct UID
isCorrectCard = true;
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != correctUID[i]) {
isCorrectCard = false;
break;
}
}
// Send a pulse when isCorrectCard changes
if (isCorrectCard != previousIsCorrectCard) {
digitalWrite(SIGNAL_PIN, HIGH); // Send a HIGH signal
delay(10); // Short pulse delay
digitalWrite(SIGNAL_PIN, LOW); // End the pulse
}
// Debug message
Serial.println(isCorrectCard ? F("Correct card detected!") : F("Incorrect card."));
// Halt the card to allow reading of a new one
mfrc522.PICC_HaltA();
}
I'm encountering an issue however where the board works perfectly fine and reads cards/sends output when the USB-MIDI is connected to a laptop, but it fails to read cards when connected to a wall adapter (despite still being powered on).
I had originally assumed it was a simple hardware issue involving either the wires, cables, or adapters, but have tried out about a dozen of each (since I keep a lot around for work) to no avail.
#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
#define SIGNAL_PIN 2 // Pin connected to the Central Arduino's A0
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
bool isCorrectCard = false; // Boolean to track if the correct card was read
// UID of the correct card
byte correctUID[] = {0xA3, 0x0F, 0x98, 0xE4};
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay
pinMode(SIGNAL_PIN, OUTPUT); // Set SIGNAL_PIN as output
digitalWrite(SIGNAL_PIN, LOW); // Start with the signal pin low
delay(1000);
Serial.println(F("Scan PICC to see UID..."));
}
void loop() {
// Reset the loop if no new card is present on the sensor/reader
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// Print the UID of the card with spaces
Serial.print(F("UID: "));
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); // Add leading zero if needed
Serial.print(mfrc522.uid.uidByte[i], HEX); // Print each byte in hexadecimal
if (i < mfrc522.uid.size - 1) {
Serial.print(" "); // Separate bytes with a space
}
}
Serial.println();
// Track the previous state of isCorrectCard
bool previousIsCorrectCard = isCorrectCard;
// Check if the read UID matches the correct UID
isCorrectCard = true;
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != correctUID[i]) {
isCorrectCard = false;
break;
}
}
// Send a pulse when isCorrectCard changes
if (isCorrectCard != previousIsCorrectCard) {
digitalWrite(SIGNAL_PIN, HIGH); // Send a HIGH signal
delay(10); // Short pulse delay
digitalWrite(SIGNAL_PIN, LOW); // End the pulse
}
// Debug message
Serial.println(isCorrectCard ? F("Correct card detected!") : F("Incorrect card."));
// Halt the card to allow reading of a new one
mfrc522.PICC_HaltA();
}
But unfortunately I'm still having the same issue. Are there any other quirks of the code that could prevent the reader from working on a wall adapter?
I don’t see any code quirks causing the issue. So, this means that hardware configuration is the likely cause. Please provide a schematic of your project, exact information on the hardware in use, and show how the wall supply connects to it (USB connection, DC barrel connection, or however you do it).
I happened to have an RC522 wired up to an Uno R3. After changing the UID to match one of my cards, moving the reset line to the pin I had I wired to, and putting an LED on pin 2, I compiled and uploaded it. Then I unhooked the USB connection and plugged the R3 into a wall adapter.
Waved the correct card over the reader and the LED flashed. Waved the other one over, and the LED flashed again.
The code appears to work just fine on a wall adapter. In the spirit of "the only dumb question is the one you didn't ask": you did upload the modified sketch after taking out the while (!Serial); line, right? You didn't just compile it and forget to upload it?