How to create an RFID tag that Activates the motor
...........................................................................Diagram..................................................................
.................................................................Connection route................................................................
How it Works:
- Setup:
- Replace
authorizedUID
with the UID of your authorized RFID tag. - The relay module is connected to
RELAY_PIN
(pin 8).
- Replace
- Motor Control:
- When the authorized tag is scanned, the motor toggles ON/OFF.
- UID Detection:
- The program reads the RFID tag's UID and compares it with
authorizedUID
.
- The program reads the RFID tag's UID and compares it with
Wiring:
- RFID Reader:
- VCC → 3.3V
- RST → Pin 9
- GND → GND
- MISO → Pin 12
- MOSI → Pin 11
- SCK → Pin 13
- SDA → Pin 10
- Relay Module:
- VCC → 5V
- GND → GND
- IN → Pin 8
................................................................... Arduino Code.....................................................................
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Reset pin for RFID
#define SS_PIN 10 // Slave Select pin for RFID
#define RELAY_PIN 8 // Pin connected to relay module
MFRC522 rfid(SS_PIN, RST_PIN);
bool motorState = false; // Motor state: false = OFF, true = ON
String authorizedUIDs[] = {"12345678", "87654321"}; // Replace with your two tag UIDs
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure motor is OFF at start
Serial.println("Place your RFID tag near the reader.");
}
void loop() {
// Check if a new card is present
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
String uid = getUID(); // Get the UID of the scanned tag
Serial.println("Scanned UID: " + uid);
if (isAuthorizedTag(uid)) {
toggleMotor(); // Toggle motor state if UID matches one of the authorized tags
} else {
Serial.println("Unauthorized tag!");
}
rfid.PICC_HaltA(); // Halt communication with the card
}
// Function to toggle motor state
void toggleMotor() {
motorState = !motorState; // Flip the motor state
digitalWrite(RELAY_PIN, motorState ? HIGH : LOW);
if (motorState) {
Serial.println("Motor turned ON");
} else {
Serial.println("Motor turned OFF");
}
}
// Function to read the UID from the RFID tag
String getUID() {
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toUpperCase();
return uid;
}
// Function to check if the scanned tag is authorized
bool isAuthorizedTag(String uid) {
for (String authorizedUID : authorizedUIDs) {
if (uid == authorizedUID) {
return true;
}
}
return false;
}
................................................................Full working model............................................................
..............................................................................................................................................................