#include
#include
#define SS_PIN 10 // Pin for SDA
#define RST_PIN 9 // Pin for RST
#define LED_PIN 6 // Pin for LED or Buzzer
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communication
SPI.begin(); // Initialize SPI bus
rfid.PCD_Init(); // Initialize the RFID reader
pinMode(LED_PIN, OUTPUT); // Set the LED or Buzzer pin as output
Serial.println("Place your RFID tag near the reader.");
}
void loop() {
// Look for new RFID cards
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.print("RFID tag UID: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX); // Print UID in hexadecimal format
}
Serial.println();
// Turn on LED or Buzzer for one second
digitalWrite(LED_PIN, HIGH); // Turn on the LED or Buzzer
delay(1000); // Keep it on for 1 second
digitalWrite(LED_PIN, LOW); // Turn off the LED or Buzzer
// Halt the PICC to prevent multiple readings of the same card
rfid.PICC_HaltA();
delay(1000); // Add a delay to avoid multiple detections
}
}
Code for Sketch Two (No serial output):
#include
#include
#define SS_PIN 10 // Pin for SDA
#define RST_PIN 9 // Pin for RST
#define LED_PIN 6 // Pin for LED or Buzzer
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
SPI.begin(); // Initialize SPI bus
rfid.PCD_Init(); // Initialize the RFID reader
pinMode(LED_PIN, OUTPUT); // Set the LED or Buzzer pin as output
digitalWrite(LED_PIN, HIGH); // Initially, turn the LED (or Buzzer) ON
}
void loop() {
// Look for new RFID cards
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
// Turn off LED or Buzzer when the RFID tag is presented
digitalWrite(LED_PIN, LOW);
// Halt the PICC to prevent multiple readings of the same card
rfid.PICC_HaltA();
delay(1000); // Add a delay to avoid multiple detections
}
}
Notes:
Use the following libraries by writing: #include <SPI.h> and #include <MFRC522.h> at the top of the sketch. This page treats it as instruction, so will not let us show it in the main code itself.
Choosing a selection results in a full page refresh.
Press the space key then arrow keys to make a selection.
Use left/right arrows to navigate the slideshow or swipe left/right if using a mobile device