In this example, we will connect an RFID reader (RC522) and an LED. Via the SPI bus, the reader communicates the card's UID number to the Arduino. If the number matches the one in the code, the Arduino turns on the LED indicator.
RFID RC522:
LED Indicator:
IMPORTANT: The RFID RC522 module operates on 3.3V. Connecting it to a 5V pin can permanently damage the module!
To function, you need the MFRC522 library. Install it in the Arduino IDE: Sketch -> Include Library -> Manage Libraries (search for "MFRC522").
/* * LIBRARY: Install "MFRC522"
* WIRING: RFID to SPI pins (10, 11, 12, 13, 9), LED to Pin 2
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
#define LED_PIN 2
MFRC522 mfrc522(SS_PIN, RST_PIN);
byte allowedUID[] = {0x70, 0x14, 0xD0, 0x13};
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.println("System ready...");
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent()) return;
if (!mfrc522.PICC_ReadCardSerial()) return;
bool isCorrect = true;
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != allowedUID[i]) {
isCorrect = false;
break;
}
}
if (isCorrect) {
Serial.println("Recognized: LED ON");
digitalWrite(LED_PIN, HIGH);
delay(3000);
digitalWrite(LED_PIN, LOW);
} else {
Serial.println("Unknown card");
}
delay(1000);
}