Rfid and 2x relais

I adapted the tutorial code for multiple Rfid tags (which is working now).

/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-relay
*/

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 5
#define RELAY_PIN1 A5 // the Arduino pin connects to relay
#define RELAY_PIN2 A4 // the Arduino pin connects to relay


MFRC522 rfid(SS_PIN, RST_PIN);

byte authorizedUID1[4] = {0x15, 0x0F, 0xA9, 0xAC};
byte authorizedUID2[4] = {0xA8, 0xC0, 0xFF, 0x1E};
byte authorizedUID3[4] = {0xA0, 0xBD, 0x08, 0x22};

void setup() {
  Serial.begin(9600);
  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  pinMode(RELAY_PIN1, OUTPUT);   // initialize pin as an output.
  digitalWrite(RELAY_PIN1, LOW); // deactivate the relay
  pinMode(RELAY_PIN2, OUTPUT);   // initialize pin as an output.
  digitalWrite(RELAY_PIN2, LOW); // deactivate the relay

  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()) { // new tag is available
    if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

      if (rfid.uid.uidByte[0] == authorizedUID1[0] &&
          rfid.uid.uidByte[1] == authorizedUID1[1] &&
          rfid.uid.uidByte[2] == authorizedUID1[2] &&
          rfid.uid.uidByte[3] == authorizedUID1[3] ) {
        Serial.println("Authorized Tag 1");
        digitalWrite(RELAY_PIN1, HIGH); // activate the relay for 5 seconds
        delay(5000);
        digitalWrite(RELAY_PIN1, LOW);  // deactivate the relay
      }
      else if (rfid.uid.uidByte[0] == authorizedUID2[0] &&
               rfid.uid.uidByte[1] == authorizedUID2[1] &&
               rfid.uid.uidByte[2] == authorizedUID2[2] &&
               rfid.uid.uidByte[3] == authorizedUID2[3] ) {
        Serial.println("Authorized Tag 2");
        digitalWrite(RELAY_PIN2, HIGH); // activate the relay for 5 seconds
        delay(5000);
        digitalWrite(RELAY_PIN2, LOW);  // deactivate the relay
      }

      else if (rfid.uid.uidByte[0] == authorizedUID3[0] &&
               rfid.uid.uidByte[1] == authorizedUID3[1] &&
               rfid.uid.uidByte[2] == authorizedUID3[2] &&
               rfid.uid.uidByte[3] == authorizedUID3[3] ) {
        Serial.println("Authorized Tag 3");
        digitalWrite(RELAY_PIN1, HIGH); // activate the relay for 5 seconds
        delay(5000);
        digitalWrite(RELAY_PIN1, LOW);  // deactivate the relay
      }


      else
      {
        Serial.print("Unauthorized Tag with UID:");
        for (int i = 0; i < rfid.uid.size; i++) {
          Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
          Serial.print(rfid.uid.uidByte[i], HEX);
        }
        Serial.println();
      }

      rfid.PICC_HaltA(); // halt PICC
      rfid.PCD_StopCrypto1(); // stop encryption on PCD
    }
  }
}

I want to adapt this in such a way that when I switch a relay ( say RelayPin1 with Tag1) it stays active and when I present Tag1 again, the relay switches off.

Any suggestions where to start?

Rgrds, Willem

when you have read the tag, read the output.
if the value of the output is LOW set the output to HIGH
if the value of the output is HIGH set the output to LOW

FYI
For a future iteration of your code, read up on memcmp().

I have changed the first part of the sketch (RELAY_PIN1) according to the below part, the relais is not reacting, what am I doing wrong?

/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-relay
*/

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 5
#define RELAY_PIN1 A5 // the Arduino pin connects to relay
#define RELAY_PIN2 A4 // the Arduino pin connects to relay


MFRC522 rfid(SS_PIN, RST_PIN);

byte authorizedUID1[4] = {0x15, 0x0F, 0xA9, 0xAC};
byte authorizedUID2[4] = {0xA8, 0xC0, 0xFF, 0x1E};
byte authorizedUID3[4] = {0xA0, 0xBD, 0x08, 0x22};

void setup() {
  Serial.begin(9600);
  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  pinMode(RELAY_PIN1, OUTPUT);   // initialize pin as an output.
  digitalWrite(RELAY_PIN1, LOW); // deactivate the relay
  pinMode(RELAY_PIN2, OUTPUT);   // initialize pin as an output.
  digitalWrite(RELAY_PIN2, LOW); // deactivate the relay

  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()) { // new tag is available
    if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

      if (rfid.uid.uidByte[0] == authorizedUID1[0] &&
          rfid.uid.uidByte[1] == authorizedUID1[1] &&
          rfid.uid.uidByte[2] == authorizedUID1[2] &&
          rfid.uid.uidByte[3] == authorizedUID1[3] ) {
        Serial.println("Authorized Tag 1");

        if (RELAY_PIN1==LOW) digitalWrite(RELAY_PIN1, HIGH); 
        if (RELAY_PIN1==HIGH) digitalWrite(RELAY_PIN1, LOW);       
        
      }
      else if (rfid.uid.uidByte[0] == authorizedUID2[0] &&
               rfid.uid.uidByte[1] == authorizedUID2[1] &&
               rfid.uid.uidByte[2] == authorizedUID2[2] &&
               rfid.uid.uidByte[3] == authorizedUID2[3] ) {
        Serial.println("Authorized Tag 2");
        digitalWrite(RELAY_PIN2, HIGH); // activate the relay for 5 seconds
        delay(5000);
        digitalWrite(RELAY_PIN2, LOW);  // deactivate the relay
      }

      else if (rfid.uid.uidByte[0] == authorizedUID3[0] &&
               rfid.uid.uidByte[1] == authorizedUID3[1] &&
               rfid.uid.uidByte[2] == authorizedUID3[2] &&
               rfid.uid.uidByte[3] == authorizedUID3[3] ) {
        Serial.println("Authorized Tag 3");
        digitalWrite(RELAY_PIN1, HIGH); // activate the relay for 5 seconds
        delay(5000);
        digitalWrite(RELAY_PIN1, LOW);  // deactivate the relay
      }


      else
      {
        Serial.print("Unauthorized Tag with UID:");
        for (int i = 0; i < rfid.uid.size; i++) {
          Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
          Serial.print(rfid.uid.uidByte[i], HEX);
        }
        Serial.println();
      }

      rfid.PICC_HaltA(); // halt PICC
      rfid.PCD_StopCrypto1(); // stop encryption on PCD
    }
  }
}

So you make the pin high if it's low, next check if it is high (which it will be) and make it low if it high.

Maybe you wanted to use an if/else instead of two ifs.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.