Arduino 门禁系统制作

Arduino 门禁系统制作

前言

本文将会一步一步实现一个门禁系统,使用 RFID 通信(NFC可以和它是相互兼容的)。也就是说用手机(支持 NFC)或者REID标签就可以实现开锁。

效果演示

【arduino门禁系统】 https://www.bilibili.com/video/BV1eg411p7kV/?share_source=copy_web&vd_source=041c9610a29750f498de1bafe953086b

硬件材料

材料价格(rmb)数量Arduino uno¥26左右1RFID-RC522¥6左右1舵机 MG996R¥17左右1杜邦线若干¥2左右-面包板一个¥5左右-下文中使用的舵机不是 MG996R ,使用的是一个小舵机,扭力不足以开门。但和 MG996R 使用都是一样的。

软件材料

1、Arduino IDE

2、安装 MFRC522 依赖

注意:是我圈的那一个

RC522 接线

RC522Arduino3.3v3.3vGNDGNDRST9SDA10MOSIICSP-4MISOICSP-1SCKICSP-3ICSP 引脚别插错了,根据下图来。

舵机 接线

舵机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

#include

#include

#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;

}

相关新闻

长条pvc吊顶怎么拆开教程
帕尼尼球星卡FIFA365

长条pvc吊顶怎么拆开教程

🕒 07-27 👽 6820
iPhone Screenshots
365体育官网登录

iPhone Screenshots

🕒 08-04 👽 9863
谲秘的意思
帕尼尼球星卡FIFA365

谲秘的意思

🕒 08-14 👽 7441
白领借款放款多久到账
365bet足球直播

白领借款放款多久到账

🕒 07-22 👽 1853
【2025年最新版】爱马仕包款人气排行榜 TOP10
365bet足球直播

【2025年最新版】爱马仕包款人气排行榜 TOP10

🕒 08-11 👽 3239
幻化的意思
帕尼尼球星卡FIFA365

幻化的意思

🕒 07-22 👽 7592