2018年8月1日 星期三

Arduino 運用RC522模組,讀取UID,根據不同的卡片改變LED顏色

本章是RFID最簡單的應用,透過判讀RFID中的UID,讓LED閃爍對應的顏色
手上有三張卡片
Card 1.大白卡(hex:  B6 55 16 DB)  >> 閃綠光
Card 2.藍色鎖匙扣(hex:  0C 8E 5A 4B )  >> 閃紅光
Card 3.撿到的停車卡(hex:  35 93 FC 1B )  >> 閃黃光




實際實驗環境

Arduino NANO
RC522 RFID模組
Card 1.大白卡(hex:  B6 55 16 DB)
Card 2.藍色鎖匙扣(hex:  0C 8E 5A 4B )
Card 3.撿到的停車卡(hex:  35 93 FC 1B ) 



參考資料
本次應用的函式庫網址如下

請將網址資料打包放至如下資料夾
"C:\Users\___XXXX___\Documents\Arduino\libraries"

接線方式

今天採用的是Arduino NANO + RC522 RFID模組
RC522式透過SPI與Arduino溝通的,接線方式如其他章節

不同的是,在這個實驗中加入WS2812的RGB LED燈條,接線方式如下
******************接線方式******************
NANO                   WS2812燈條

+5V------------------VCC
D6------------------DIN
GND------------------GND
********************************************

測試程式
本次是用函式庫中的Dumpinfo範例做修改,讀取RFID Card中的資料
判斷是哪一張卡片,閃爍對應的顏色
/*
 * --------------------------------------------------------------------------------------------------------------------
 * 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>
#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      6
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


constexpr uint8_t RST_PIN = 9;          // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10;         // Configurable, see typical pin layout above
int LED_i=0;

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..."));

  pixels.begin(); // This initializes the NeoPixel library.
 }

void loop() {
  
  //***************RGB LED WS2812***************//
  if(LED_i<6){  pixels.setPixelColor(LED_i, pixels.Color(0,0,20));   }
  else{   pixels.setPixelColor(LED_i-6, pixels.Color(0,0,0));   }
  LED_i++;
  if(LED_i>11)LED_i=0;
  
   //pixels.setPixelColor(i, pixels.Color(0,50,0)); // Moderately bright green color.
   pixels.show(); // This sends the updated pixel color to the hardware.
   delay(200);
  //***************RFID***************//
   
// 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));
  
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println(F(" "));

  //***************check RFID card***************//
  //CARD 1
  if( mfrc522.uid.uidByte[0]==0xB6 & mfrc522.uid.uidByte[1]==0x55 & 
      mfrc522.uid.uidByte[2]==0x16 & mfrc522.uid.uidByte[3]==0xDB  )
  {
    RGB_All_Flash(0, 50, 0 );     // Green
  }

  //CARD 2
  if( mfrc522.uid.uidByte[0]==0x0C & mfrc522.uid.uidByte[1]==0x8E & 
      mfrc522.uid.uidByte[2]==0x5A & mfrc522.uid.uidByte[3]==0x4B  )
  {
    RGB_All_Flash(50, 0, 0 );     // Red
  }
  
  //CARD 3
  if( mfrc522.uid.uidByte[0]==0x35 & mfrc522.uid.uidByte[1]==0x93 & 
      mfrc522.uid.uidByte[2]==0xFC & mfrc522.uid.uidByte[3]==0x1B  )
  {
    RGB_All_Flash(30, 30, 0 );    //Yellow
  }
  
}
/**
 * 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);
  }
}

void RGB_All_Flash(int All_Flash_R, int All_Flash_G, int All_Flash_B){

    for(LED_i=0; LED_i<NUMPIXELS; LED_i++)
    {  pixels.setPixelColor(LED_i, pixels.Color(0,0,0));    }
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);
    
    for(LED_i=0; LED_i<NUMPIXELS; LED_i++)
    {  pixels.setPixelColor(LED_i, pixels.Color(All_Flash_R, All_Flash_G, All_Flash_B));    }
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);
    
    for(LED_i=0; LED_i<NUMPIXELS; LED_i++)
    {  pixels.setPixelColor(LED_i, pixels.Color(0,0,0));    }
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);

    for(LED_i=0; LED_i<NUMPIXELS; LED_i++)
    {  pixels.setPixelColor(LED_i, pixels.Color(All_Flash_R, All_Flash_G, All_Flash_B));    }
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);
    
    for(LED_i=0; LED_i<NUMPIXELS; LED_i++)
    {  pixels.setPixelColor(LED_i, pixels.Color(0,0,0));    }
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);
  
}




實際測試影片




實際回傳的畫面

讀取到的是卡片的UID,下方是卡片16個扇區的資料,這張是模組附贈的白卡,所以看起來沒什麼東西


未來可行的應用
  • 製作門禁系統
  • 透過讀取RFID TAG做相對應的功能
  • 保全人員簽到用




沒有留言:

張貼留言

專題許願池,請不要吝嗇將你想製作的專題用回覆的方式寫在下方

專題許願池 開這個板是為了幫助一些人,有專題製作需求,但是又不知道要如何實現 如果您有想製作的專題,請您將您的題目回覆在下方,先提出您的需求,進而討論可行性或製做的方向,或是您進行到什麼階段,有遇到什麼樣的問題也可以提出來討論,如果我知道會盡量幫助您順利完成。 不限...