Académique Documents
Professionnel Documents
Culture Documents
Lock
Description
What is RFID?
“RFID stands for Radio-Frequency Identification. […] The RFID device serves the
same purpose as a bar code or a magnetic strip on the back of a credit card or ATM
card; it provides a unique identifier for that object. And, just as a bar code or
magnetic strip must be scanned to get the information, the RFID device must be
scanned to retrieve the identifying information.” – .
RFID is used in this project to read data from the RFID tags and send the information
to the non-volatile memory of the MCU. The ID read from the tags is compared with
the stored information, and if it matches, then the door unlocks/opens.
First, we will make the connections between LCD and I2C. To do that, we need an
I2C LCD display adapter (LCD1602). The adapter converts the 16 x 2 character LCD
display into a serial I2C LCD that can be controlled via Arduino by just 2 wires.
1) First, download the files from Github. (I’ll be downloading Keypad library.)
4) Open Arduino and add Keypad.zip: : Sketch menu > Include Library > Add
.ZIP Library.
1 #include <Wire.h>
3 #include <LiquidCrystal_I2C.h>
5 LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
7 void setup() {
10
12
13 lcd.backlight();
14
15 lcd.print("Hello, world!");
16
17 }
18
19 void loop() {
20
21 }
Now, on to the keypad! We will connect the keypad in order to display the numbers
on the LCD that we input from the keypad.
Keypad.h is a library which allows the Arduino to read a matrix type keypad.
I used a 4×4 keypad in this tutorial.
The table shows the connection between the Arduino board and the keypad. The
keypad pins are connected to Arduino’s digital output pins. Pin D6 was used for the
buzzer because it was a PWM pin.
Keypad pin Arduino pin
1 D2
3 D4
4 D5
5 A0
6 D7
7 D8
Next, we will add the RFID. In this case, the RFID board uses a SPI communication
protocol, where the Arduino will be acting as a master and the RFID reader as a
slave. The card reader and the tags are designed to communicate using a frequency
equal to 13.56MHz.
This is an important step because it helps us to read data from the card and it will
decide if the ID corresponds to the information stored in the EEPROM. If it matches,
it will give us access and display “Unlocked”. Otherwise, the LCD will display
“Blocked”.
For this project, I wanted to create a custom case to house all of the components
neatly and keep them free of damage.
I have designed a box using SketchUp, which has a user-friendly interface with simple
buttons like Eraser, Lines, and Tape Measure Tool.
HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096
M+91-91762-73514 ,E-abhishek@haleducation.com
The dimensions of the box is 120x125x37 mm.
If you’re not familiar with Sketchup, I recommend you go through some SketchUp
tutorials here.
After I measured the dimensions of the components, I designed a box that’s rather
compact. Feel free to change the design to your preference.
Full Code
1 #include <SPI.h>
3 #include <MFRC522.h>
5 #include <Wire.h>
7 #include <LiquidCrystal_I2C.h>
9 #include <Keypad.h>
10
11 #include<EEPROM.h>
12
13
14
16
17 int state=0;
18
19 byte COD[10];
20
21 byte AUX[10];
22
23 int k=0;
24
25 String accessCode="*123456#";
26
27 String codpairing="*654321#";
28
29 //NFC
30
31 #define RST_PIN 9
32
33 #define SS_PIN 10
34
36
38
39 MFRC522::MIFARE_Key key;
40
41
42
43 //LCD
44
45 LiquidCrystal_I2C lcd(0x27,16,2);
46
47
48
49 //KEYPAD
50
52
54
55
56
57 char keymap[numRows][numCols]=
58
59 {
60
62
64
66
68
69 };
70
71
72
73 //Code that shows the the keypad connections to the arduino terminals
74
76
78
79
80
82
84
85
86
88
89 pinMode(A0,OUTPUT);
90
91 digitalWrite(A0,HIGH);
92
93 pinMode(A3,OUTPUT);
94
95 digitalWrite(A3,HIGH);
96
97 pinMode(A1,OUTPUT);
98
99 digitalWrite(A1,HIGH);
100
101 pinMode(A2,OUTPUT);
102
103 digitalWrite(A2,LOW);
104
105 //NFC
106
108
110
112
114
115
116
118
120
121 }
122
124
125 lcd.backlight();
126
127 lcd.setCursor(0,0);
128
129 lcd.clear();
130
132
133 }
134
135
136
137 void readNFC(){ // This function will read the code stored on
138
140
141 COD[i]=mfrc522.uid.uidByte[i];
142
143 }
144
145 Serial.print("COD");
146
147 Serial.print(COD[0]);
148
149 Serial.print(COD[1]);
150
151 Serial.print(COD[2]);
152
153 Serial.print(COD[3]);
154
155 }
156
157
158
160
162
163 Serial.print(COD[0]);
164
165 Serial.print(COD[1]);
166
167 Serial.print(COD[2]);
168
169 Serial.print(COD[3]);
170
172
174
176
178
180
182
184
186
187 }
188
189 if((i)%4==0)
190
191 {Serial.println(r);
192
196 lcd.clear();
197
198 lcd.setCursor(0,0);
199
201
202 lcd.setCursor(0,1);
203
204 lcd.print("SYSTEM");
205
206 delay(2000);
207
208 c=1;
209
210 break;}
211
212 }
213
214 }
215
216
217
219
221
222 Serial.print(COD[0]);
223
224 Serial.print(COD[1]);
225
226 Serial.print(COD[2]);
227
228 Serial.print(COD[3]);
229
230
233
234 EEPROM.write(aux2+2,COD[1]);
235
236 EEPROM.write(aux2+3,COD[2]);
237
238 EEPROM.write(aux2+4,COD[3]);
239
240
241
243
244 Serial.println("aux2");
245
246 Serial.println(aux2);
247
248 EEPROM.write(0,0);
249
250 EEPROM.write(0,aux2);
251
252 lcd.clear();
253
254 lcd.setCursor(0,0);
255
257
258 delay(2000);}
259
260 }
261
262
263
265
269
270 switch(i%4){
271
273
275
277
279
280 }
281
282 if((i)%4==0)
283
284 {
285
287
289
290 }
291
292 }
293
294 return c;
295
296 }
297
298
299
300 int compareCODE(String a) //We type a code on keypad and this will be compared
301
304 if(a.equals(accessCode))
305
306 return 1;
307
309
311
312 }
313
314
315
316 String takeCode(char x) //Will display on the LCD the code typed
317
319
320 vec[0]=x;
321
322 lcd.setCursor(0,0);
323
324 lcd.clear();
325
326 lcd.print('X');
327
329
331
333
334 vec[8]=NULL;
335
337
340 }
341
342
343
345
346
347
348 switch(state){
349
350 case 0: {
351
352 mfrc522.PCD_Init();
353
355
356
357
358 readNFC(); //It will read the card and it will search for UID in its
359
361
362 { state=1;
363
364 lcd.clear();
365
366 lcd.setCursor(0,0);
367
369
370 delay(1000);
371
372 return;
373
374 }
376 else{
377
378 lcd.clear();
379
380 lcd.setCursor(0,0);
381
382 lcd.print( "INVALID NFC CODE" ) //If the code was wrongblocked
383
384 delay(1000);
385
386 lcd.setCursor(0,0);
387
388 lcd.clear();
389
391
392 return;
393
394 }
395
396 }
397
398
399
401
403
404
405
407
409
412 lcd.clear();
413
415
416 delay(2000);
417
418 lcd.setCursor(0,0);
419
420 lcd.clear();
421
422 lcd.print("BLOCKED");
423
424 return;
425
426 }
427
428 if(A==1){
429
430 lcd.setCursor(0,0);
431
432 lcd.clear();
433
435
436 delay(2000);
437
438 state = 1;
439
440 Return;
441
442 }
443
444 if(A==2); {
445
446 state=2;
448 lcd.clear();
449
450 lcd.setCursor(0,0);
451
453
454 delay(2000);
455
456 return;}
457
458 }
459
460 break;
461
462 }
463
464
465
467
468 lcd.clear();
469
470 lcd.setCursor(0,0);
471
473
474 digitalWrite(A3,LOW);
475
477
479
481
484 digitalWrite(A3,HIGH);
485
486 digitalWrite(A1,HIGH);
487
488 digitalWrite(A2,LOW);
489
490 state=0;
491
492 lcd.setCursor(0,0);
493
494 lcd.clear();
495
497
498 return;
499
500 }
501
502
503
505
506 mfrc522.PCD_Init();
507
509
510 readNFC();
511
512 pairNFC();
513
514 state=0;
515
516 delay(2000);
517
518 lcd.clear();
520 lcd.setCursor(0,0);
521
523
524
525
526 break;
527
528 }
529
530 }
531
Conclusion
So, we managed to build a low-cost Arduino RFID door lock. It was an interesting
project for me because I made it for my personal use. Moreover, building such a
device will provide you not only the satisfaction of doing something useful, but also a
lot of knowledge and an initiation in the first steps of custom electronics. Dealing with
a wide range of boards will be definitely very challenging. The project presents a lot
of interesting tasks, such as:
Working with other Arduino libraries
Understanding the use of EEPROM
Working with multiple communications like I2C, SPI