Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Trouble with 2 SPI devices

  1. #1
    Member
    Join Date
    May 2017
    Location
    South Wales, UK
    Posts
    27

    Trouble with 2 SPI devices

    Hello peeps, I have been helping out a student working on a project with a Teensy 3.5, he is working on a two part device where a user scans in with an RFID card and then has to race to the other device and scan in to get a completion time. The Teensy 3.5 will eventually be used with an i2c screen, datalogging with the SD card and possibly some sound.

    He is using an RC522 scanner and a NRF24L01 module with a breakout I made - these are both SPI devices and we have code working for both with all hardware assembled on breadboard, but we are having difficulty getting the two to work together. The program at present is a bit of a hack, where recognised users are hard coded in (there are two for testing), when a recognised user is detected, the radio should send a simple packet out. At the moment the sketch will successfully read an RFID tag but freezes at the point the radio should send the message.

    I'm pretty sure there is a crossover issue between the SPI devices, the NRF24L01 has a CE pin and the RC522 has a SDA or SS pin, I think what is likely happening is the two libraries used are not disabling or enabling the devices at the appropriate times. I have tried forcing the NRF24L01 and the RC522 readers to enable/disable at the appropriate times using the enable pins and adding long delays, but have had no luck - code in its current form is below, any advice very welcome - though this project will most likely be picked up after Xmas now.

    Code:
     /* Typical RFID 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      SDA(SS)      10            53        D10        10               10
     * 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
    */
    
    /*
    Tx Rx Connections:
    
    Radio    Arduino
    CE    -> 8
    CSN   -> 7 (Hardware SPI SS)
    MOSI  -> 11 (Hardware SPI MOSI)  
    MISO  -> 12 (Hardware SPI MISO)
    SCK   -> 13 (Hardware SPI SCK)
    IRQ   -> No connection
    VCC   -> No more than 3.6 volts
    GND   -> GND
    */
    
    
    #include <SPI.h>
    #include <MFRC522.h>
    #include <NRFLite.h>
    
    //reader 1
    int SS_PIN = 15;
    int RST_PIN = 9;
    
    // Radio Variables
    byte chan = 1;
    byte radio_id = 1; // this radio's ID
    byte send_id = 0; // the ID of the receiver radio
    byte cepin = 8;
    byte csnpin = 7;
    byte radio_chan = 0; // please change this number to your group number
    
    NRFLite radio; // this allows us to talk to the library in the sketch
    
    ///
    
    MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
    
    MFRC522::MIFARE_Key key; 
    
    // Init array that will store new NUID 
    byte nuidPICC[4];
    
    void setup() { 
      Serial.begin(115200);
      SPI.begin(); // Init SPI bus
      rfid.PCD_Init(); // Init MFRC522 
    
      for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
      }
    
      Serial.println(F("This code scan the MIFARE Classsic NUID."));
      Serial.print(F("Using the following key:"));
      printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
    
      // Test the radio is connected properly:
      if (!radio.init(radio_id, cepin, csnpin, NRFLite::BITRATE2MBPS, radio_chan)) {
        Serial.println("Cannot communicate with radio");
        while(true); // Wait here forever.
      }
    }
     
    void loop() {
      digitalWrite(cepin, HIGH);
      int user;
      // Look for new cards
      if ( ! rfid.PICC_IsNewCardPresent())
        return;
    
      // Verify if the NUID has been read
      if ( ! rfid.PICC_ReadCardSerial())
        return;
    
      Serial.print(F("PICC type: "));
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
      Serial.println(rfid.PICC_GetTypeName(piccType));
    
      // Check is the PICC of Classic MIFARE type
      if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
        piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
        Serial.println(F("Your tag is not of type MIFARE Classic."));
        return;
      }
    
      if (rfid.uid.uidByte[0] != nuidPICC[0] || 
        rfid.uid.uidByte[1] != nuidPICC[1] || 
        rfid.uid.uidByte[2] != nuidPICC[2] || 
        rfid.uid.uidByte[3] != nuidPICC[3] ) {
        Serial.println(F("A new card has been detected."));
    
        // Store NUID into nuidPICC array
        // ** This is where the new UUID is stored in the background array - it will prevent the same card being read twice.
        for (byte i = 0; i < 4; i++) {
          nuidPICC[i] = rfid.uid.uidByte[i];
        }
    
        // ** This is the user identification that Aidan added
        if (nuidPICC[0] == 0x19 && 
        nuidPICC[1] == 0x5A &&
        nuidPICC[2] == 0xDA && 
        nuidPICC[3] == 0x2A ) {
          user = 1;
          Serial.println("User 1 Start Detected");
        }
    
        if (nuidPICC[0] == 0x39 && 
        nuidPICC[1] == 0xAF &&
        nuidPICC[2] == 0xD2 && 
        nuidPICC[3] == 0x2A ) {
          user = 2;
          Serial.println("User 2 Start Detected");
        }
       
        Serial.println(F("The NUID tag is:"));
        Serial.print(F("In hex: "));
        printHex(rfid.uid.uidByte, rfid.uid.size);
    //    Serial.println();
    //    Serial.print(F("In dec: "));
    //    printDec(rfid.uid.uidByte, rfid.uid.size);
        Serial.println();
    
        for (byte i = 0; i < 4; i++) {
          nuidPICC[i] = 0xFF;
        }
      }
      else Serial.println(F("Card read previously."));
    
      // Halt PICC
      rfid.PICC_HaltA();
    
      // Stop encryption on PCD
      rfid.PCD_StopCrypto1();
    
      digitalWrite(cepin, LOW);
    
      delay(3000);
      if(radio.send(send_id, &user, sizeof(user))) { // & must be used in front of variable
        Serial.println("...Success");
      }
      else {
        Serial.println("...Failed");
      }
      delay(3000);
    }
    
    
    /**
     * Helper routine to dump a byte array as hex values to Serial. 
     */
    void printHex(byte *buffer, byte bufferSize) {
      for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
      }
      
    //}
    
    /**
     * Helper routine to dump a byte array as dec values to Serial.
     */
    //void printDec(byte *buffer, byte bufferSize) {
    //  for (byte i = 0; i < bufferSize; i++) {
    //    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    //    Serial.print(buffer[i], DEC);
    //  }
    }

  2. #2
    Member
    Join Date
    May 2017
    Location
    South Wales, UK
    Posts
    27
    As term is about to start again I just want to give this a little bump - when we sit down to work on the project next I will go through very carefully to make sure the devices are enabled/disabled at appropriate times - any tips in the mean time would be very welcome

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •