Professional Documents
Culture Documents
THUYẾT MINH PBL3 (BẢN CUỐI)
THUYẾT MINH PBL3 (BẢN CUỐI)
PBL3: DESIGN OF
MICROCONTROLLERS AND
SENSORS
Da Nang, June/2023
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Contents
PREFACE ........................................................................................................................... 4
CHAPTER 1: INTRODUCTION OVERVIEW.................................................................. 5
1. What is Digital lock? .................................................................................................... 5
2. Benefits of digital lock ................................................................................................. 6
3. Analyze the project and choosing option ..................................................................... 6
3.1. Analyze .................................................................................................................. 6
3.2. Choosing option ..................................................................................................... 6
CHAPTER 2: HARDWARE DESIGN ............................................................................... 7
1. Overview of system...................................................................................................... 7
2. Component of system ................................................................................................... 7
2.1. PIC16F877A .......................................................................................................... 7
2.2. 4x4 keypad matrix ............................................................................................... 11
2.3. LCD16X2............................................................................................................. 13
2.4. Servo motor SG90 ............................................................................................... 15
2.5. RC522 .................................................................................................................. 17
2.6 LM393 .................................................................................................................. 18
3. Component in proteus ................................................................................................ 19
3.1. Source circuit ....................................................................................................... 19
3.2. Processing block .................................................................................................. 20
3.3. Source and clock pulse block .............................................................................. 21
3.4. Reset and charging block ..................................................................................... 21
3.5. Button and led block ............................................................................................ 22
3.6. LCD block............................................................................................................ 22
3.7. RS232................................................................................................................... 23
3.8. Sensor, servo & buzzer ........................................................................................ 23
3.9. Keypad 4x4 .......................................................................................................... 24
3.10. RC522 ................................................................................................................ 24
3.11. PCB layout & 3D Visualizer .............................................................................. 25
CHAPTER 3: SOFTWARE DESIGN ............................................................................... 27
2
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
3
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
PREFACE
4
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Da Nang, June/2023
Digital locks are electronic or battery-operated locks that do NOT require the use
of physical keys for access. They’re used as an alternative to the conventional mechanical
locks, with the automated features giving a more creative security solution depending on
its intended use.
Like any other door lock, digital locks also use a latch or a bolt that can run across
the door and the doorframe to prevent access. What differs is how the latch or bolt is
controlled. Digital locks work by the use of any of the following: RFIDs, Pin codes,
fingerprints, smartphones, and more.
Digital locks are commonly found on cars and doors, but they can also be used
for lockers, furniture and cabinets. Reviews of these locks for cars and doors are sometimes
5
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
mixed, but for businesses who use lockers and cabinets, the benefits of digital locks are
greater compared to the traditional key locks.
2. Benefits of digital lock
• You can authorize multiple people simultaneously
• No more broken keys in locks
• Don't be afraid to lose the key
• Easy to change the code
• High security
• Easy out of hours access for approved persons, no calling a keyholder out
• There is no need to carry keys
• There is no need for spare keys
3. Analyze the project and choosing option
3.1. Analyze
Through the actual survey, the digital key needs to have the components of a
normal set of keys
• Easy to use
• Easy to use database
• Can warn when wrong login
• The power use is not dangerous to the user
• The price is suitable for students
3.2. Choosing option
Based on the above analysis, we make the following choices:
• Using microcontroller 16F877A
• Using 16x2 LCD screen to display information and communicate with users
• Using the 4x4 keyboard to sign in
• Using RC522 to scan the magnetic card to open the door
• Using servo motor to close and open the door
• Using sensor to open the door when someone comes out
6
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
LCD 16x2
Servo
7
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Figure 2: PIC16F877A
• Specifications
CPU PIC 8 bit
EEPROM/HEF 256/HEF
Number of pins 40
Internal oscillator No
8
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Number of comparators 2
9
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
• Pinout diagram
10
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
11
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
12
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
• Set logic level 0 for C1: Set column 1 to 0 and the other columns to 1. The
programmer checks each pin from R1 to R4 to determine which button is being
pressed. For example, if pin R3 is at level 0, then button 7 is being pressed.
• Set logic level 0 for C2: Similar to the above step, set column 2 to 0 and the other
columns to 1. The programmer checks each pin from R1 to R4 to determine which
button is being pressed (2, 5, 8, or 0).
• Repeat the above steps for the remaining columns.
• Go back to step 1.
For column scanning: In contrast to scanning by row (we switch row and column)
2.3. LCD16X2
• Overview
• The LCD (Liquid Crystal Display) display device is widely used in
microcontroller applications because it has the ability to display various
characters, symbols, and graphics in a user-friendly manner, and can be easily
integrated into different application circuits using various communication
protocols (such as 4-bit mode, 8-bit mode, …).
• Pinout diagram
13
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
14
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Overview
The SG90 servo motor is small in size and is the most commonly used type for
small models or mechanisms that do not require heavy force.
The SG90 180-degree servo motor has a fast response speed, but its plastic gears
need to be handled with care when lifting heavy loads to avoid damaging them. The RC
Servo 9G motor has a built-in driver for motor control, making it easy to control the
rotation angle using the PWM pulse width modulation method.
- Pulse width range of 0.5ms to 2.5ms corresponds to a rotation angle
of 0-180 degrees
- Frequency of 50Hz, with a cycle of 20ms
Technical specifications
• Operating voltage: 4.8-5VDC
• Speed: 0.12 sec/60 deg (4.8VDC)
• Torque: 1.6 Kg.cm
• Dimensions: 21x12x22mm
• Weight: 9g
Wiring diagram
- Red: Power positive
- Brown: Power negative
15
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
- Orange: Signal
16
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
2.5. RC522
Figure 9. RC522
RC522 RFID module uses Phillip's MFRC522 IC used to read and write data
for 13.56mhz NFC tags, with a low price and compact design, this module is the
first choice for card reading and writing applications. RFID.
Specifications
• Power: 3.3VDC, 13 - 26mA
• Wave frequency: 13.56MHz
• Operating distance: 0~60mm(mifare1 card)
• Communicate: SPI
• Data transfer rate: 10Mbit/s
• Supported RFID cards: mifare1 S50, mifare1 S70, mifare UltraLight, mifare
Pro, mifare Desfire
• Size: 40mm × 60mm
17
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
2.6 LM393
18
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
3. Component in proteus
3.1. Source circuit
19
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
20
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
21
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
22
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
3.7. RS232
23
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
3.10. RC522
24
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
25
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
26
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
22
27
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Then we choose Save As window will appear, enter the name of the project in the
File name box and click the Save button to save the project to be done (note: Names cannot
start with a number, no sign, don't use space button).
28
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
After successfully saving the newly created project file, the CCS compiler will
ask the programmer to select the microcontroller line and the quartz frequency to use. For
this project, the team used PIC16 in the Family section, PIC16F877A in the Device section
and 4MHz in the Crystal Clock Speed section. Click Create Project to finish creating a new
project
29
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
30
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
To load the code into the microcontroller we do the following steps: File → Import
Hex → Choose file you want to add
31
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
• New chips can be added to the software by updating the software, which is freely
downloaded from the Mircochip website.
3. Visual studio
Microsoft Visual Studio is integrated software from Microsoft. Visual Studio uses
Microsoft's software development platform Visual Studio includes other built-in tools
including a form designer for building application interfaces, web designer, class designer,
and database schema designer.
Visual Studio supports many different programming languages and allows the code
editor and debugger to support almost any programming language. Integrated languages
include C, C++, C++/CLI…
32
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
33
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
4. Code
4.1. CCS
Main program:
34
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Keypad program:
35
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
Servo program:
Code:
#include <main.h>
#FUSES NOWDT,PUT,HS,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(crystal = 4mhz)
#use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7,bits=8)
36
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
#define C1 PIN_B4
#define C2 PIN_B5
#define C3 PIN_A0
#define C4 PIN_A1
#define H1 PIN_B0
#define H2 PIN_B1
#define H3 PIN_B2
#define H4 PIN_B3
37
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
int a=0;
int8 k, kp=0,index = 0,b=1,h=0;
int password[4]={1,2,3,4};
int pw_nhap[4];
int tt=0;
char c;
void enter_pass(){
lcd_init();
lcd_putc("\f");
lcd_gotoxy(3,2);
lcd_putc("PASSWORD OR ID CARD");
}
38
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
{
FOR (int i = 0; i < 5; i++)
{
if(UID[i]== DATA[i] )
{
THE_1=1;
}
else
{
THE_1=0;
break;
}
}
return THE_1;
}
void keyarray()
{
pw_nhap[index]= kp;
index++;
if(index==4) index=0;
}
39
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
void nhap_phim(){
int8 x,a=0;
for (int i=0; i < 5; i++){
port_b_pullups(1);
switch (i){
case 1:{
quet (1);
if (input (H1) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"1");b++;a=1;kp=1;keyarray(); coi_bao(1,100);
}
else if (input (H2) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"4");;b++;a=1;kp=4;keyarray();coi_bao(1,100); }
else if (input (H3) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"7");b++;a=1;kp=7;keyarray(); coi_bao(1,100); }
else if (input (H4) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"*");b++;a=1;kp=10;keyarray();coi_bao(1,100); }
break;
}
case 2:{
40
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
quet (2);
if (input (H1) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"2");b++;a=1;kp=2;keyarray(); coi_bao(1,100); }
else if (input (H2) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"5");b++;a=1;kp=5;keyarray(); coi_bao(1,100); }
else if (input (H3) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"8");b++;a=1;kp=8;keyarray(); coi_bao(1,100); }
else if (input (H4) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"0");b++;a=1;kp=0;keyarray(); coi_bao(1,100); }
break;
}
case 3:{
quet (3);
if (input (H1) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"3");b++;a=1;kp=3;keyarray(); coi_bao(1,100); }
else if (input (H2) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"6");b++;a=1;kp=6;keyarray(); coi_bao(1,100); }
else if (input (H3) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"9");b++;a=1;kp=9;keyarray(); coi_bao(1,100); }
else if (input (H4) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"=");b++;a=1;kp=11;keyarray();coi_bao(1,100);
}
break;
}
case 4:{
quet (4);
if (input (H1) ==
0){LCD_gotoxy(b,1);printf(LCD_putc,"*");b++;a=1;kp=12;keyarray(); coi_bao(1,100);
}
41
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
}
}
void mocua()
{ set_tris_c(0);
output_high(PIN_c0);
delay_us(1000);
output_low(PIN_c0);
delay_us(19000);
void dongcua()
42
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
{ set_tris_c(0);
output_high(PIN_c0);
delay_us(2000);
output_low(PIN_c0);
delay_us(18000);
void check_pass()
{
for(int j=0;j<4;j++){
if(password[j] == pw_nhap[j])
{
tt++;
delay_ms(200);
}
else
{
lcd_putc("\fERROR");
coi_bao(5,100);
delay_ms(500);
break;
}
if (tt>=4)
43
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
lcd_putc("\fWELCOME");
delay_ms(500);
mocua();
delay_ms(1000);
dongcua();
}
}
}
void visual()
{
c = getch ();
if(c=='1') mocua();
if(c=='2') dongcua();
}
void main()
{
CHAR UID[6];
UNSIGNED int TagType;
lcd_init ();
output_low(PIN_A5);
set_tris_c(0x00);
set_tris_d(0x00);
set_tris_b(0x0F);
44
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
set_tris_a(0x03);
set_tris_e(0xFF);
port_b_pullups(1);
enter_pass();
while(TRUE)
{
visual();
nhap_phim();
if(b==5)
{check_pass();
enter_pass();
h=0;
tt=0;
b=1;
}
if(input(PIN_E2)==0)
{
mocua();
delay_ms(1000);
dongcua();}
IF (MFRC522_isCard (&TagType))
{
45
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
IF (MFRC522_ReadCardSerial (&UID))
{
tt_1 = QUET_THE(DATA_KHAI,UID);
tt_2 = QUET_THE(DATA_QUAN,UID);
if( tt_1 == 1)
{
delay_ms(5000);
lcd_putc("\f");
lcd_gotoxy(1,2);
printf(LCD_PUTC, "Cua da duoc dong");
output_low(PIN_C0);
dongcua();
delay_ms(500);
}
else if(tt_2 == 1)
{
printf(LCD_PUTC, "\fTran Anh Quan");
46
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
lcd_gotoxy(1,2);
printf(LCD_PUTC, "WELCOME");
output_high(PIN_C0);
mocua();
delay_ms(5000);
lcd_putc("\f");
lcd_gotoxy(1,2);
printf(LCD_PUTC, "CLOSED");
output_high(PIN_C0);
dongcua();
delay_ms(500);
}
else if (tt_1==0 & tt_2==0)
{
lcd_gotoxy(1, 1);
lcd_putc ("WRONG");
lcd_gotoxy(4, 2);
lcd_putc ("WARNING!!!");
coi_bao(5,100) ;
}
}
MFRC522_Halt () ;
}
}
}
47
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
4.2. Visual
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
48
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
using System.IO.Ports;
using AForge.Video;
using AForge.Video.DirectShow;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private FilterInfoCollection cameras;
private VideoCaptureDevice cam;
public Form1()
{
InitializeComponent();
cameras = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo info in cameras)
{ comboBox2.Items.Add(info.Name); }
comboBox2.SelectedIndex = 0;
khoi_tao_bang(); //2. Khởi tạo bảng dữ liệu: datagridview1
loadData(); // 3. Lấy dữ liệu ở access đưa lên bảng datagrdiview
}
49
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open();
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
groupBox2.Enabled = true;
groupBox3.Enabled = true;
timer1.Start();
}
catch(Exception ex)
{
MessageBox.Show("Error!"+ex.Message);
}
50
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
51
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
52
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
53
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
54
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
loadData();
}
catch
{
MessageBox.Show("Có lỗi");
}
}
if (!String.IsNullOrEmpty(tam))
{
char[] tam2 = tam.ToCharArray();
if (tam2[0] == 0x02)
{
for (int j = 1; j < 13; j++)
{
ma_the = ma_the + tam2[j].ToString();
}
}
if (ma_the != "") label7.Text = ma_the;
}
55
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
56
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
57
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if(cam != null && cam.IsRunning)
{
cam.Stop();
}
cam = new VideoCaptureDevice(cameras[comboBox2.SelectedIndex].MonikerString);
cam.NewFrame += cam_NewFrame;
cam.Start();
}
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
pictureBox3.Image = bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = "C:\\Users\\VICTUS\\pictures";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox3.Image.Save(saveFileDialog1.FileName);
}
}
}
}
58
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
CHAPTER 4: CONCLUSION
1. Conclusion
The circuit has worked with concise, easy-to-understand functions, from which it
can be relied on to develop applications in current life.
2. Drawback
- With this database comparison model, we still have to bring a computer when we need
to use it.
- We have not been able to scan the magnetic card to return data to the computer.
- No password change function.
59
MENTOR: DR. Dang Phuoc Vinh Topic: Digital lock
REFERENCES
[1] Đặng Phước Vinh, Trần Quang Khải, Đoàn Lê Anh, Võ Như Thành, Phạm Anh Đức,
“Lập trình Vi Điều khiển PIC và Thiết Bị Ngoại Vi”, NSB KH và KT.
[2] https://www.youtube.com/watch?v=htoHSMjgVSE&list=LL&index=14&t=97s
[3] https://www.youtube.com/watch?v=zkOL2jri3DE&list=LL&index=3
60