Microcontroller project: Bluetooth wireless password lock design based on microcontroller

Main introduction

In the design of Bluetooth wireless password lock based on microcontroller, Bluetooth technology is taken as the core content of this design. By using STC89C52 microcontroller, plus LCD liquid crystal display module, and a variety of related parts, a variety of more convenient Settings, successfully help users solve many aspects such as unlocking and locking, and scientifically rationalize the entire process to ensure people’s safety to the greatest extent.
Experiments have proven that compared to traditional mechanical locks, Bluetooth wireless code locks have the advantages of safety, convenience and low cost, and can be widely used in homes, hotels, office buildings, banks and other places. This safe and convenient unlocking method provides protection for people’s daily life and work, protects the safety of people and property, and greatly improves the safety factor.

Keywords: microcontroller; Bluetooth module; LCD1602; password lock

1. Overall system design

In order to realize remote unlocking and protect people’s property and information security, this article designs a microcontroller-based Bluetooth wireless password lock system that integrates alarm, remote control, password modification, false password and other functions to ensure the security of the lock. sexual improvement.

1.1 System design requirements

(1) Use AT24C02 chip to store the set password and have the function of saving the password after power failure;
(2) Users can set the corresponding password by themselves. If they want to change to a new password, they must re-enter the original password twice to pass the system review;
(3) If the password is entered incorrectly three times, the buzzer will sound, the alarm will be locked, and the screen will display ERROR;
(4) Enter the correct password and the relay will close, the unlock indicator light will light up, and the screen will display OPEN;
(5) 4×4 matrix keyboard includes numeric keys from 0 to 9, function keys from A to D, and * and # keys;
(6) The LCD screen adopts a new optical sensing system, which can successfully change the screen according to the weather, thus saving power to the greatest extent;
(7) False passwords can be used. When unlocking, you can enter the false password at will to hide the password and prevent others from stealing the password;
(8) Using Bluetooth module, remote unlocking can be achieved.

1.2 System design ideas

In the selection of the overall solution, two solutions were initially considered. One is to use digital circuits to realize the function of the password lock, cooperate with other main control chips, and combine with the Bluetooth module to achieve the expected functions. The other is to use a single-chip microcomputer as the The main control chip [4] uses its programmability to realize the password lock function, while taking into account the use of the Bluetooth module.
Option 1: Use digital circuit control system
The digital logic circuit composed of triggers is used as the core control of the password lock. When the user wants to avoid the leakage of his or her information, he only needs to press the corresponding interference button to successfully terminate the current password input behavior, and if the user When entering a password, if it takes too long, the system will determine that the input is invalid and issue an alarm. In this way, the operation of the entire system can be made more convenient and easy to operate. The disadvantage is that the flexibility is too poor and it is easy for the user to It brings unnecessary trouble, and when adding a Bluetooth module, an additional main control chip must be added, which is complicated and costly to implement, and the use effect is not very good. Therefore, this solution has no practical application.
Option 2: Use microcontroller to control the system
Using a single-chip microcomputer as the main control system, the single-chip microcomputer is a small but complete chip that is used in integrated circuits and can also be called a microcomputer system. It is stable and can react in real time to the received data, and is often used in industrial environments. Then connect external devices such as LCD monitors, Bluetooth modules, matrix keyboards, memory chips, etc., and the microcontroller controls external devices through I/O ports. Users can enter passwords in two ways. They can use the matrix keyboard to enter the password or use the Bluetooth APP to enter the password. If the correct password is successfully entered, the relay will close, the unlock indicator light will light up, and the screen will show the open status. If the correct password is not entered, the buzzer will sound the corresponding alarm. If incorrect content is entered three times, the buzzer will remain in the alarm state and the entire system will enter a locked state.
Comparing the two options, option two is better. Its biggest advantages are high flexibility, high performance and low price, so this design uses a single-chip microcomputer control system solution.

2. System hardware design

2.1 Design Principle

The main hardware of this design consists of microcontroller [5], display module, driver module and other hardware. When the entire system is running, the microcontroller will follow the corresponding content actually entered by the user. During this process, the microcontroller determines the correctness of the password entered by the user. If the correct password is successfully entered, the relay will close, the unlock indicator light will light up, and the screen will show the open status. If the correct password is not entered, the buzzer will sound the corresponding alarm. If you answer incorrect content multiple times, the system will determine that it has been stolen by others based on this situation, and the entire system will enter a locked state. The schematic block diagram of the Bluetooth wireless password lock hardware is shown in Figure 2.1.

Figure 2.1 Bluetooth wireless password lock hardware principle block diagram

2.2 Main control module

The microcontroller plays a vital role in this graduation project and is the core of the entire design plan. This project plans to use a single-chip computer as the main control system. It plans to use STC89C52 single-chip computer and STM32 single-chip computer. The comparison of the two single-chip computers is as follows.
Option 1: STC89C52 microcontroller
The STC89C52 microcontroller has many advantages, such as extremely fast operation, low energy demand, effective resistance to external interference, and many other aspects. Moreover, its instruction code can be well connected with the traditional May 1st microcontroller. There is a complete operation processor inside the STC89C52 microcontroller, and the object unit processed is not words or bytes, but bits. It uses 51Core core and has 64KB space. At the same time, due to its own dual address range, the use of STC89C52 microcontroller is more diversified.
Option 2: STM32 microcontroller
The STM32 microcontroller mainly serves the Cortex-M core and contains 1μs dual 12-bit ADC, 4 Mbit/s UART, 18 Mbit/s SPI, etc. However, due to its excellent performance, it is much more complicated to use than the STC89C52 microcontroller. Therefore, this design does not plan to use STM32 microcontroller.
Comparing the two options, option one is better. It has low requirements and complete functions, and can realize all functions in a smaller range, so this design chooses STC89C52 single-chip microcomputer as the main control chip.
The STC89C52 microcontroller can maintain a stable working state after entering work. The voltage requirement needs to be between 5.5V and 2.0V. After entering the working state, its operating frequency always remains below 48Hz. For the STC89C52 microcontroller, it It has the following characteristics: first, it has its own internal program storage space, secondly, it has three timers to help it work, and finally, it has strong anti-interference and can successfully display the current working status. . The schematic diagram of the STC89C52 microcontroller is shown in Figure 2.2.

Figure 2.2 STC89C52 microcontroller schematic diagram

This design uses the minimum number of components to form a system that the microcontroller can work on, and can be called a minimal system [6]. For the STC89C52 microcontroller, the minimum system must have a microcontroller, crystal oscillator circuit, and reset circuit. The crystal oscillator circuit and reset circuit are necessary conditions for the operation of the microcontroller, and can keep the microcontroller in normal working condition. Often, the smallest system is the most important part of the entire system. By operating and improving it accordingly, it can help us complete many complex tasks. The minimum system schematic diagram of the microcontroller is shown in Figure 2.3.

Figure 2.3 Minimum system schematic diagram of microcontroller

3. System software design

3.3 Program flow chart

The overall software designed this time is relatively simple, mainly divided into main programs and subprograms, as well as programs for setting passwords. First, initialize each module, detect whether a button is pressed, and if so, determine which button was pressed, and then determine whether to call the corresponding subroutine. The main program flow chart is shown in Figure 3.2.

Figure 3.2 Main program flow chart

Real object

Schematic diagram


Program

//Include header files
#include <REG52.h>
#include<intrins.h>
//Macro definition
#define LCM_Data P0 //Define P0 port as LCM_Data
#define uchar unsigned char
#define uint unsigned int
#define w 6 //Define the number of password digits
//Control pin of 1602

sbit lcd1602_rs=P2^5;
sbit lcd1602_rw=P2^6;
sbit lcd1602_en=P2^7;

sbit Scl=P3^4; //24C02 serial clock
sbit Sda=P3^5; //24C02 serial data

sbit ALAM = P2^1; //Alarm
sbit KEY = P3^6; //Unlock
 

bit operation=0; //operation flag bit
bit pass=0; //Password correct flag
bit ReInputEn=0; //Reset input allow flag
bit s3_keydown=0; //3 seconds keydown flag bit
bit key_disable=0; //Lock keyboard flag
bit flag_REC =0; //Serial port acceptance flag bit
unsigned char buff[5]=0;
unsigned char countt0,second; //t0 interrupt counter, second counter

void Delay5Ms(void); //Declare the delay function

unsigned char code a[]={<!-- -->0xFE,0xFD,0xFB,0xF7}; //Control disk scan control table
//LCD display data array
unsigned char code start_line[] = {<!-- -->"password: "};
unsigned char code name[] = {<!-- --> "===Coded Lock==="}; //Display name
unsigned char code Correct[] = {<!-- -->" correct "}; //Input is correct
unsigned char code Error[] = {<!-- -->" error "}; //Input error
unsigned char code codepass[] = {<!-- -->" pass "};
unsigned char code LockOpen[] = {<!-- -->" open "}; //OPEN
unsigned char code SetNew[] = {<!-- -->"SetNewWordEnable"};
unsigned char code Input[] = {<!-- -->"input: "}; //INPUT
unsigned char code ResetOK[] = {<!-- -->"ResetPasswordOK "};
unsigned char code initword[] = {<!-- -->"Init password..."};
unsigned char code Er_try[] = {<!-- -->"error,try again!"};
unsigned char code again[] = {<!-- -->"input again "};

unsigned char InputData[16]; //Input password temporary storage area
unsigned char CurrentPassword[16]={<!-- -->1,3,1,4,2,0}; //Administrator password (can only be modified in the program)
unsigned char TempPassword[16];
unsigned char N=0,M=0,flag=0; //Number of password input digits
unsigned char ErrorCont; //Error count
unsigned char CorrectCont; //Correct input count
unsigned char ReInputCont; //Re-enter count
unsigned char code initpassword[16]={<!-- -->0,0,0,0,0,0}; //After entering the administrator password, initialize the password to 000000
unsigned char MM[2]={<!-- -->0,0};

//=====================5ms delay======================== ======
void Delay5Ms(void)
{<!-- -->
unsigned int TempCyc = 5552;
while(TempCyc--);
}

//====================400ms delay============================ ====
void Delay400Ms(void)
{<!-- -->
 unsigned char TempCycA = 5;
 unsigned int TempCycB;
 while(TempCycA--)
 {<!-- -->
  TempCycB=7269;
  while(TempCycB--);
 }
}

//==============================Main function================ ===============
void main(void)
{<!-- -->
 unsigned char KEY,NUM;
unsigned char i,j;
 P1=0xFF; //P1 port reset
TMOD=0x12; //Define working mode
 TH0=0xB0;
 TL0=0x3C; //Assign initial value to timer
 EA=1; //Turn on the main interrupt switch
 ET0=1; //Turn on the interrupt enable switch
 TR0=0; //Turn on the timer switch
TH1=0Xfd;
TL1=0Xfd; //9600
TR1=1; // Timer 1 starts
SM0=1; //Set the working mode of the serial port
SM1=1; //Mode 1
REN=1; // Allow the serial port to receive data
ES=1; // Serial port interrupt promise
 \t
 Delay400Ms(); //Start waiting and wait for LCM to enter the working state
 lcd_init(); //LCD initialization
write_1602com(yi);//Calendar display fixed symbols start displaying after the 0th position on the first line
for(i=0;i<16;i + + )
{<!-- -->
write_1602dat(name[i]);//Write the startup screen to the LCD screen
}
write_1602com(er);
for(i=0;i<16;i + + )
{<!-- -->
write_1602dat(start_line[i]);//Write input password waiting interface
}
write_1602com(er + 9); //Set the cursor position
write_1602com(0x0f); //Set the cursor to flash
 Delay5Ms(); //Delay for a moment (optional)

 N=0; //Initialize the number of data input digits
RdFromROM(MM,17,1);
M=MM[0];
if(M==0||M>16)
{<!-- -->
M=6;
MM[0]=6;
WrToROM(MM,17,1);
}
 while(1) //Enter the loop
 {<!-- -->
if(key_disable==1) //Lock keyboard flag is 1
Alam_KeyUnable(); //Alarm keyboard lock
else
ALAM=1; //turn off alarm

KEY=keynum(); //Read the position code of the key
if(KEY!=0) //When a key is pressed
{<!-- -->
if(key_disable==1) //Lock keyboard flag is 1
{<!-- -->
second=0; //clear seconds
}
else //When the keyboard is not locked
{<!-- -->
NUM=coding(KEY); //Encode the key according to its position, and assign the encoding value to NUM
{<!-- -->
switch(NUM) //Determine the key value
{<!-- -->
case ('A'): ; break;
case ('B'): ; break;
case ('C'):
write_1602com(yi);//Calendar display fixed symbols start displaying after the 0th position on the first line
for(i=0;i<16;i + + )
{<!-- -->
write_1602dat(name[i]);//Write the startup screen to the LCD screen
}
if(N>=1) N--; OneAlam(); //Key tone
//DisplayOneChar(6 + N,1,'*');
for(j=N;j<16;j + + )
{<!-- -->
write_1602com(er + j);
write_1602dat(' ');
}
for(j=0;j<N;j + + )
{<!-- -->
write_1602com(er + j); //The number of displayed digits increases as the input increases
write_1602dat('*'); //But do not display the actual number, use * instead
}
InputData[N]=N + 6;
break; //ABC is an undefined button
case ('D'): ResetPassword(); break; //Reset password
case ('*'): Cancel(); break; //Cancel the current input
case ('#'): Ensure(); break; //Confirm key,
default: //If the function key is not pressed, it is the numeric key.
{<!-- -->
//DisplayListChar(0,1,Input);
write_1602com(yi);
for(i=0;i<16;i + + )
{<!-- -->
write_1602dat(Input[i]); //Display input screen
}
operation=0; //Operation flag cleared
if(N<16) //When the entered password is less than 6 characters, accept the input and save it. If it is greater than 6 characters, it will be invalid.
{<!-- -->
OneAlam(); //Button tone
//DisplayOneChar(6 + N,1,'*');
for(j=0;j<16;j + + )
{<!-- -->
if(j>N)
{<!-- -->
write_1602com(er + j);
write_1602dat(' ');
}
if(j<=N)
{<!-- -->
write_1602com(er + j); //The number of displayed digits increases as the input increases
write_1602dat('*'); //But do not display the actual number, use * instead
}
}
InputData[N]=NUM; //Assign the code of the numeric key to the InputData[] array for temporary storage
N + + ; //Password digits added
}
else //After the input data digits are greater than 6, ignore the input
{<!-- -->
N=16; //When the password input is greater than 6 digits, the input will not be accepted
break;
}
write_1602com(er + N);
if(ReInputEn==1 & amp; & amp;ReInputCont==0)
M=N;
}
}
}
}
}
}
}

//******************************Interrupt service function****************** *********************
void time0_int(void) interrupt 1 //Timer T0
{<!-- -->
 TH0=0xB0;
 TL0=0x3C; //Reassign timer initial value
 //TR0=1;
 countt0 + + ; // Timing variable addition, 50ms when added once
  if(countt0==20) //Add to 20 times and it will be 1s
   {<!-- -->
countt0=0; //clear variable
second + + ; //Add seconds
if(pass==1) //When in unlocking state
{<!-- -->
if(second==1) //When the second is added to 1s
{<!-- -->
\t\t\t 
TR0=0; //Close timer
TH0=0xB0;
 TL0=0x3C; //Assign initial value again
second=0; //clear seconds
}
}
else //Not in the unlocking state
{<!-- -->
if(second==3) //When seconds add to 3
{<!-- -->
TR0=0; //Close the timer
second=0; //clear seconds
key_disable=0; //Lock keyboard clear
s3_keydown=0;
TH0=0xB0;
TL0=0x3C; //Reassign initial value
}
else
TR0=1; //Open timer
}
\t\t\t
   }
}

// Communication interrupt receiving program. Interrupt function has no return value.
  void uart_rx(void) interrupt 4 using 3 //Putting it here is the same as putting it in main()
 {<!-- -->

   unsigned char i;
unsigned char flag;
unsigned char dat;
    if(RI) //Whether to receive interrupt
    {<!-- -->
       RI=0;
       dat=SBUF;
       if(dat=='O' & amp; & amp;(i==0)) //Receive the first frame of data
       {<!-- -->
            buff[i]=dat;
            flag=1; //Start receiving data
       }
       else
if(flag==1)
{<!-- -->
i + + ;
buff[i]=dat;
if(i>=2)
{<!-- -->
i=0;
flag=0;
flag_REC=1;
} // Stop receiving
}
}

 }

Summary

This design uses the microcontroller STC89C52 as the main control chip, and then connects external liquid crystal display LCD1602, Bluetooth module, matrix keyboard, memory chip AT24C02 and other devices to develop an electronic password lock with Bluetooth wireless function. In terms of the choice of microcontroller, this design chose the STC89C52 microcontroller. Compared with other microcontrollers, this microcontroller has the advantages of high performance, low power consumption, and super anti-interference, making the system more secure and able to operate in the long term. Reduce costs and improve promotion in use.
After relevant debugging of the device, the Bluetooth wireless remote unlocking function was finally realized. The emergence of Bluetooth wireless password locks provides convenience and security for people’s daily lives, making more and more modern people feel the importance of technology. The launch of any new technology or new product is not a step away. Only continuous technological progress and improvement can it be displayed to the world in perfect form. Bluetooth wireless code lock is a technological achievement in the code lock industry that emerged after the technical combination of electronic products and household products.

Directory

Table of contents
Abstract Ⅰ
AbstractⅡ
Introduction 1
1 Overall system design 3
1.1 System design requirements 3
1.2 System design ideas 3
2 System hardware design 5
2.1 Design Principles 5
2.2 Main control module 5
2.3 Chip module 8
2.4 Matrix keyboard module 9
2.5 LCD module 10
2.6 Relay driver module 12
2.7 Buzzer module 13
2.8 Bluetooth module 14
3 System software design 16
3.1 Software design and development environment 16
3.2 Programming language selection 16
3.3 Program flow chart 17
4 System Debugging 20
4.1 Hardware debugging 20
4.2 Software Debugging 20
4.3 Problems and solutions 21
Conclusion 23
Reference 24
Appendix 1 Hardware Schematic 26
Appendix 2 Source Program List 27
Acknowledgments 34