Issue while interfacing NRF24L01 and RFID on same arduino



  • I wanted to interface an RFID sensor and NRF24L01 on same arduino and on the other end another arduino with NRF24L01 to recive a message if the card is detected.

    /*
     * Typical pin layout used:
     * -----------------------------------------------------------------------------------------
     *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
     *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
     * Signal      Pin          Pin           Pin       Pin        Pin              Pin
     * -----------------------------------------------------------------------------------------
     * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
     * SPI SS 1    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
     * SPI SS 2    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
     * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
     * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
     * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
     *
     */
    
    #include <SPI.h>
    #include <MFRC522.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte add[6] = "00001";
    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_PIN        10         // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
    //#define SS_2_PIN        8          // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
    
    #define NR_OF_READERS   1
    
    byte ssPins[] = {SS_PIN};
    
    MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
    
    /**
     * Initialize.
     */
    void setup() {
    
      Serial.begin(9600); // Initialize serial communications with the PC
      while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    
      SPI.begin();        // Init SPI bus
    
      for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
        mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
        Serial.print(F("Reader "));
        Serial.print(reader);
        Serial.print(F(": "));
        mfrc522[reader].PCD_DumpVersionToSerial();
      }
    }
    /**
     * Main loop.
     */
    void loop() {
      /*Serial.println(i);
      if(i>100 && i<200){
        after();
        Serial.println("after");
        delay(10);
      }*/
      for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
        // Look for new cards
    
        if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
          Serial.print(F("Reader "));
          Serial.print(reader);
          // Show some details of the PICC (that is: the tag/card)
          Serial.print(F(": Card UID:"));
          dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
          Serial.println();
          mfrc522[reader].PCD_StopCrypto1();
          /*
          Serial.println();
          Serial.print(F("PICC type: "));
          MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
          Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
          *//*
          // Halt PICC
          mfrc522[reader].PICC_HaltA();
          // Stop encryption on PCD
          */
        } //if (mfrc522[reader].PICC_IsNewC
      } //for(uint8_t reader
      /*
      if(i<=100 || i>200){
        before();
        Serial.println("before");
        delay(10);
      }
      i++;*/
      after();
    }
    
    /**
     * Helper routine to dump a byte array as hex values to Serial.
     */
    void dump_byte_array(byte *buffer, byte bufferSize) {
      before();
      delay(10);
      mysetup();
      for (byte i = 0; i < bufferSize; i++) {
        const char text[] = "Hello World";
        radio.write(&text, sizeof(text));
        delay(10);
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
      }
    }
    
    void before() {
    // Make sure MFRC is disabled from the SPI bus
    pinMode(RST_PIN, INPUT);
    digitalWrite(RST_PIN, HIGH);
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, LOW);
    }
    
    void after() {
    // Make sure MFRC is disabled from the SPI bus
    pinMode(RST_PIN, OUTPUT);
    digitalWrite(RST_PIN, HIGH);
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, HIGH);
    }
    
    void mysetup(){
      radio.begin();
      delay(10);
      radio.openWritingPipe(add);
      delay(10);
      radio.setPALevel(RF24_PA_MAX);
      delay(10);
      radio.stopListening();
      delay(10);
    }
    
    

  • Mod

    Welcome to the MySensors community @123cheatn !

    Have you looked at the MySensors rfid example?


  • Mod

    @123cheatn if you can't resolve your issue with the example provided by @mfalkvidd then please give a thorough description of the issue.
    Helping is hard when you only state it just doesn't work 😬


  • Mod

    I had the same problem when I tried it: if you connect the rfid reader you loose the nrf24 communication. It is like they don't like sharing the SPI 😄


  • Mod

    @gohan that's probably why the examples has this:

    // Enable soft spi as radio has a hard time sharing spi 
    


  • thanks to all of you for the reply.
    I found the solution.
    after looking at the code and some debugging.
    my code is working fine now.
    This is the code

    /**
     * --------------------------------------------------------------------------------------------------------------------
     * Example sketch/program showing how to read data from more than one PICC to serial.
     * --------------------------------------------------------------------------------------------------------------------
     * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
     *
     * Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
     * MFRC522 based RFID Reader on the Arduino SPI interface.
     *
     * Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
     *          and knowledge are required!
     *
     * @license Released into the public domain.
     *
     * Typical pin layout used:
     * -----------------------------------------------------------------------------------------
     *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
     *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
     * Signal      Pin          Pin           Pin       Pin        Pin              Pin
     * -----------------------------------------------------------------------------------------
     * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
     * SPI SS 1    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
     * SPI SS 2    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
     * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
     * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
     * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
     *
     */
    
    #include <SPI.h>
    #include <MFRC522.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte add[6] = "00001";
    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_PIN        10         // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
    //#define SS_2_PIN        8          // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
    
    #define NR_OF_READERS   1
    
    byte ssPins[] = {SS_PIN};
    
    MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
    
    /**
     * Initialize.
     */
    void setup() {
    
      Serial.begin(9600); // Initialize serial communications with the PC
      while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    
      SPI.begin();        // Init SPI bus
    
      for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
        mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
        Serial.print(F("Reader "));
        Serial.print(reader);
        Serial.print(F(": "));
        mfrc522[reader].PCD_DumpVersionToSerial();
      }
      radio.begin();
      radio.openWritingPipe(add);
      radio.setPALevel(RF24_PA_MAX);
      radio.stopListening();
    }
    /**
     * Main loop.
     */
    void loop() {
      for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
        // Look for new cards
    
        if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
          Serial.print(F("Reader "));
          Serial.print(reader);
          // Show some details of the PICC (that is: the tag/card)
          Serial.print(F(": Card UID:"));
          dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
          Serial.println();
          mfrc522[reader].PCD_StopCrypto1();
        }
      }
    }
    
    /**
     * Helper routine to dump a byte array as hex values to Serial.
     */
    void dump_byte_array(byte *buffer, byte bufferSize) {
      int c[]={176,240,158,94};
      int count=0;
      delay(10);
      for (byte i = 0; i < bufferSize; i++) {
        delay(10);
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
      }
      Serial.println();
      for (byte i = 0; i < bufferSize; i++) {
        if(buffer[i]==c[i]){
          count++;
        }
      }
      int flag=0;
      if(count==4){
        const char text[] = "17624015894";
        radio.write(&text, sizeof(text));
      }else{
        const char text[] = "No";
        radio.write(&text, sizeof(text));
      }
    }
    
    void before() {
    // Make sure MFRC is disabled from the SPI bus
    pinMode(RST_PIN, INPUT);
    digitalWrite(RST_PIN, HIGH);
    pinMode(SS_PIN, OUTPUT);
    digitalWrite(SS_PIN, LOW);
    }
    

  • Mod

    But you still haven't used any mysensors code 🤔


  • Mod

    @gohan and we still don't know what the issue was 🤣


  • Mod

    he wrote me a PM, so the problem is the one I described



  • @yveaux the problem was in the code and in the circuit, actually both the modules were not working together.
    Previously I had to disconnect either one of the module.



  • hey, @gohan it was working fine without mysensors library.


  • Mod

    @123cheatn Ok, clear. Thanks for reporting back! 👍


  • Mod

    @123cheatn if you make it to work with mysensors, pls report back



  • @gohan sure I will try.



  • @123cheatn I'm trying very similar project but could not achieve it 😞
    On master Arduino, RFID UID ID will be read and send to 4 other Arduinos; they will react regarding to UID ID (activate a relay, led etc.). Also those 4 Arduinos have MFRC522 but won't be sending RF UID ID's, just reacting depending on which UID ID read by their reader.
    I think I could not solve multiple SPI module code 😕 Here is my code;

    #include <SPI.h>
    #include <MFRC522.h>
    #include <RF24.h>
    #include <NRF24.h>
    #include <nRF24L01.h>
    NRF24 radio;
    
    unsigned long time_now = 0;
    int blinkled = 300;
    int value;
    int displace = 1000;
    unsigned long CountDownStarted = 0;
    
    int CountdownState = 0;
    
    int Set60State = 0;
    int Set90State = 0;
    int EngineState = 0;
    
    #define EnginePin 6
    #define StartPin 7
    #define Set90Pin 8
    #define Set60Pin 9
    #define TestLed 13
    
    String cardUID ;
    #define SS_RF 10
    #define RST 5
    #define SS_NRF 53
    MFRC522 mfrc522(SS_RF, RST);   // Create MFRC522 instance.
    
    
    void setup()
    { pinMode(EnginePin, OUTPUT);
      pinMode(StartPin, OUTPUT);
      pinMode(Set90Pin, OUTPUT);
      pinMode(Set60Pin, OUTPUT);
      Serial.begin(115200);   // Initiate a serial communication
      SPI.begin();      // Initiate  SPI bus
    
      radio.setAddress(0xD2);
      radio.begin(40, 53);
      radio.setChannel(124);
      radio.setRetries(0, 15);
      radio.setActive(true);
      pinMode(SS_RF, OUTPUT);
      pinMode(SS_NRF, OUTPUT);
      pinMode(TestLed, OUTPUT);
      digitalWrite(EnginePin, LOW);
      digitalWrite(StartPin, LOW);
      digitalWrite(Set90Pin, LOW);
      digitalWrite(Set60Pin, LOW);
      digitalWrite(TestLed, LOW);
    }
    void loop()
    {
      radio.startListening();
    
      if (CountdownState == 1) {
        CountDown();
      }
    
    
      // Look for new cards
      if ( ! mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial())
      {
        return;
      }
    
      //Show UID on serial monitor
      Serial.print("UID tag :");
      String content = "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase();
    
    
      if (content.substring(1) == "E0 0A DD A4" || content.substring(1) == "C0 57 FE A7" || content.substring(1) == "B0 AB 9F A5" || content.substring(1) == "50 51 1B A4" )
      {
        if (EngineState == 0) {
          
          digitalWrite(EnginePin, HIGH);
          Serial.print(F("Sending Engine Code... "));
          bool sent = radio.send(0xD3, "E0 0A DD A4");
          bool sent2 = radio.send(0xD4, "E0 0A DD A4");
          bool sent3 = radio.send(0xD5, "E0 0A DD A4");
          bool sent4 = radio.send(0xD6, "E0 0A DD A4");
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          EngineState = 1;
          delay(50);
        }
    
        if (EngineState == 1) {
          bool sent = radio.send(0xD3, "E0 0A DD A4");
          bool sent2 = radio.send(0xD4, "E0 0A DD A4");
          bool sent3 = radio.send(0xD5, "E0 0A DD A4");
          bool sent4 = radio.send(0xD6, "E0 0A DD A4");
    
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          delay(100);
        }
      }
    
      if ((EngineState != 0) && (content.substring(1) == "CD 9E 08 75" || content.substring(1) == "7D A6 4F 75" || content.substring(1) == "8D 2B 55 75" || content.substring(1) == "DD D1 6C 3E" ))
      {
        if (CountdownState == 0) {
          Serial.println("Countdown Started");
          digitalWrite(StartPin, HIGH);
          delay(50);
          digitalWrite(StartPin, LOW);
          Serial.print(F("Sending Start Code... "));
          bool sent = radio.send(0xD3, "CD 9E 08 75");
          bool sent2 = radio.send(0xD4, "CD 9E 08 75");
          bool sent3 = radio.send(0xD5, "CD 9E 08 75");
          bool sent4 = radio.send(0xD6, "CD 9E 08 75");
    
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          CountdownState = 1;
        }
    
        if (CountdownState == 1) {
          Serial.print(F("Sending Start Code... "));
          bool sent = radio.send(0xD3, "CD 9E 08 75");
          bool sent2 = radio.send(0xD4, "CD 9E 08 75");
          bool sent3 = radio.send(0xD5, "CD 9E 08 75");
          bool sent4 = radio.send(0xD6, "CD 9E 08 75");
    
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          delay(100);
        }
    
      }
    
     
    
      if ((EngineState != 0) && (content.substring(1) == "7D 1C 65 5C" || content.substring(1) == "AD C2 5C 74"))
      {
        if (Set90State == 0) {
          
          digitalWrite(Set90Pin, HIGH);
          delay(50);
          digitalWrite(Set90Pin, LOW);
          Serial.print(F("Sending 90' Code... "));
          bool sent = radio.send(0xD3, "7D 1C 65 5C");
          bool sent2 = radio.send(0xD4, "7D 1C 65 5C");
          bool sent3 = radio.send(0xD5, "7D 1C 65 5C");
          bool sent4 = radio.send(0xD6, "7D 1C 65 5C");
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          Set90State = 1;
          delay(50);
    
        }
        else
        {
          
          bool sent = radio.send(0xD3, "7D 1C 65 5C");
          bool sent2 = radio.send(0xD4, "7D 1C 65 5C");
          bool sent3 = radio.send(0xD5, "7D 1C 65 5C");
          bool sent4 = radio.send(0xD6, "7D 1C 65 5C");
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          delay(100);
        }
      }
    
    
    
      if ((EngineState != 0) && (content.substring(1) == "20 C4 18 A4" || content.substring(1) == "1D AE 2C 75"))
      {
        if (Set60State == 0) {
          
          digitalWrite(Set60Pin, HIGH);
          delay(50);
          digitalWrite(Set60Pin, LOW);
         
          bool sent = radio.send(0xD3, "20 C4 18 A4");
          bool sent2 = radio.send(0xD4, "20 C4 18 A4");
          bool sent3 = radio.send(0xD5, "20 C4 18 A4");
          bool sent4 = radio.send(0xD6, "20 C4 18 A4");
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          Set60State = 1;
          delay(50);
    
        }
        else
        {
          
          bool sent = radio.send(0xD3, "20 C4 18 A4");
          bool sent2 = radio.send(0xD4, "20 C4 18 A4");
          bool sent3 = radio.send(0xD5, "20 C4 18 A4");
          bool sent4 = radio.send(0xD6, "20 C4 18 A4");
          Serial.println(sent ? "OK" : "failed");
          Serial.println(sent2 ? "OK" : "failed");
          Serial.println(sent3 ? "OK" : "failed");
          Serial.println(sent4 ? "OK" : "failed");
          delay(100);
        }
      }
    
    
      if (radio.available()) {
        char buf[32];
        uint8_t numBytes = radio.read(buf, sizeof(buf));
        Serial.print(F("Received: "));
        Serial.println(buf);
      }
    
    
    }
    
    
    void CountDown() {
      time_now = millis();
      value = 128 + 127 * cos(2 * PI / blinkled * time_now);
      analogWrite(TestLed, value);
      //Serial.println(value);
    }
    

    Do you have any idea to solve this?


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 5

67
Online

11.4k
Users

11.1k
Topics

112.7k
Posts