如何制作一个Arduino门锁

步骤1:组件
以下是我们用于设置的组件,您可以混合/匹配各种型号。
以下组件可以可以从各种卖家那里购买,例如aliexpress或ebay。
我们用于成功的组件如下:
arduino mega(2560r3)
伺服(mg996r)
sd卡模块(无型号)
实时时钟( ds3231)
蓝牙(hc-05)
rfid(rc522)
键盘4 * 4(膜基质)
lcd 16 * 2(1602a)
rfid标签
sd
电阻(2k和220)
led(绿色,红色)
开关
电位计
安装模块的sd卡
android手机
更完整的清单附上金额,模型和丹麦供应商的链接。
第2步:它应该如何工作
当第一次接通电源时,门锁通过锁定开始,然后“空闲”,等待任何用户输入。
键盘
如果按下键盘,它开始按下存储的密码检查按下的键。
如果按下错误的键,则会增加错误的计数器。
当按下4位数时,它将检查错误的计数器,如果超过‘ 0’液晶显示屏打印“错误的p “如果按下了4个数字并且错误的计数器为‘0’,lcd将打印”密码已接受“并且锁定解锁。
如果按”#“或在任何此过程中‘*’将重置过程和计数器值。
蓝牙
如果收到蓝牙数据,它会读取接收的数据 - 如果此数据锁定为“1”锁定,它还会将“解锁”发送回蓝牙设备(android手机)
rfid
rfid模块会扫描是否有任何附近的可读标签,如果没有它只是跳过。如果附近有可读标签 - 它会读取标签值并将其与我们预定义的标签进行匹配,如果不匹配,则只需在lcd上打印“未授权”。如果标签匹配,则打印“欢迎”并解锁门。它还会将日期和时间(包括标签持有者的名称)保存到sd卡上的文本文件中。
解锁后当锁定解锁时,它会设置一个步骤,这用于检查门何时打开/关闭,以便锁再次接合。它基本上使用3个步骤来跟踪它的位置,操作是通过当前的简单开关完成的。
成功解锁也会点亮绿色指示灯,就像红色指示灯在当前锁定时会点亮一样。
我们在上面附上了一个流程图,简化了这一点。
步骤3:接线和电气元件设置
我们利用fritzing软件绘制了我们的布线图。
为了使fritzing原理图更容易,我们还制作了一个io/arduino超级引脚列表 - 这些都列在我们的文件中。
在上面的电气图中,你将找到组件之间的所有必要连接,包括布线概述。
由于我们使用了相当多的电线和arduino底部的双排线,因此很难看到线点有了这样的质量,我们建议下载图片并放大。
正如你所看到的那样,我们只为面包板连接了一些东西,主要是电源(5 v)和gnd,但这并不是反映我们使用面包板的真实世界测试设置持有除sd卡和键盘之外的大多数模块,这也将在后面的步骤中显示。
另请注意,我们正在使用电位计来控制lcd的亮度。
从左到右列出“较大”的物体/模块:伺服,ds3231,hc-05,面包板+ arduino,rc522,sd卡,lcd,键盘。
在上图中,+导线为红色为5 v,棕色为3.3 v, - (gnd)导线为蓝色。
第4步:编程arduino
所以编程arduino本身 - 这就是乐趣开始的地方 - 我们在代码的不同部分附上了5张图片。这是我们的测试设置中的代码,我们在代码中对串行打印进行了大量的故障排除,它们对于查看我们遇到的问题/我们做错了什么非常有帮助。
代码已被压缩更好地适应图片。
上面的第一张图片是我们正在初始化和全局变量:
以下行:
1初始化软件序列,包括设置引脚所以我们不必使用hw串口。
4初始化实时时钟模块
7-9定义引脚,包括rfid模块的从选择引脚,并创建实例。
12 -13定义sd卡模块的从机选择引脚,同时设置文件变量
16-28定义(映射)键盘布局,行和列,同时设置使用的引脚。
31定义用于lcd的引脚。
33-37声明变量。
40定义伺服电机。
43我们的字符串变量包含默认密码。
44-45变量适用于lcd位置和键盘前置sse的。
上面的第二张图片是对键盘进行编程:
3-4是用于跟踪键盘上按下的内容的变量以及“键”密码。
6-12正在打印键按下lcd后,延迟使代码等待几秒钟,然后按下按键消失在液晶显示屏上。
如果按下‘*’或‘#’,应该会发生什么情况 - 在我们的例子中如果按下的键与我们预定义的密码匹配,会发生什么情况。
如果按下的键与我们预定义的密码不符,则为25-29。如果发生的话,会发生什么情况。我们达到了密码的最大长度(4位数),我们按键错误.46-60,最后如果我们达到了最大长度且输入没有错误
第三上面的图片是蓝牙编程:这里我们检查串口上是否有来自蓝牙的数据。由于我们只是从蓝牙处理一个简单的‘1’代码这里从序列中读取并将其放入我们的变量中e。
4-8如果蓝牙连接有‘1’会发生什么,这里我们还会将“unlocked”发回蓝牙串口。
第四张图片以上是编程rfid:4-5我们正在检查附近是否有卡。
8-9从卡上读取值。
12-18将rfid值添加到我们的字符串变量。
20-21基本上使我们的变量大写的内容。
22我们正在检查变量是否符合我们的预定义。
24-25选择我们的奴隶,我们需要sd卡模块。
27我们打开sd卡上的文件,以便接收打印数据。
如果我们的可变数据与我们预定义的任何一个匹配,我们在lcd上打印名称,并将日期和时间以及名称打印到sd卡上的日志。
70-72调用功能“setlocked”并将其设置为false - 解锁锁定。
如果标签值没有,则为74-78不符合我们的预定义。
上面的第五张图片是我们实际控制伺服的功能:如果是setl,则为3调用ocked(true)。
5-10安装舵机并给它一个位置,基本上锁门。
如果设置了setlocked(假),则为12连接伺服并给它一个位置去解锁门。
步骤5:编程android手机
我们已经使用简单易用的基于web的mit app inventor 2来编写android手机。
之前没有使用app inventor,我们发现它非常简单直接使用 - 因为它就像拼图一样。然而,我们确实遵循了各种基本功能指南。
第一张照片是我们简单的应用程序最终看起来的方式 - 正如您在右侧看到的那样,有两个按钮,其中一个是列表。单击顶部的蓝牙横幅显示手机的蓝牙列表,点击hc-05将手机连接到arduino蓝牙模块。
然后是解锁按钮 - 它按照说法执行操作,解锁门,我们无法锁门,因为如果你打开它,它会在关闭时自动锁定。
第一行文字只显示你是否连接 - 在实际应用中根据连接,它是红色或蓝色。
第二个显示锁定的当前状态。
第二张图片是我们的块设计(拼图) - 应用实际上做了什么。
从左上角开始,我们基本上告诉它在点击蓝牙横幅时要做什么。
第二部分是我们想要在弹出窗口中连接到所选设备。
t第三部分是我们在点击解锁按钮时想要做的 - 我们只是发送49,因为这是用ascii发送的,数字‘49’等于‘1’ - 所以我们实际上发送一个‘1’到arduino。
继续到图片的右侧,我们使用时钟计时器来更新(有点实时)连接文本,所以如果连接被删除,那么文本就会显示。/》我们也使用这个时钟发送我们的锁定/解锁状态,这在启动应用程序时抛出“无连接的蓝牙设备”的缺点,但我们更喜欢这个状态。
步骤6:3d打印
如前所述,我们使用了一些3d打印部件用于将伺服系统安装到锁舌上。
我们使用的打印机是ultimaker 2+,使用0.4 mm黑色pla。我们使用的软件是fusion 360编辑设计和cura来制作它适合ultimaker。它很简单,由两个组成事情,一个是死锁附件,第二个是伺服的安装座 - 包括一个用于led的孔,如果需要,还有一个用于手动操作按钮的方孔。
deadbolt附件需要三个多小时才能打印,伺服支架/外壳需要超过21个小时才能打印出来。
hacker shack对死锁的3d设计及其链接的致谢:https://bit.ly/2yq4c97
第7步:测试设置
因为我们都是编程arduino的新手 - 只是因为在将所有东西焊接在一起并可能放在一起之前先测试一下是个好主意在一个漂亮的盒子里,我们利用大型面包板将大多数东西连在一起。然而,这确实使得所有接线看起来都很麻烦。
确保一切都按照我们想要的方式工作后,剩下的就是将伺服装置安装到3d打印机适配器并连接使用螺丝将伺服系统安装到外壳上。
你可能需要使用垫片才能使它适合你的锁舌。
由于这是我们学期的一个项目,我们实际上并没有把东西焊在一起,因为我们我希望以后可以将我们的零件用于不同的项目 - 但是它可以使它更好看,能够移除大量的布线。
步骤8:测试设置 - youtube
第9步:完整代码
//libraries used
#include //lcd module
#include //keypad module
#include《 mfrc522.h》 //rfid module
#include //rfid and sd card modules
#include //sd card module
#include //rtc module
#include //servo module
#include //software serial
softwareserial myserial(10, 11); //softserial initialize. pin number rx, tx
//realtime clock hardware interface initialize
ds3231 rtc(sda, scl);
//rfid pins same instance
#define ssrfid_pin 53 //ss pin for rfid //ss pin (slave select)
#define rstrfid_pin 47 //rst pin
mfrc522 mfrc522(ssrfid_pin, rstrfid_pin); //instance
//sd card
#define sssd_pin 54 //ss pin (slave select)
file myfile; //file being used
//keypad grid
const byte rows = 4; //keypad rows
const byte cols = 4; //keypad columns
//layout af keypad
char hexakeys[rows][cols] = {
{‘1’, ‘2’, ‘3’, ‘a’},
{‘4’, ‘5’, ‘6’, ‘b’},
{‘7’, ‘8’, ‘9’, ‘c’},
{‘*’, ‘0’, ‘#’, ‘d’}
};
byte rowpins[rows] = {31, 33, 35, 37}; //pinout rows
byte colpins[cols] = {39, 41, 43, 45}; //pinout columns
keypad keypad = keypad( makekeymap(hexakeys), rowpins, colpins, rows, cols);
//lcd related
liquidcrystal lcd(22, 23, 24, 25, 26, 27); //lcd pins
int redpin = 2; //red led pin (no access)
int greenpin = 3; //green led pin (access granted)
int doorpin = 5; //door magnet (high if door is closed)
int steps = 0; //step counter for door + locking control
int state = 0; //bluetooth state
//servo (needed in setlocked function)
servo servo_motor;
string password = “1234”; //keypad default password
int position = 0; // variable for cursor(lcd) and password position(0-3 = 4)
int wrong = 0; // variabel for wrong password
//rfid
//1a 83 57 d3 = blue rfid tag
//52 0b c6 1f = white creditcard tag
void setup() {
serial.begin(9600); //start the serial function(to communicate over usb)
rtc.begin(); //start the real time clock
myserial.begin(9600); //bluetooth serial
pinmode(doorpin, input_pullup);
//sd card
pinmode(46, output); //changes pin 46 to output mode (ss pin)
digitalwrite(46, high); //has to be high from startup since we‘re using rfid
//rfid
pinmode(53, output); //changes pin 53 to output mode (ss pin)
digitalwrite(53, low); //has to be low from startup so rfid is used.
spi.begin(); //start the spi (serial peripheral interface) for rfid&sdcard
mfrc522.pcd_init(); //start the rfid
if (!sd.begin(46)) { //start the sd library and card (! for invert)
while (1);
}
serial.println(“initialization done.”); //troubleshoot serial
lcd.begin(16, 2); //lcd size
lcd.clear();
servo_motor.attach(6); //attach servo to pin 6(pwm)
setlocked(true); //locks the first time during setup
}
void loop() {
lcd.clear(); //clears the lcd
lcd.print(“enter password:”); //prints on the lcd
delay(100);
//keypad
char pressed = keypad.getkey(); //variable for pressed on keypad
string key[4]; //variable
if (pressed) //if keypad pressed
{
lcd.clear(); //clear lcd
lcd.print(“enter password:”); //print lcd
lcd.setcursor(position, 1); //sets the cursor to our variable on the “second row”
lcd.print(pressed); //prints the pressed key
delay(200);
if (pressed == ’*‘ || pressed == ’#‘) //if star or square is pressed
{
position = 0; //reset position
setlocked(true);//set locked
lcd.clear(); //clear lcd
}
else if (pressed == password[position]) //else if the pressed key matches the password position
{
key[position] = pressed; //adds the pressed to the key variable
position++; //adds 1 to position
}
else if (pressed != password[position] ) //else if pressed key doesnt match password position
{
wrong++; //add 1 to wrong variable
position ++; //add 1 to position
}
if (position == 4) //if position = 4 (password is max 4)
{
if ( wrong 》 0) //if the wrong variable is above zero
{
wrong = 0; //reset wrong variable
position = 0; //reset position variable
lcd.clear(); //clear lcd
lcd.print(“wrong”); //print
lcd.setcursor(5, 1); //sets cursor position lcd
lcd.print(“password”); //print
delay(1000);
setlocked(true); //set locked
}
else if (position == 4 && wrong == 0) //if position is 4 and wrong variable is zero
{
position = 0; //reset position variable
wrong = 0; //reset wrong variable
lcd.clear();
lcd.print(“password”);
lcd.setcursor(6, 1);
lcd.print(“accepted”);
delay(1000);
lcd.clear();
lcd.print(“door open”);
setlocked(false); //unlocking
steps = 1; //sets step to 1
}
}
}
//bluetooth
else if (myserial.available() 》 0) { // checks whether data is comming from the serial port
state = myserial.read(); // reads the data from the serial port
if (state == ’1‘) {
lcd.clear(); //clear lcd
lcd.print(“bluetooth open”); //lcd print
setlocked(false); //unlocking
steps = 1; //sets steps so door autolock works
myserial.println(“unlocked”); // send back the string “unlocked”
}
}
//rfid
else {
//wait for a card
if ( ! mfrc522.picc_isnewcardpresent()) {
return;
}
//read card value
if ( ! mfrc522.picc_readcardserial()) {
return;
}
string content = “”; //variable for uid from tag
for (byte i = 0; i 《 mfrc522.uid.size; i++) {
content.concat(string(mfrc522.uid.uidbyte[i] 《 0x10 ? “ 0” : “ ”));
content.concat(string(mfrc522.uid.uidbyte[i], hex));
}
//compare rfid tags and do xx
content.touppercase();
content = content.substring(1);
if ((content == “1a 83 57 d3”) || (content == “52 0b c6 1f”)) { //if variable content matches either tag
digitalwrite(53, high); //ss pin rfid high to not use
digitalwrite(46, low); //ss pin sd card low to use
myfile = sd.open(“log.txt”, file_write); //opens the file on sdcard
if (content == “1a 83 57 d3”) { //if variable matches
lcd.clear();
lcd.setcursor(3, 0);
lcd.print(“velkommen”); //print welcome lcd
lcd.setcursor(4, 1);
lcd.print(“flemming”); //print name lcd
//sd start
myfile.print(rtc.getdatestr()); //prints the rtc date to sd card
myfile.print(“ ”); //prints a space
myfile.print(rtc.gettimestr()); //prints rtc time hh/mm/ss
myfile.print(“ ”); //prints a space
myfile.println(“flemming”); //prints the name entering
myfile.close(); //closes the file opened on the sd card
digitalwrite(46, high); //ss pin sd card high
digitalwrite(53, low); //ss pin rfid low
}
if (content == “52 0b c6 1f”) {
lcd.clear();
lcd.setcursor(3, 0);
lcd.print(“velkommen”);
lcd.setcursor(4, 1);
lcd.print(“mikkel”);
myfile.print(rtc.getdatestr()); //prints the rtc date to sd card
myfile.print(“ ”); //prints a space
myfile.print(rtc.gettimestr()); //prints rtc time hh/mm/ss
myfile.print(“ ”); //prints a space
myfile.println(“mikkel”); //prints the name entering
myfile.close(); //closes the file opened on the sd card
digitalwrite(46, high); //ss pin sd card high
digitalwrite(53, low); //ss pin rfid low
}
setlocked(false); //unlocking
steps = 1; //setting step 1
}
else { //if rfid tag doesnt match
lcd.clear(); //clear lcd
lcd.print(“not authorized”); //print lcd
delay(2000);
}
}
//door magnet, closed, open, closed (for relocking)
while (steps 》 0) { //if steps is above 0
if ((steps == 1) && (digitalread(doorpin) == high )) { //if step = 1 read the doorpin if high (closed) continue
steps = 2; //set step 2
}
if ((steps == 2) && (digitalread(doorpin) == low)) { //if step = 2 read the doorpin if low (open)
steps = 3; //set step 3
}
if ((steps == 3) && (digitalread(doorpin) == high)) { //if step = 3 read the doorpin if high (closed) continue
setlocked(true); //set locked
lcd.clear(); //clear lcd
lcd.print(“enter password:”); //lcd print
myserial.println(“locked”); // send back the string “locked”
steps = 0; //set steps = 0
}
}
} //loop end
void setlocked(int locked) //function for the doorlock
{
if (locked) //if locked is true, lock
{
digitalwrite(redpin, high); //red on
digitalwrite(greenpin, low); //green off
servo_motor.attach(6); //attach servo to pin 6
servo_motor.write(150); //write servo position
delay(1000);
servo_motor.detach(); //deattach servo
}
else //if locked is false, unlock
{
digitalwrite(redpin, low); //red off
digitalwrite(greenpin, high); //green on
servo_motor.attach(6); //attach servo to pin 6
servo_motor.write(0); //write servo position
delay(1000);
servo_motor.detach(); //deattach servo
}
}
第10步:最后的想法
对于我们自动化的第二个学期来说,这是一个有趣的小项目。
好处:
有趣和实用的项目,我们必须尝试使用mit app inventor 2构建一个简单的应用程序,并使用ultimaker 3d打印机,同时摆弄围绕面包板上的所有电线,使代码实际工作 - 它确实给出了基于文本的编程如何工作的一些见解。
缺点:
可能不是最安全(没有)智能锁 - 也不会留下最小的足迹 - 即使是arduino和面包板都非常大。
可能的未来升级:
更改3d设计,既可以减少占地面积,也可以为arduino腾出空间 - 这可能包括使用齿轮功能伺服器可以和arduino一起坐在一边。
如前所述,将电线焊接在一起,电线非常杂乱。
添加某种安全性,使其真正适合于前门。

等离子清洗机处理材料(PTFE/碳纤维/玻璃)的表面亲水性原理
什么是多级组成的计算机系统
GPS不惧中国汽车信息娱乐市场放缓-预计今年产品营业收入增长
如何通过手机APP远程控制PLC
AMD最强芯片 全村的希望!
如何制作一个Arduino门锁
浅谈光纤压力传感器FOP-M在矿井测量气压中的应用
Kinara推出Ara-2边缘AI处理器,为边缘服务器和笔记本电脑提供高性
欧普照明和佛山照明2018年三季度业绩报告
什么是数字告示
继VR AR MR之后,XR是什么?
利用OpenCV加速嵌入式系统开发
如何选择智能制造系统?
电源设计之缓冲正向转换器
TMS320C6678存储器访问性能 (下)
浅谈MCU下载程序电路原理
爆华为将发布5nm麒麟处理器!10月将搭配Mate 40系列首发
IKEA 推出的 iOS App IKEA Place,运用苹果的 ARkit 技术
288芯光缆交接箱用途_288芯光缆交接箱安装方法
如何正确区分和安装CPU