【问题标题】:RFID-RC522 not reading cardRFID-RC522不读卡
【发布时间】:2016-11-05 22:20:32
【问题描述】:

我有一个 RFID-RC522 (MF-RC522) 模块,我正在使用 Arduino 草图程序。我已经下载了示例代码:

/*
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read data from a 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 a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
 * Reader on the Arduino SPI interface.
 * 
 * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
 * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
 * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
 * will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
 * when removing the PICC from reading distance too early.
 * 
 * If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
 * So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
 * details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
 * keep the PICCs at reading distance until complete.
 * 
 * @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      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
 */

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

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

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
    mfrc522.PCD_Init();     // Init MFRC522
    mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
    Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
    }

    // Dump debug info about the card; PICC_HaltA() is automatically called
    mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}

当我执行它时,我得到消息:

固件版本:0x0 =(未知)

WARNING:通讯失败,MFRC522是否正确连接?

扫描 PICC 以查看 UID、SAK、类型和数据块...

我已经重新检查了一千次连接,但它不起作用,引脚已按照示例所述连接,并且 LED D1 亮起(红色)。有谁能帮帮我吗?

【问题讨论】:

  • 使用的是哪个 Arduino?
  • 我有 uno 和 Mega。两者都不起作用
  • 您确定 SS 引脚已插入 SDA?
  • 我遵循了上面的那些数字。所以它们如下:RST ->9, SDA ->10, MOSI -> 11, MISO -> 12, SCK -> 13
  • 这一切看起来都不错。而且你的 3.3v 和接地线太正确了?

标签: arduino rfid


【解决方案1】:

如果有人在寻找解决方案...

步骤 1:如果您使用 Arduino Uno 或在 Arduino Mega 中使用 RST 引脚 9,请将 RST 引脚更改为 5。其他引脚保持不变。
第二步:下载AddicoreRFID库。
步骤 3:打开 Arduino IDE 并导航到 Sketch > Include the Library 使用“Add .ZIP Library...”选项。
步骤 4:重新启动 Arduino IDE(关闭并再次打开)
第 5 步:在 Arduino IDE 中导航到文件 > 示例 > AddicoreRFID。选择“Addicore_RFID_Example”草图并上传草图
第 6 步:打开 Serial 窗口并选择“Both NL & CR”,然后选择“9600”作为波特率选项

详细解释可以在link找到

【讨论】:

    【解决方案2】:

    此问题是由于 RFID 传感器接线不正确或接触松动所致。您需要焊接触点才能获得结果。

    另外,为您的 Arduino 板选择合适的 PIN。

    【讨论】:

      【解决方案3】:

      UNO 解决方案与我合作

      • 重置 Bin 是 5 而不是 10
      • 使用 5V 而不是 3.3v

      Arduino forum Solution with explanation

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      【解决方案4】:

      试试这个代码

      
           #include <RFID.h>
      
          /*
          * Pin layout should be as follows (on Arduino Uno):
          * MOSI: Pin 11 / ICSP-4
          * MISO: Pin 12 / ICSP-1
          * SCK: Pin 13 / ISCP-3
          * SS/SDA: Pin 10
          * RST: Pin 9
          */
      
          #include <SPI.h>
          #include <RFID.h>
      
          #define SS_PIN 10
          #define RST_PIN 9
      
          RFID rfid(SS_PIN,RST_PIN);
      
      
          int self = 7;
          int bat = 8; 
          int s = 6;
      
          int serNum[5];
          int cards[][5] = {
            {42,249,70,213,64}
      
          };
      
          bool access = false;
      
          void setup(){
      
              Serial.begin(9600);
              SPI.begin();
              rfid.init();
      
              pinMode(self, OUTPUT);
              pinMode(bat ,OUTPUT);
              pinMode(s,OUTPUT);
      
      
              digitalWrite(self, LOW);
              digitalWrite(bat , LOW);
      
          }
      
          void loop(){
      
              if(rfid.isCard()){
      
                  if(rfid.readCardSerial()){
                      Serial.print(rfid.serNum[0]);
                      Serial.print(" ");
                      Serial.print(rfid.serNum[1]);
                      Serial.print(" ");
                      Serial.print(rfid.serNum[2]);
                      Serial.print(" ");
                      Serial.print(rfid.serNum[3]);
                      Serial.print(" ");
                      Serial.print(rfid.serNum[4]);
                      Serial.println("");
      
                      for(int x = 0; x < sizeof(cards); x++){
                        for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                            if(rfid.serNum[i] != cards[x][i]) {
                                access = false;
                                break;
                            } else {
                                access = true;
                            }
                        }
                        if(access) break;
                      }
      
                  }
      
                 if(access){
                    Serial.println("Welcome!");
                    digitalWrite(bat,HIGH);
                    digitalWrite(self,HIGH);
                    delay(1000);
                    digitalWrite(self,LOW);
      
                } else {
                     Serial.println("Not allowed!");
                    digitalWrite(s,HIGH);
                    delay(2000);
                    digitalWrite(s,LOW); 
                    digitalWrite(bat,LOW);
      
                 }        
              }
      
      
      
              rfid.halt();`enter code here`
      
          }
      

      【讨论】:

      • 虽然此代码 sn-p 可以解决问题,包括解释 really helps 以提高您的帖子质量。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      相关资源
      最近更新 更多