Vous êtes sur la page 1sur 36

Make Your Own Arduino RFID Door

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.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Hardware Software
 Arduino Nano
 RFID RC522
 Piezo Buzzer
 2x LED  Arduino IDE
 2x 330 Resistor  SketchUp
 Keypad 4×4  Github
 Adapter I2C for
LCD
 LCD16X2BL

Wiring the LCD to the Arduino


The LCD has 16 pins which is too much for an Arduino Nano, that’s why it’s
important to have an I2C adapter. This allows running the display from only 2 signal
pins on the Arduino. It is helpful because of the small number of pins that will have to
be controlled from the MCU.

Figure 1: LCD pins

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
The LCDs have a parallel interface, meaning that the MCU has to manipulate several
interface pins at the same time to control the display. The table below gives a
description for each of the pins:

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.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 3: Connections between Arduino and LCD

Next, we’ll add library “<LiquidCrystal_I2C.h>” in Arduino IDE. The library


allows us to connect the LCD to the Arduino. The built-in LiquidCrystal_I2C library
makes it easy to display characters on the LCD.

You can download the LiquidCrystal_I2C.h here.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Please follow the steps below to install a new library into your Arduino IDE.

1) First, download the files from Github. (I’ll be downloading Keypad library.)

2) Zip (.zip) each folder.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
3) Copy the .zip files to your Arduino folder.

4) Open Arduino and add Keypad.zip: : Sketch menu > Include Library > Add
.ZIP Library.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
5) Add keypad library: Sketch menu > Include Library > Keypad.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Codes to test LCD:

1 #include <Wire.h>

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
2

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() {

9 lcd.begin();// initialize the LCD

10

11 lcd.backlight();// Turn on the blacklight and print a message.

12

13 lcd.backlight();

14

15 lcd.print("Hello, world!");

16

17 }

18

19 void loop() {

20

21 }

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 4: LCD connected to Arduino Nano

Connecting the Keypad

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
2 D3

3 D4

4 D5

5 A0

6 D7

7 D8

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 5: Connection between Arduino, LCD, and Keypad

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096
M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 6: LCD and Keypad connected to Arduino

Wiring the RFID

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”.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 7: Connection between Arduino, LCD, and RFID

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 8: Arduino, LCD, and RFID

Adding a buzzer & LEDs


Next step is to add a buzzer and 2 LEDs to simulate a controlled access system. Please
review the wire diagram below (Figure 9). The buzzer is set so that it buzzes
whenever we gain access (unlocked). The red LED is always on when it is locked, but
the green LED will come on when it is unlocked.
In order to protect the modules, I decided to 3D print the housing box/case. If you
don’t have a 3D printer, you can just use a plastic case that allows you to fit all of the
components inside. This is very helpful because the modules will be placed inside and
the only parts outside of the box will be the LEDs, keypad, and LCD.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 9: Wire diagram showing connection between Nano, LCD, Keypad, RFID, and
buzzer

Building The Housing Case/Box

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.

Figure 10: Housing case (top view)


HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096
M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 11: Housing case (bottom view)

When I designed the box, I took these dimensions into consideration:


 Top view:
 2 openings for the LEDs (5.2 mm)
 1 opening for LCD (42.2×7.3 mm)
 1 opening for cable (16×10.5 mm)
 Bottom view:
 1 opening for keypad (27×10 mm)

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.

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Figure 12: Complete housing case with modules placed inside

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
Code diagram

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
15 int relPin;

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

35 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

36

37 #define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
51 const byte numRows= 4; //number of rows on the keypad

52

53 const byte numCols= 4; //number of columns on the keypad

54

55

56

57 char keymap[numRows][numCols]=

58

59 {

60

61 {'1', '2', '3', 'A'},

62

63 {'4', '5', '6', 'B'},

64

65 {'7', '8', '9', 'C'},

66

67 {'*', '0', '#', 'D'}

68

69 };

70

71

72

73 //Code that shows the the keypad connections to the arduino terminals

74

75 byte rowPins[numRows] = {2,3,4,5}; //Rows 0 to 3

76

77 byte colPins[numCols]= {A0,7,8,9}; //Columns 0 to 3

78

79

80

81 //initializes an instance of the Keypad class

82

83 Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

84

85

86

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
87 void setup() {

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

107 Serial.begin(9600); // Initialize serial communications with the PC

108

109 while (!Serial); // Do nothing if no serial port is opened

110

111 SPI.begin(); // Init SPI bus

112

113 mfrc522.PCD_Init(); // Init MFRC522 card

114

115

116

117 for (byte i = 0; i < 6; i++) {

118

119 key.keyByte[i] = 0xFF;

120

121 }

122

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
123 lcd.begin();

124

125 lcd.backlight();

126

127 lcd.setCursor(0,0);

128

129 lcd.clear();

130

131 lcd.print( "BLOCKED" );

132

133 }

134

135

136

137 void readNFC(){ // This function will read the code stored on

138

139 for (byte i =0; i<(mfrc522.uid.size); i++) { // the UID

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
159 void pairNFC(){

160

161 Serial.println("COD in pair");

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

171 long r=0;

172

173 int c=0;

174

175 for(int i=1;i<=EEPROM.read(0);i++){ //The UID cannot be stored on

176

177 switch(i%4){ // one variable, it was needed to be

178

179 case 1 :{AUX[0]=EEPROM.read(i); break;} // split

180

181 case 2 :{AUX[1]=EEPROM.read(i); break;}

182

183 case 3 :{AUX[2]=EEPROM.read(i); break;}

184

185 case 0 :{AUX[3]=EEPROM.read(i); break;}

186

187 }

188

189 if((i)%4==0)

190

191 {Serial.println(r);

192

193 if( AUX[0]==COD[0] && AUX[1]==COD[1] && AUX[2]==COD[2] && AUX[3]==COD[3]


){ //Verify if the code is in EEPROM
194

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
195

196 lcd.clear();

197

198 lcd.setCursor(0,0);

199

200 lcd.print("CODE ALREADY IN");

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

218 if(c==0){int aux2=EEPROM.read(0);

219

220 Serial.println("CODE PAIRED");

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
231

232 EEPROM.write(aux2+1,COD[0]); //Writing code in EEPROM

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

242 aux2=aux2+4; // Position for a new code

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

256 lcd.print("CODE PAIRED");

257

258 delay(2000);}

259

260 }

261

262

263

264 boolean validationNFC(){

265

266 boolean c=false;

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
267

268 for(int i=1;i<=EEPROM.read(0);i++){ //Read the EEPROM

269

270 switch(i%4){

271

272 case 1 :{AUX[0]=EEPROM.read(i); break;}

273

274 case 2 :{AUX[1]=EEPROM.read(i); break;}

275

276 case 3 :{AUX[2]=EEPROM.read(i); break;}

277

278 case 0 :{AUX[3]=EEPROM.read(i); break;}

279

280 }

281

282 if((i)%4==0)

283

284 {

285

286 if( AUX[0]==COD[0] && AUX[1]==COD[1] && AUX[2]==COD[2] && AUX[3]==COD[3])

287

288 c=true; //Verify if the code is in EEPROM and make flag=true;

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

302 { //with the accessCode;

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
303

304 if(a.equals(accessCode))

305

306 return 1;

307

308 else if(a.equals(codpairing)) return 2;

309

310 else return 0;

311

312 }

313

314

315

316 String takeCode(char x) //Will display on the LCD the code typed

317

318 { char vec[10];

319

320 vec[0]=x;

321

322 lcd.setCursor(0,0);

323

324 lcd.clear();

325

326 lcd.print('X');

327

328 for(int i=1;i<8;i++)

329

330 {vec[i]=myKeypad.waitForKey(); //Waits for 8 keys to be pressed and after that

331

332 lcd.print('X');} //is taking the decision

333

334 vec[8]=NULL;

335

336 String str(vec);

337

338 return str;

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
339

340 }

341

342

343

344 void loop() {

345

346

347

348 switch(state){

349

350 case 0: {

351

352 mfrc522.PCD_Init();

353

354 if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ){

355

356

357

358 readNFC(); //It will read the card and it will search for UID in its

359

360 if(validationNFC()) //memory

361

362 { state=1;

363

364 lcd.clear();

365

366 lcd.setCursor(0,0);

367

368 lcd.print( "VALID NFC CODE" ); //The door will be opened

369

370 delay(1000);

371

372 return;

373

374 }

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
375

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

390 lcd.print( "BLOCKED" );

391

392 return;

393

394 }

395

396 }

397

398

399

400 char c=myKeypad.getKey();

401

402 if(c != NO_KEY){

403

404

405

406 String codcurent=takeCode(c);

407

408 int A=compareCODE(codcurent);

409

410 if(A==0){ //A is a variable that stores the current code

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
411

412 lcd.clear();

413

414 lcd.print("INVALID CODE");

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

434 lcd.print( "VALID CODE " );

435

436 delay(2000);

437

438 state = 1;

439

440 Return;

441

442 }

443

444 if(A==2); {

445

446 state=2;

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
447

448 lcd.clear();

449

450 lcd.setCursor(0,0);

451

452 lcd.print( " Pairing..." );

453

454 delay(2000);

455

456 return;}

457

458 }

459

460 break;

461

462 }

463

464

465

466 case 1:{

467

468 lcd.clear();

469

470 lcd.setCursor(0,0);

471

472 lcd.print( "UNLOCKED" );

473

474 digitalWrite(A3,LOW);

475

476 digitalWrite(A1,LOW); //The red LED will be off

477

478 digitalWrite(A2,HIGH); //The green LED will be on

479

480 tone(6,3000,5010); //The buzzer will make a sound

481

482 delay(5000); //After 5 seconds the system will be blocked

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
483

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

496 lcd.print( "BLOCKED" );

497

498 return;

499

500 }

501

502

503

504 case 2:{

505

506 mfrc522.PCD_Init();

507

508 if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ){

509

510 readNFC();

511

512 pairNFC();

513

514 state=0;

515

516 delay(2000);

517

518 lcd.clear();

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com
519

520 lcd.setCursor(0,0);

521

522 lcd.print( "BLOCKED" ); }

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

HAL Education54, Avantika Nilaya,Bethal Nagar,Chennai 600096


M+91-91762-73514 ,E-abhishek@haleducation.com

Vous aimerez peut-être aussi