Arduino Nano RFID-RC522 类库使用

一、前言

Arduino Nano 结合 RFID-RC522 实现M1卡读写。

二、接线

越是基础越是别出错,有时候出问题半天解决不了,最后发现线接错了。所以新手请注意。接线如下图
接线示意图(图片来自网络)
![图片来自网络]

没有OLED的不必理会

根据不同颜色的杜邦线方便区分

RFID-RC522 Nano Color
3.3V 3.3V RED
RST PIN9 YELLOW
GND GND ORANGE
RQ
MISO PIN12 BROWN
MOSI PIN11 BLACK
SCK PIN13 WHITE
SDA PIN10 GRAY

三、代码

在编写代码之前我们需要添加 RC522 对应的库。https://www.arduinolibraries.info/libraries/mfrc522

下载你喜欢的版本(我一般下最新版)
![mfrc522 library

然后打开 Arduino >> Sketch >> Include Library >> Add .ZIP library..

选择刚刚下载的库文件如 MFRC522-1.4.4.zip 进行类库的添加,类库添加成功之后我们可以看看第三方提供的 Example , Arduino >> File >> Example >> MFRC522
然后选择其中一个案例如 ReadAndWrite

ps: 发现文档中已有接线说明

 * 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

四、上载程序

打开Arduino IDE >> TOOLS 下选择对应的 board 以及 port

点击上载程序按钮,这里需要注意的是,如果一直无法上传成功的话。报以下错误

Sketch uses 8566 bytes (27%) of program storage space. Maximum is 30720 bytes.
Global variables use 307 bytes (14%) of dynamic memory, leaving 1741 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
An error occurred while uploading the sketch

尝试将 Processor 这里切换为 ATmega328P(Old Bootloader)

需要注意的是 程序默认使用的 Key A 为 FFFFFFFFFFFF 所以请确认你的卡片Key A 与其对应

未经允许不得转载