5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Arduino ESP32 と STM32 で NFC RFID-RC522

Last updated at Posted at 2020-03-07

(Felica/Mifare/NFC チャレンジシリーズ) その他の記事はこちら 「Felica/Mifare/NFC でいろいろ実験」
https://qiita.com/nanbuwks/items/1f416d6e45a87250ee0a


Arduino 開発環境で、 NFC の格安モジュール NFC RFID-RC522 を使ってみます。
IMG_20200304_153805.jpg

今回の実験では、動作安定性など、結構課題がありました。→ 2020/03/07 追記 STM32の動作安定性については電源供給を改善することで改善しました。

IMG_20200312_130200.jpg

詳細はこちら
「NFC モジュールの動作安定性を改善する」
https://qiita.com/nanbuwks/items/a9217ba2e8f206b899b1

ライブラリインストール

Arduino開発環境から、「ツール」-「ライブラリを管理...」でMFRC522 で検索、「MFRC522 by GithubCommunity」を選んで読み込みます。

ESP32

環境

ESP32 WEMOS Lolin32
https://qiita.com/nanbuwks/items/111ee8cd69f3390d866f
NFC RFID-RC522
Arduino 開発環境 1.8.10

配線

「ESP-WROOM32とRC522でRFIDお遊びしてみた」
https://qiita.com/kmhumd/items/c2dd0850f9deabb1787c
を参照して、以下のように配線しました。

WEMOS LOLIN32 RFID-RC522
3V3 3.3V
AI9/DAC2/26 RST
GND GND
(NC) IRQ
MISO/19 MISO
MOSI/23 MOSI
SCK/18 SCK
SS/5/LED SDA

ソフトウェア

「ファイル」-「スケッチ例」-「カスタムライブラリのスケッチ例」-「MFRC522」から「ReadNUID」 を呼び出します。
配線にあわせ、以下を変更します。


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

#define SS_PIN 10
#define RST_PIN 9


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

#define SS_PIN 5
#define RST_PIN 26

実行

スケッチは9600bpsの設定なので最初が文字化けしています。


I-U7⸮x⸮52I-U0
⸮i5-q555Te|⸮!&⸮%=%⸮⸮⸮This code scan the MIFARE Classsic NUID.
Using the following key: FF FF FF FF FF FFPICC type: MIFARE 1KB
A new card has been detected.
The NUID tag is:
In hex:  F2 C1 6B 33
In dec:  242 193 107 51

何故か最初の1回しか認識なかったりして、調べてみたらちょっと挙動が不安定でした。(先に挙げた電源の安定化をすれば改善するかも?)

カード内容のダンプ

同じようにして、サンプルプログラムの DumpInfo を実行してみます。

付属のカードを読ませたもの


)⸮
⸮ܠ⸮⸮J⸮⸮1⸮⸮⸮1!y⸮⸮M⸮⸮⸮ĥFirmware Version: 0x92 = v2.0
Scan PICC to see UID, SAK, type, and data blocks...
Card UID: F2 C1 6B 33
Card SAK: 08
PICC type: MIFARE 1KB
Sector Block   0  1  2  3   4  5  6  7   8  9 10 11  12 13 14 15  AccessBits
  15     63   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         62   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         61   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         60   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  14     59   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         58   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         57   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         56   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  13     55   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         54   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         53   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         52   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  12     51   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         50   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         49   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         48   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  11     47   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         46   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         45   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         44   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
  10     43   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         42   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         41   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         40   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   9     39   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         38   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         37   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         36   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   8     35   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         34   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         33   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         32   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   7     31   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         30   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         29   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         28   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   6     27   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         26   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         25   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         24   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   5     23   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         22   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         21   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         20   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   4     19   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         18   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         17   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         16   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   3     15   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         14   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         13   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
         12   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   2     11   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
         10   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          9   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          8   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   1      7   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          6   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          5   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          4   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
   0      3   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          2   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          1   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          0   F2 C1 6B 33  6B 08 04 00  62 63 64 65  66 67 68 69  [ 0 0 0 ] 


先程の ReadNUID は、ちょっと不安定でしたがこれは安定性はチェックしてません。

今度は、書き込みをしてみます。
同じようにして、サンプルプログラムのReadAndWriteを書き換えて実行します。こちらはかなり安定して動きました。


)%Mo⸮^ <⸮}!~))⸮Md⸮!⸮	⸮	J⸮Scan a MIFARE Classic PICC to demonstrate read and write.
Using key (for A and B): FF FF FF FF FF FF
BEWARE: Data will be written to the PICC, in sector #1
Card UID: F2 C3 2B 33
PICC type: MIFARE 1KB
Authenticating using key A...
Current data in sector:
   1      7   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          6   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          5   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          4   01 02 03 04  05 06 07 08  09 0A FF 0B  0C 0D 0E 0F  [ 0 0 0 ] 

Reading data from block 4 ...
Data in block 4:
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F

Authenticating again using key B...
Writing data into block 4 ...
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F

Reading data from block 4 ...
Data in block 4:
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F
Checking result...
Number of bytes that match = 16
Success :-)

Current data in sector:
   1      7   00 00 00 00  00 00 FF 07  80 69 FF FF  FF FF FF FF  [ 0 0 1 ] 
          6   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          5   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  [ 0 0 0 ] 
          4   01 02 03 04  05 06 07 08  09 0A FF 0B  0C 0D 0E 0F  [ 0 0 0 ] 

書けているみたいです。書き込み前のデータは既に01 02 03 04 ... となっていますがこれは元々 00 00 00... となっているところに書き込んだときの1回目のログが消えてしまって再度ログを取り直したためです。

STM32

環境

RobotDyn STM32F103 Dev.Board
https://qiita.com/lowlevel/items/47b0a8ba17f8afc473a6
NFC RFID-RC522
Arduino_STM32 https://github.com/rogerclarkmelbourne/Arduino_STM32
Arduino 開発環境 1.8.10

配線

IMG_20200306_155431.jpg

SPI1を使います
PA3にしてみました

STM32 Dev. BOARD RFID-RC522 スケッチ上の記述
3V3 3.3V
PA3 RST RST_PIN
GND GND
(NC) IRQ
PA6 MISO
PA7 MOSI
PA5 SCK
PA4 SDA SS_PIN

また、今回はUSBシリアルモジュールを別につなげてシリアルモニターをつなげてみました。
cf.,「STM32 を Arduino 開発環境で使う場合のシリアルポートの調査 (STM32duino + STM32F103C8 : Blue Pill)」
https://qiita.com/nanbuwks/items/5a01b924b192d5d36b31

STM32 Dev. BOARD USBシリアル
PA9(TX) RX
PA10(RX) TX
GND GND

ソフトウェア

「ファイル」-「スケッチ例」-「カスタムライブラリのスケッチ例」-「MFRC522」から「ReadNUID」 を呼び出します。
配線にあわせ、以下を変更します。


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

#define SS_PIN 10
#define RST_PIN 9


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

#define SS_PIN PA4
#define RST_PIN PA3

また、今回ST-Linkで書き込むためこのPA9,PA10に割り当てられているシリアルオブジェクトはSerial1になります。

なのでソースコード中のSerial を Serial1に書き換えました

実行

結構、不安定です。連続して読ませたりすると動きがヘンになったりします。→ 2020/03/07 追記 電源供給の改善をすることでほぼ問題は解消しました。確認していませんが、この記事の他の動作安定性についても恐らく電源供給でクリアするのではないかな?


This code scan the MIFARE Classsic NUID.
Using the following key: FF FF FF FF FF FFPICC type: MIFARE 1KB
A new card has been detected.
The NUID tag is:
In hex:  F2 C3 2B 33
In dec:  242 195 43 51

読めました。

同じようにして、サンプルプログラムの DumpInfo をコンパイルしてみましたが結果を取得できませんでした。ライブラリが Serial のみに記述しているかららしいです。

ReadAndWrite の方は、同様に書き換え、実行で以下のように動きました。最初、あれれ〜動かないな・・・と思ったらこれはかなり動作が不安定です。実験を繰り返して、調子の良い実験結果は以下のようになりました。(不安定さについては先に挙げた電源の安定化対策で改善しました。)


Scan a MIFARE Classic PICC to demonstrate read and write.
Using key (for A and B): FF FF FF FF FF FF
BEWARE: Data will be written to the PICC, in sector #1
Card UID: F2 C1 6B 33
PICC type: MIFARE 1KB
Authenticating using key A...
Current data in sector:

Reading data from block 4 ...
Data in block 4:
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F

Authenticating again using key B...
Writing data into block 4 ...
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F

Reading data from block 4 ...
Data in block 4:
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F
Checking result...
Number of bytes that match = 16
Success :-)

Current data in sector:

こちらも、既に書き換え終わったカードを使った実験だったので、変化はないように見えてます。

以下のデータのところを、


    byte dataBlock[]    = {
        0x01, 0x02, 0x03, 0x04, //  1,  2,   3,  4,
        0x05, 0x06, 0x07, 0x08, //  5,  6,   7,  8,
        0x09, 0x0a, 0xff, 0x0b, //  9, 10, 255, 11,
        0x0c, 0x0d, 0x0e, 0x0f  // 12, 13, 14, 15
    };

    byte dataBlock[]    = {
        0x0f, 0x0e, 0x0d, 0x0c,
        0x0b, 0x0a, 0x09, 0x08,
        0x07, 0x06, 0x05, 0x03,
        0x03, 0x02, 0x01, 0x00
            };

のように書き換えて実行したら、ちゃんと書き換えていることがわかりました。


Scan a MIFARE Classic PICC to demonstrate read and write.
Using key (for A and B): FF FF FF FF FF FF
BEWARE: Data will be written to the PICC, in sector #1
Card UID: F2 C1 6B 33
PICC type: MIFARE 1KB
Authenticating using key A...
Current data in sector:

Reading data from block 4 ...
Data in block 4:
 01 02 03 04 05 06 07 08 09 0A FF 0B 0C 0D 0E 0F

Authenticating again using key B...
Writing data into block 4 ...
 0F 0E 0D 0C 0B 0A 09 08 07 06 05 03 03 02 01 00

Reading data from block 4 ...
Data in block 4:
 0F 0E 0D 0C 0B 0A 09 08 07 06 05 03 03 02 01 00
Checking result...
Number of bytes that match = 16
Success :-)

Current data in sector:

Current data in sector:
の後ろが出ていませんね。これは、プログラムの該当箇所が


    mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);

となっていて、Serialに出力するようになっているためです。ここは悪影響を防ぐためにコメントアウトしました。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?