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.


 

327
Online

7.4k
Users

8.3k
Topics

89.6k
Posts