主要内容:使用两个RFID电子标签。第一种电子标签可以实现舵机旋转(开门),LCD显示屏显示开锁成功,蜂鸣器发出短促滴滴声,绿色LED点亮;第二种电子标签不能使舵机旋转,LCD显示开锁失败,同时会触发蜂鸣器长时间警报,红色LED灯闪烁。没有标签被检测到时,LCD显示屏显示字符串,蓝色LED灯亮
硬件准备:
1、 Arduino Uno
2 、RFID MFRC522(淘宝有售,一般会赠送两个电子标签)
3、舵机模块
4、LCD I2C(注意:不是普通的LCD,是使用I2C集成的LCD)
5、 蜂鸣器
6、 LED灯
7、杜邦线若干
接线:
注意:需要使用面包板来拓展供电槽。建议:引出3.3v和5v供电线路。
3.3v给MFRC522供电
5v给LCD、舵机供电
PS:部分面包板,供电槽中间是不连通的
RFID MFRC522
Arduino | MFRC522 |
---|---|
pin 10 | SDA |
pin 13 | SCK |
pin 11 | MOSI |
pin 12 | MISO |
pin 9 | RST |
3.3 | vcc |
GND | GND |
LCD I2C
Arduino | LCD I2C |
---|---|
A4 | SDA |
A5 | SCK |
5V | VCC |
GND | GND |
舵机
Arduino | SG90 |
---|---|
pin6 | data(橙色) |
5V | VCC(红色) |
GND | GND(棕色) |
LED和蜂鸣器
Arduino | LED和蜂鸣器 |
---|---|
pin 2 | 蓝色led正极,负极接地 |
pin 3 | 绿色led正极,负极接地 |
pin 4 | 红色led正极,负极接地 |
pin 5 | 蜂鸣器正极,负极接地(如果蜂鸣器无法判断正负极,那就正反各接一次) |
编程实现
先获取任意一张卡片的标签号(作为合法开锁标签),需要使用Arduino IDE的串口工具,程序如下:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
String UIDCard = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Scan your RFID Card:");
for (int i = 0; i < 20; i++) {
Serial.print(".");
delay(50);
}
Serial.println("");
}
void loop() {
//Wait until new tag is available
while (getUID())
{
Serial.print("UID: ");
Serial.println(UIDCard);
for (int i = 0; i < 20; i++) {
Serial.print(".");
delay(50);
}
delay(3000);
}
}
boolean getUID()
{
if (! mfrc522.PICC_IsNewCardPresent()) {
//Serial.println("card Not found");
return false;
}
if (! mfrc522.PICC_ReadCardSerial()) {
//Serial.println("Not able to read the card");
return false;
}
UIDCard = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UIDCard.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
UIDCard.concat(String(mfrc522.uid.uidByte[i], HEX));
}
UIDCard.toUpperCase();
UIDCard = UIDCard.substring(1);
mfrc522.PICC_HaltA();
return true;
}
注意:需要添加MFRC522库
Arduino添加库有点慢,需要耐心等待,可能需要科学上网
编译成功后,打开串口工具,波特率为9600,即可查看到返回的标签值。
保存该标签值
具体功能实现代码
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Wire.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
String MasterTag = "D3 F4 17 14"; // Enter you tag UID which we get it from first code.
String UIDCard = "";
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servo;
#define BlueLED 2
#define GreenLED 3
#define RedLED 4
#define Buzzer 5
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
lcd.init();
lcd.backlight();
lcd.clear();
servo.attach(6);
servo.write(10);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(Buzzer, OUTPUT);
digitalWrite(BlueLED, HIGH);
lcd.setCursor(0,0);//设置显示位置
lcd.print("424 Sharing Center");//显示字符数据
lcd.setCursor(0,1);//设置显示位置
lcd.print("Designed by Bzk");//显示字符数据
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(BlueLED, HIGH);
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, LOW);
noTone(Buzzer);
servo.write(10);
//Wait until new tag is available
while (getUID())
{
Serial.print("UID: ");
Serial.println(UIDCard);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Detect a Card");
lcd.setCursor(0, 1);
if (UIDCard == MasterTag)
{
lcd.print("Sharing!");
digitalWrite(GreenLED, HIGH);
digitalWrite(BlueLED, LOW);
digitalWrite(RedLED, LOW);
servo.write(100);
delay(50);
for (int i = 0; i < 2; i++) {
tone(Buzzer, 2000);
delay(250);
noTone(Buzzer);
delay(250);
}
}
else
{
lcd.print("Controling");
digitalWrite(BlueLED, LOW);
digitalWrite(GreenLED, LOW);
tone(Buzzer, 2000);
servo.write(10);
for(int i = 0; i < 10;i++){
digitalWrite(RedLED, HIGH);
delay(250);
digitalWrite(RedLED, LOW);
delay(250);
}
noTone(Buzzer);
}
delay(2000);
lcd.clear();
lcd.print("424 Charging STA");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card>>");
}
}
boolean getUID()
{
if (! mfrc522.PICC_IsNewCardPresent()) {
//Serial.println("card Not found");
return false;
}
if (! mfrc522.PICC_ReadCardSerial()) {
//Serial.println("Not able to read the card");
return false;
}
UIDCard = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UIDCard.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
UIDCard.concat(String(mfrc522.uid.uidByte[i], HEX));
}
UIDCard.toUpperCase();
UIDCard = UIDCard.substring(1);
mfrc522.PICC_HaltA();
return true;
}
注意:需要添加库文件LiquidCrystal_I2C
将第一步中在串口中获取到的标签号填写到正文第二行代码中,即将标签值赋给全局变量
MasterTag
编译成功后烧入Arduino即可得到一款智障门锁!