目录
简介
了解如何使用Arduino和非常基本的电子组件在家里用RFID锁制作安全的储物柜。
步骤一 材料准备
硬件准备:
arduino R3
RFID阅读器
12v继电器
电磁锁
12v 2A电源
1kΩ电阻
NPN三极管
led
8mm MDF板
软件准备:
arduino IDE
步骤二 原理说明
我们采用NFC设备来制作锁,通过NFC装置控制继电器的开关。继电器打开则电磁锁工作,产生强磁吸附门;继电器断开电磁锁不工作,门可以轻易拉开。
步骤三 电路搭建
根据原理图进行制作焊接:焊好如下所示
步骤四 编写程序
现在该进行编程了。在这里,您需要两个程序来使其运行。首先,您需要找出钥匙卡的UID。找出UID上传优先程序,
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_1_PIN 10 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
#define SS_2_PIN 8 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
#define NR_OF_READERS 2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PCwhile (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI busfor (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
Serial.print(F("Reader "));
Serial.print(reader);
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
}
}
/**
* Main loop.
*/
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cardsif (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
Serial.print(F("Reader "));
Serial.print(reader);
// Show some details of the PICC (that is: the tag/card)
Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
现在打开串行监视器并将卡放在RFID读取器上,该卡的唯一ID将出现在串行监视器副本上或将该ID记下来。
将第二个程序上传到arduino。
/*
program by Shubham Shinganapure on 14-07-2019
for RFID Safe Locker
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_1_PIN 10 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
#define SS_2_PIN 8 // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
#define NR_OF_READERS 2
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
int ledRed = A1;
int rly = A3;
const int ledBlue = A2;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 90;
int flg=0;
void setup() {
pinMode(ledRed,OUTPUT);
pinMode(ledBlue,OUTPUT);
pinMode(rly,OUTPUT);
SPI.begin(); // Init SPI busfor (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card//Serial.print(F("Reader "));// Serial.print(reader);//Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
}
}
/**
* Main loop.
*/
int i =0 ;
char arry2[5] = {0x06,0xF8,0x51,0x1B,0}; // put your cards ID here. for example my card ID is 06,F8,51,1B so write it by this way{0x06,0xF8,0x51,0x1B,0} char arry[5] = {0};
void loop() {
Blink();
readTag();
if(strncmp(arry2,arry,4) == 0)
{
digitalWrite(rly,HIGH);
digitalWrite(ledBlue,HIGH);
delay(5000);
digitalWrite(rly,LOW);
digitalWrite(ledBlue,LOW);
//arry[5] = {0};memset(arry,0,5);
flg=0;
}
if(flg==1 && (strncmp(arry2,arry,4) != 0))
{
digitalWrite(ledBlue,LOW);
wrongID();
memset(arry,0,5);
flg=0;
}
}
void wrongID()
{
digitalWrite(ledRed,HIGH);
delay(500);
digitalWrite(ledRed,LOW);
delay(500);
digitalWrite(ledRed,HIGH);
delay(500);
digitalWrite(ledRed,LOW);
delay(500);
digitalWrite(ledRed,HIGH);
delay(500);
digitalWrite(ledRed,LOW);
delay(500);
digitalWrite(ledRed,HIGH);
delay(500);
digitalWrite(ledRed,LOW);
delay(500);
digitalWrite(ledRed,HIGH);
delay(500);
digitalWrite(ledRed,LOW);
}
void readTag()
{
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new cardsif (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
// Serial.print(F("Reader "));// Serial.print(reader);// Show some details of the PICC (that is: the tag/card)//Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
//Serial.println();for(i = 0;i < 4;i++)
{
//Serial.print(mfrc522[reader].uid.uidByte[i],HEX);
arry[i] = mfrc522[reader].uid.uidByte[i];
flg=1;
//Serial.println(arry[i],HEX);
}
for(i = 0;i < 4;i++)
//Serial.print(arry[i],HEX);// Serial.println();//Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
//Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));// Halt PICC
mfrc522[reader].PICC_HaltA();
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
} //if (mfrc522[reader].PICC_IsNewC
} //for(uint8_t reader
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
//Serial.print(buffer[i] < 0x10 ? " 0" : " ");//Serial.print(buffer[i], HEX);
}
}
void Blink() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledBlue, ledState);
}
}
现在,将先前复制的UID放在此程序的第39行中,然后将其上传。
全部做完!谢谢!
步骤五 验证结果
接下来试一下门刷卡前是否锁上了,刷卡后门是否打开了。