Arduino 门禁系统制作

在这里插入图片描述

前言

本文将会一步一步实现一个门禁系统,使用 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 依赖

注意:是我圈的那一个

MFRC522.PNG

RC522 接线

RC522Arduino
3.3v3.3v
GNDGND
RST9
SDA10
MOSIICSP-4
MISOICSP-1
SCKICSP-3

ICSP 引脚别插错了,根据下图来。
ICSP.PNG

舵机 接线

舵机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;
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小明IO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值