前言
本文将会一步一步实现一个门禁系统,使用 RFID 通信(NFC可以和它是相互兼容的)。也就是说用手机(支持 NFC)或者REID标签就可以实现开锁。
效果演示
【arduino门禁系统】 https://www.bilibili.com/video/BV1eg411p7kV/?share_source=copy_web&vd_source=041c9610a29750f498de1bafe953086b
硬件材料
材料 | 价格(rmb) | 数量 |
---|---|---|
Arduino uno | ¥26左右 | 1 |
RFID-RC522 | ¥6左右 | 1 |
舵机 MG996R | ¥17左右 | 1 |
杜邦线若干 | ¥2左右 | - |
面包板一个 | ¥5左右 | - |
下文中使用的舵机不是 MG996R ,使用的是一个小舵机,扭力不足以开门。但和 MG996R 使用都是一样的。
软件材料
1、Arduino IDE
2、安装 MFRC522 依赖
注意:是我圈的那一个
RC522 接线
RC522 | Arduino |
---|---|
3.3v | 3.3v |
GND | GND |
RST | 9 |
SDA | 10 |
MOSI | ICSP-4 |
MISO | ICSP-1 |
SCK | ICSP-3 |
ICSP 引脚别插错了,根据下图来。
舵机 接线
舵机 | Arduino |
---|---|
红色 | 5v |
棕色 | GND |
黄色 | 8 |
代码编写
/*
* 舵机接线 arduino
* 黄色 8
* 红色 5v
*
* =========
*
* MFRC522 arduino
* 3.3v 3.3v
* RST/Reset RST 9
* SPI SS SDA(SS) 10
* SPI MOSI MOSI 11 / ICSP-4 , 本项目用 ICSP
* SPI MISO MISO 12 / ICSP-1 , 本项目用 ICSP
* SPI SCK SCK 13 / ICSP-3 , 本项目用 ICSP
*/
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
byte nuidPICC[4];
// 把标签中存储的数据在这里定义好,如果能比对上就说明可以开锁
// 十进制卡密 83 191 16 25 ,下面直接用二进制会更方便
byte str0 = 0x53;
byte str1 = 0xBF;
byte str2 = 0x10;
byte str3 = 0x19;
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(8);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println(F("This code scan the MIFARE Classsic NUID."));
Serial.print(F("Using the following key:"));
printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
void loop() {
// 检测是否有标签存在
if (!rfid.PICC_IsNewCardPresent())
return;
// 检测标签是否被读取过了
if (!rfid.PICC_ReadCardSerial())
return;
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));
// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.print(F("ID: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
Serial.print(F("2 ID: "));
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
if (nuidPICC[0] == str0 && nuidPICC[1] == str1 && nuidPICC[2] == str2 && nuidPICC[2] == str2 && nuidPICC[3] == str3) {
Serial.println(" hello ");
// 密码验证成功
myservo.write(179);
delay(1500);
// 1.5秒后关门
myservo.write(0);
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
char str1[100];
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
// strcat(str1, str2);
}
}
/**
* Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
return;
}