Turning a Lamp On and Off with Arduino RFID Card
Turning a Lamp On and Off with Arduino RFID Card
Hello friends, in this post we will create an application with Arduino where the LED lamp stays on for as long as the RFID card is read by the system; in addition to the duration the card is read, it will only light up when the correct card is scanned, meaning it won’t be possible to use other cards to turn on the lamp. Let’s get started with our project.
Requirements for the Project
- Arduino
- RFID Kit
- Jumper Cable
- Relay
- LED
I think the most important point in making a project is to understand how it works, so first let me summarize how it operates.
By using a relay connected to the Arduino, we turn on and off the 12V LED lights I have by cutting or providing power; if the card is near the sensor and it’s the correct card, the relay switches on and the light starts to glow. If the card is far away or the wrong card is used, the relay never engages.
Circuit Diagram of Our Project
Code of Our Project
Let’s include our libraries
#include <SPI.h> #include <MFRC522.h>
Note: Don’t forget to download and install the library! To download the library, click here.
Let’s define our variables
int rf_reset = 9, rf_ss = 10;
int led_control = 8;
MFRC522 RFID(rf_ss, rf_reset);
byte card_id[4] = {185, 144, 187, 176};
bool card_statue = LOW;Let’s explain our variables
rf_reset and rf_ss are the pins of the RFID card sensor, led_control is the pin where we connect the relay’s in pin.
card_id is the card’s identity data which allows operation only when the correct card is presented.
card_statute is the variable that’s True when the card is near and False when it’s far.
The part defined as MFRC522 RFID is the class provided by the library that allows us to access the RFID sensor.
Let’s define our functions
void RFIDStatue() {
if (!card_statue) {
if (!RFID.PICC_IsNewCardPresent()) {
return false;
} else {
card_statue = true;
if (RFID.uid.uidByte[0] == card_id[0] && RFID.uid.uidByte[1] == card_id[1] && RFID.uid.uidByte[2] == card_id[2] && RFID.uid.uidByte[3] == card_id[3])
ledOpen();
}
}
if (!RFID.PICC_ReadCardSerial()) {
card_statue = false;
ledClose();
return false;
}
}
void ledOpen() {
digitalWrite(led_control, LOW);
}
void ledClose() {
digitalWrite(led_control, HIGH);
}Let’s explain our functions
ledOpen() and ledClose() are functions we created instead of repeatedly writing digitalWrite for turning the LEDs on and off.
RFIDStatue is the function that checks whether the RFID card is near and whether the correct RFID card has touched the sensor.
If any information about the card cannot be obtained in the part "if(!RFID.PICC_ReadCardSerial())", it turns the LED off,
In the part "if (!RFID.PICC_IsNewCardPresent()){...}else{...}", it gets information about the card and if it is the correct card, it turns on the LED.
Let’s define the main functions
void setup() {
Serial.begin(9600);
SPI.begin();
RFID.PCD_Init();
pinMode(led_control, OUTPUT);
ledClose();
}
void loop() {
RFIDStatue();
}All codes
#include <SPI.h>
#include <MFRC522.h>
int rf_reset = 9, rf_ss = 10;
int led_control = 8;
MFRC522 RFID(rf_ss, rf_reset);
byte card_id[4] = {185, 144, 187, 176};
bool card_statue = LOW;
void setup() {
Serial.begin(9600);
SPI.begin();
RFID.PCD_Init();
pinMode(led_control, OUTPUT);
ledClose();
}
void RFIDStatue() {
if (!card_statue) {
if (!RFID.PICC_IsNewCardPresent()) {
return false;
} else {
card_statue = true;
if (RFID.uid.uidByte[0] == card_id[0] && RFID.uid.uidByte[1] == card_id[1] && RFID.uid.uidByte[2] == card_id[2] && RFID.uid.uidByte[3] == card_id[3])
ledOpen();
}
}
if (!RFID.PICC_ReadCardSerial()) {
card_statue = false;
ledClose();
return false;
}
}
void ledOpen() {
digitalWrite(led_control, LOW);
}
void ledClose() {
digitalWrite(led_control, HIGH);
}
void loop() {
RFIDStatue();
}


Yorum Gönder