docs

a slatepencil documentail site

View on GitHub

RFID

hardware

wire

RFID Arduino Uno
3.3v 3.3v
GND GND
RST 2
MISO 3
MOSI 4
SCK 5
SDA 6
IRQ 7
I2C LCD1602 Arduino Uno
VCC 5V
GND GND
SDA A4
SCL A5
Relay Module Arduino Uno
VCC 5V
GND GND
SIG 8

code

/******************
 * get the id of RFID key
 Card type: MFOne-S50
 The card's number is: 90B8F325
 Card type: MFOne-S50
 The card's number is: 60830F53
 ******************/
#include "rfid1.h"
RFID1 rfid;      // create a variable type of RFID1
uchar serNum[5];  // arry to store the ID

void setup() {
  Serial.begin(9600);
  rfid.begin(7, 5, 4, 3, 6, 2);
  delay(100);
  rfid.init();
}

void loop() {
  uchar status;
  uchar str[MAX_LEN];
  status = rfid.request(PICC_REQIDL, str);
  if (status != MI_OK) {
    return;
  }
  rfid.showCardType(str);
  // Prevent conflict, return the 4 bytes Serial number of the card
  status = rfid.anticoll(str);
  if (status == MI_OK) {
    Serial.print("The card's number is: ");
    memcpy(serNum, str, 5);
    rfid.showCardID(serNum);
    Serial.println();
    Serial.println();
  }
  delay(500);
  rfid.halt();  // put card into sleep mode
}


/***************************
 * rfid entrance guard system
 * first get ght eid of RFID key by the getid.ino file, then divide the ID into four parts and fill them in loop() function
 * swipe the RFID key ring on the RFID module
 * if the password is correct, the normally open contact of the relay will be closed and the LCD will display a string "ID90B8F325" "Hello Arduino"
 * and then "Welcome" 2 seconds later;
 *
 * if the password is incorrect, the normally open contact of the relay will be disconnected and the LCD will display a string "Hello unknow guy".
 ***************************/
#include "rfid.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
RFID rfid;
#define relayPin 8
uchar serNum[5];

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  rfid.begin(7, 5, 4, 3, 6, 2);
  // rfid.begin(IRQ_PIN, SCK_PIN, MOSI_PIN, MISO_PIN, SDA_PIN, RST_PIN);
  delay(100);
  rfid.init();
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("    Welcome!    ");
  delay(2000);
}

void loop() {
  uchar status;
  uchar str[MAX_LEN];
  status = rfid.request(PICC_REQIDL, str);
  if (status != MI_OK) {
    return;
  }
  rfid.showCardType(str);
  status = rfid.anticoll(str);
  if (status == MI_OK) {
    lcd.setCursor(0, 0);
    lcd.print(" ID: ");
    memcpy(serNum, str, 5);
    rfid.showCardID(serNum);

    uchar* id = serNum;
    if (id[0] == 0x60 && id[1] == 0x83 && id[2] == 0x0F && id[3] == 0x53) {
      digitalWrite(relayPin, LOW);
      lcd.setCursor(0, 1);
      lcd.print(" Hello Jean! ");
      delay(2000);
      lcd.clear();
      digitalWrite(relayPin, HIGH);
    } else if (id[0] == 0x90 && id[1] == 0xB8 && id[2] == 0xF3 && id[3] == 0x25) {
      digitalWrite(relayPin, LOW);
      lcd.setCursor(0, 1);
      lcd.print("    Hello Arduino   ");
      delay(2000);
      lcd.clear();
      digitalWrite(relayPin, HIGH);
    } else {
      lcd.setCursor(0, 1);
      lcd.print("Hello unknow guy");
      delay(2000);
      lcd.clear();
    }
  }

  lcd.setCursor(0, 0);
  lcd.print("   Welcome!    ");
  delay(2000);
  rfid.halt();
}