RFID门禁系统-Arduino中文社区 - Powered by Discuz! Archiver

G2331104119 发表于 2021-8-13 19:54

RFID门禁系统

RFID门禁系统准备材料:一块arduino开发板(uno,nano,mega2560均可),RC522 RFID门禁射频,SG90舵机,oled ssd1306 128x64本篇作者使用的是mega2560 atmega2560首先开始电脑上安装arduino ide菜单栏里打开项目>加载库>管理库里挨个搜索将其下载U8glib,Wire,Servo,SPI,MFRC522,代码如下:/*-------------------------------------------------------*/#include <U8g2lib.h>#include <Wire.h>#include <Servo.h>#include <SPI.h>#include <MFRC522.h>//调用所需库U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);Servo servo_9;MFRC522 rfid(2, 3);//初始化oled,舵机,rc522String MFRC522_ReadCardUID(MFRC522 *_name){String _CardUID = "";for (byte _i = 0; _i < _name->uid.size; _i++){    if(_name->uid.uidByte < 0x10)      _CardUID += "0";    _CardUID += String(_name->uid.uidByte, HEX) + String(" ");}return _CardUID;} boolean MFRC522_IsNewCard(MFRC522 *_name){if(!_name->PICC_IsNewCardPresent())    return false;if(!_name->PICC_ReadCardSerial())    return false;return true;} void page2() {u8g2.setFont(u8g2_font_helvR10_tf);//oled设置字体、字号、加粗u8g2.setFontPosTop();u8g2.setCursor(0,17);//设置文字坐标u8g2.print("The RF card ID is:");//oled显示文字”The RF card ID is:”u8g2.setCursor(0,30);u8g2.print(MFRC522_ReadCardUID(&rfid));} void page1() {u8g2.setFont(u8g2_font_helvR10_tf);u8g2.setFontPosTop();u8g2.setCursor(0,17);u8g2.print("Please place the RFID");u8g2.setCursor(0,29);u8g2.print("card near the sensor");} void setup(){u8g2.setI2CAddress(0x3C*2);//设置oled IIC地址u8g2.begin();servo_9.attach(9);//设置舵机针脚Serial.begin(9600);//设置串口波特率为9600SPI.begin();rfid.PCD_Init();u8g2.enableUTF8Print(); } void loop(){servo_9.write(0);delay(0);u8g2.firstPage();do{    page1();}while(u8g2.nextPage());if(MFRC522_IsNewCard(&rfid)){    u8g2.firstPage();    do    {      page2();    }while(u8g2.nextPage());    Serial.println(MFRC522_ReadCardUID(&rfid));    if (String(MFRC522_ReadCardUID(&rfid)).equals(String("29 5d 8b 47 "))) {   //当读取卡号与自己的卡号一致时舵机开      servo_9.write(90);      delay(0);    }    delay(5000);    rfid.PICC_HaltA();    rfid.PCD_StopCrypto1();}}效果图如下https://www.arduino.cn/forum.php?mod=image&aid=76828&size=300x300&key=6ae3347bbc9be472&nocache=yes&type=fixnoneps:拍照技术不是很行,请多多谅解:D


G2331104119 发表于 2021-8-13 19:56

源码如下
#include <U8g2lib.h>
#include <Wire.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
Servo servo_9;
MFRC522 rfid(2, 3);

String MFRC522_ReadCardUID(MFRC522 *_name){
String _CardUID = "";
for (byte _i = 0; _i < _name->uid.size; _i++){
    if(_name->uid.uidByte < 0x10)
      _CardUID += "0";
    _CardUID += String(_name->uid.uidByte, HEX) + String(" ");
}
return _CardUID;
}

boolean MFRC522_IsNewCard(MFRC522 *_name){
if(!_name->PICC_IsNewCardPresent())
    return false;
if(!_name->PICC_ReadCardSerial())
    return false;
return true;
}

void page2() {
u8g2.setFont(u8g2_font_helvR10_tf);
u8g2.setFontPosTop();
u8g2.setCursor(0,17);
u8g2.print("The RF card ID is:");
u8g2.setCursor(0,30);
u8g2.print(MFRC522_ReadCardUID(&rfid));
}

void page1() {
u8g2.setFont(u8g2_font_helvR10_tf);
u8g2.setFontPosTop();
u8g2.setCursor(0,17);
u8g2.print("Please place the RFID");
u8g2.setCursor(0,29);
u8g2.print("card near the sensor");
}

void setup(){
u8g2.setI2CAddress(0x3C*2);
u8g2.begin();
servo_9.attach(9);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
u8g2.enableUTF8Print();

}

void loop(){
servo_9.write(0);
delay(0);
u8g2.firstPage();
do
{
    page1();
}while(u8g2.nextPage());
if(MFRC522_IsNewCard(&rfid)){
    u8g2.firstPage();
    do
    {
      page2();
    }while(u8g2.nextPage());
    Serial.println(MFRC522_ReadCardUID(&rfid));
    if (String(MFRC522_ReadCardUID(&rfid)).equals(String("29 5d 8b 47 "))) {
      servo_9.write(90);
      delay(0);

    }
    delay(5000);
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
}

}

kaifang 发表于 2021-10-3 22:01

:handshake:handshake:handshake

hlm4321 发表于 2021-10-18 20:43

大佬在吗,这是java写的吧,现在遇到点问题不知道能不能请教一下,就是字符串对比那一块,为什么我这里不能呢,现在串口显示读卡的ID但是就是不进if语句进行字符串对比

吼吼吼嘻嘻嘻 发表于 2022-6-24 18:50

能不能弄一个LCD1602的显示屏代码
页: [1]
查看完整版本: RFID门禁系统