<p><a href="https://www.youtube.com/watch?v=BYBiCHGGIzQ">https://www.youtube.com/watch?v=BYBiCHGGIzQ</a><br> A friend and I teamed up at school for a capstone project. We built a safe powered by an Arduino. The safe can be unlocked by either putting in a pin code or scanning an RFID tag. 3 wrong attempts will cause a timeout and not allow anything to happen. A correct password or RFID tag will open the door. There are alarms that will sound if the safe is picked up. The alarm can be deactivated by flipping a switch on the inside of the box.</p> <p>Here is the info:</p> <p>Components:<br> 1 Arduino Mega 2560<br> 1 RFID Module -RC522<br> 1 Ultrasonic Sensor<br> 1 Servo Motor<br> 1 Active Buzzer<br> 1 Membrance Switch Module (4x4 keypad)<br> 1 RGB 10mm LED<br> 1 Resistor (330Ω)<br> 3 Relays, TE (OMR-C-105H)<br> 2 Buzzers, Radio Shack 7-14VDC 108dB<br> 1 9V battery with power supply adapter battery holder<br> 2 23A 12V Batteries with battery holder<br> 1 Electrolytic Capacitor (100uF 50V)<br> 3 170 tie-Points Mini Breadboard<br> 1 Mega 2560 case<br> 1 main power switch<br> Jump wires<br> 22 AWG stranded<br> 22 AWG solid</p> <p>The Code:<br> #include <Servo.h> // servo motor library (0-180 degrees)<br> #include <MFRC522.h> // Include of the RC522 Library<br> #include <SPI.h> // Used for communication via SPI with the Module (RC522)<br> #include <Keypad.h> // 4x4 keypad library<br> #include <Password.h> // password library (holds password)</p> <p>int ledGreen = 6; // Green LED<br> int ledRed = 4; // Red LED<br> int ledBlue = 7; // Blue LED<br> int val = 0; // variable to store the read value<br> int count = 0; // counter<br> int relayLED = 11; // 12 LED panel inside the safe<br> int alarmRelay1 = 8; // 108dB buzzer for ultrasonic sensor<br> int alarmSwitch = 10; // switch to turn off alarm<br> int alarmState; // detects change of state on the alarm switch<br> int alarmRelay2 = 26; // 108dB buzzer for ultrasonic sensor<br> int trigPin = 22; // trigger pin on the ultrasonic sensor<br> int echoPin = 24; // echo pin on the ultrasonic sensor<br> int setDistance = 6; // 6cm set max distance</p> <p>long duration; // return ping detection variable, duration of ping<br> int distance;// variable storing distance</p> <p>#define SDAPIN 53 // RFID Module SDA Pin is connected to the MEGA 53 Pin<br> #define RESETPIN 5 // RFID Module RST Pin is connected to the MEGA 5 Pin</p> <p>#define Buzzer 3 // Pin 3 connected to + pin of the Buzzer</p> <p>byte FoundTag; // Variable used to check if Tag was found<br> byte ReadTag; // Variable used to store anti-collision value to read Tag information<br> byte TagData[MAX_LEN]; // Variable used to store Full Tag Data<br> byte TagSerialNumber[4]; // Variable used to store only Tag Serial Number<br> byte GoodTagSerialNumber[4] = {0x13, 0xB8, 0xC0, 0x1}; // The Tag Serial number we are looking for</p> <p>String newPasswordString;// hold new password<br> char newPassword[6]; // up to 6 digits for password<br> Password password = Password("2234"); // the correct password is 2234</p> <p>byte maxPasswordLength = 6; // 6 digits max input for password<br> byte currentPasswordLength = 0; // same as above<br> const byte ROWS = 4; // 4 rows on keypad<br> const byte COLS = 4; // 4 colums for keypad</p> <p>char keys[ROWS][COLS] = {<br> {'1','2','3','A'},<br> {'4','5','6','B'},<br> {'7','8','9','C'},<br> {'*','0','#','D'},<br> }; // keypad map layout</p> <p>byte rowPins[ROWS] = {48,46,44,42}; // Pin out for rows on keypad<br> byte colPins[COLS] = {49,47,45,43}; // Pin out for colums on keypad</p> <p>Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // keymap inputs</p> <p>MFRC522 nfc(SDAPIN, RESETPIN); // Init of the library using the MEGA pins declared above</p> <p>Servo myServo; // Naming the servo motor</p> <p>void setup() {<br> myServo.attach(A1); // Servo motor connected to pin A1<br> pinMode(Buzzer, OUTPUT); // Set buzzer pin to an output pin<br> pinMode(ledGreen, OUTPUT); // Set green LED to an output pin<br> pinMode(ledRed, OUTPUT); // Set red LED to an output pin<br> pinMode(ledBlue, OUTPUT); // Set blue LED to an output pin<br> pinMode(alarmRelay1, OUTPUT); // Set alarm relay1 buzzer to an output<br> pinMode(alarmSwitch, INPUT); // Set alarm switch to an input<br> pinMode(relayLED, OUTPUT); // Set relay LED panel to an output<br> pinMode(alarmRelay2, OUTPUT); // Set alarm relay2 buzzer to an output<br> pinMode(trigPin, OUTPUT); // Set trigger pin to an output<br> pinMode(echoPin, INPUT); // Set echo pin to an input<br> digitalWrite(relayLED, LOW); // Relay LED panel off at startup<br> digitalWrite(alarmRelay1, LOW); // alarm relay1 buzzer off at startup<br> digitalWrite(alarmRelay2, LOW); // alarm relay2 buzzer off at startup<br> digitalWrite(Buzzer, LOW); // Buzzer Off at startup<br> digitalWrite(ledGreen, LOW); // Green LED Off at startup<br> digitalWrite(ledRed, LOW); // Red LED Off at startup<br> digitalWrite(ledBlue, LOW); // Blue LED Off at startup<br> SPI.begin(); // communication begins for the RFID<br> Serial.begin(115200); // communication will print on serial monitor 115200</p> <p>// Start to find an RFID Module<br> Serial.println("Looking for RFID Reader"); // searching for RFID reader, printed on serial monitor 115200<br> nfc.begin(); // start near field communication with RFID<br> byte version = nfc.getFirmwareVersion(); // Variable to store Firmware version of the Module</p> <p>// If can't find an RFID Module<br> if (! version) {<br> Serial.print("Didn't find RC522 board."); // the RFID module could not be found, printed on serial monitor 115200<br> while(1); //Wait until a RFID Module is found<br> }</p> <p>// If found, print the information about the RFID Module<br> Serial.print("Found chip RC522 "); //RFID found, printed on serial monitor 115200<br> Serial.print("Firmware version: 0x"); // print RFID reader software version for RC522<br> Serial.println(version, HEX); // print on binary hex decimal<br> Serial.println(); // print RFID card or fob on serial monitor 115200<br> }</p> <p>void loop() {</p> <p>char key = keypad.getKey(); // password to be entered on keypad<br> if(key!=NO_KEY){<br> delay(60); // if no key is intered delay .06 seconds<br> switch(key){<br> case'#':checkPassword(); break; // # button on the keypad will check password<br> case'*':lockSafe(); break; // * button on keypad will lock the safe<br> default: processNumberKey(key); // process password for correct password<br> }<br> }</p> <p>digitalWrite(trigPin, LOW); // trigger pin on low state<br> delayMicroseconds(2);// delay 2 micro second</p> <p>digitalWrite(trigPin, HIGH); // trigger pin turns high<br> delayMicroseconds(10);// delay 10 micro seconds<br> digitalWrite(trigPin, LOW); // trigger pin turns low</p> <p>duration = pulseIn(echoPin, HIGH);// waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing.<br> distance = duration*0.034/2; // duration x .034cm/Micro Second / 2</p> <p>String GoodTag="False"; // Variable used to confirm good Tag Detected</p> <p>// Check to see if a Tag was detected<br> // If yes, then the variable FoundTag will contain "MI_OK"<br> FoundTag = nfc.requestTag(MF1_REQIDL, TagData);</p> <p>if (FoundTag == MI_OK) {<br> delay(200); // delay .2 second</p> <p>// Get anti-collision value to properly read information from the Tag<br> ReadTag = nfc.antiCollision(TagData);<br> memcpy(TagSerialNumber, TagData, 4); // Write the Tag information in the TagSerialNumber variable</p> <p>Serial.println("Tag detected."); // tag detected printed on serial monitor<br> Serial.print("Serial Number: "); // print serial number: on serial monitor<br> for (int i = 0; i < 4; i++) { // Loop to print serial number to serial monitor<br> Serial.print(TagSerialNumber[i], HEX); // print tag number in hex decimal<br> Serial.print(", "); // print tage numbers after Serial number:<br> }<br> Serial.println(""); // print on next line<br> Serial.println(); // space printed</p> <p>// Check if detected Tag has the right Serial number we are looking for<br> for(int i=0; i < 4; i++){<br> if (GoodTagSerialNumber[i] != TagSerialNumber[i]) {<br> break; // if not equal, then break out of the "for" loop<br> }<br> if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching<br> GoodTag="TRUE"; // tag maches code<br> }<br> }<br> if (GoodTag == "TRUE"){<br> Serial.println("ACCESS GRANTED!"); // access grented printed on serial monitor<br> Serial.println(); // space printed<br> myServo.write(180); // servo motor moves from 0 to 180 degrees (safe unlocks)<br> digitalWrite(relayLED, HIGH); // LED panel turns on</p> <p>for (int y = 0; y < 3; y++){<br> digitalWrite (Buzzer, HIGH) ;// Buzzer On<br> delay (50) ;// Delay .05 seconds<br> digitalWrite (ledGreen, HIGH); // green LED turns on<br> delay (50); // delay .05 seconds<br> digitalWrite (Buzzer, LOW) ;// Buzzer Off<br> delay (50) ;// delay .05 seconds<br> digitalWrite (ledGreen, LOW); // green LED turns off<br> delay (50); // delay .05 seconds<br> count = 0; // counter reset to zero<br> }<br> delay(1500); // delay 1 1/2 seconds<br> }<br> else {<br> Serial.println("ACCESS DENIED!"); // access denied printed on serial monitor<br> Serial.println(); // space printed</p> <p>for (int y = 0; y < 3; y++){<br> digitalWrite (Buzzer, HIGH) ;// Buzzer On<br> delay (300) ;// Delay .3 seconds<br> digitalWrite (ledRed, HIGH); // red LED turns on<br> delay (300); // delay .3 seconds<br> digitalWrite (Buzzer, LOW) ;// Buzzer Off<br> delay (400) ;// delay .4 second<br> digitalWrite (ledRed, LOW); // red LED turns off<br> delay (400); // delay .4 seconds<br> count ++; // counter incresses<br> }<br> delay(500); // delay 1/2 seconds<br> }<br> }<br> // when counter reaches 9 the safe times out for 15 seconds<br> if (count >= 9){<br> count = 0; // counter reset when couter reaches 9 or over 9<br> digitalWrite(ledBlue, HIGH); // blue LED turns on<br> delay(15000); // delay 15 seconds<br> digitalWrite(ledBlue,LOW); // blue LED turns off<br> }<br> alarmState = digitalRead(alarmSwitch); // the alarm switch will determind the alarm state<br> if(alarmState == HIGH){<br> digitalWrite(alarmRelay1, LOW); // if alarm switch is high, then the alarm relay1 turns off (disables the alarm)<br> digitalWrite(alarmRelay2, LOW);} // if alarm switch is high, then the alarm relay2 turns off (disables the alarm)<br> else{<br> if(distance>setDistance){<br> digitalWrite(alarmRelay1, HIGH); // if the the distance measured of the ultra sonic sensor is greater then the set distance, then alarm realy1 goes high (alarm turns on)<br> digitalWrite(alarmRelay2, HIGH); // if the the distance measured of the ultra sonic sensor is greater then the set distance, then alarm realy2 goes high (alarm turns on)<br> }<br> else{<br> digitalWrite(alarmRelay1, LOW); // if the the distance measured is less then the set distance then the alarm relay1 goes low (alarm turns off)<br> digitalWrite(alarmRelay2, LOW); // if the the distance measured is less then the set distance then the alarm relay2 goes low (alarm turns off)<br> }<br> }<br> }</p> <p>void processNumberKey(char key){<br> Serial.print(key); // serial print the keys being pressed on the keypad<br> currentPasswordLength++; // keep adding inputed keys to the current password<br> password.append(key); // add password key<br> if(currentPasswordLength==maxPasswordLength){<br> checkPassword(); // if the max length of the password allowed is met, then check password<br> }<br> }</p> <p>void checkPassword(){<br> if(password.evaluate()){<br> Serial.println("ACCESS GRANTED!"); // if password is a mach then access granted is printed on the serial print<br> myServo.write(180); // servo motor turns to 180 degrees ( opens)<br> digitalWrite(relayLED, HIGH); // LED light panel turns on</p> <p>for (int y = 0; y < 3; y++){<br> digitalWrite (Buzzer, HIGH) ;// Buzzer On<br> delay (50) ;// Delay .05 seconds<br> digitalWrite (ledGreen, HIGH); // green LED turns on<br> delay (50); // delays .05 second<br> digitalWrite (Buzzer, LOW) ;// Buzzer Off<br> delay (50) ;// delay .05 seconds<br> digitalWrite (ledGreen, LOW); // green LED turns off<br> delay (50); // delays .05 seconds<br> count = 0; // counter goes back to zero<br> }<br> delay(1500); // delays 1.5 seconds<br> }<br> else {<br> Serial.println(" ACCESS DENIED!"); // access denied printed on serial print<br> Serial.println(); // space printed</p> <p>for (int y = 0; y < 3; y++){<br> digitalWrite (Buzzer, HIGH) ;// Buzzer On<br> delay (300) ;// Delay .3 seconds<br> digitalWrite (ledRed, HIGH); // red LED turns on<br> delay (300); // delays .3 seconds<br> digitalWrite (Buzzer, LOW) ;// Buzzer Off<br> delay (400) ;// delay .4 seconds<br> digitalWrite (ledRed, LOW); // red LED turns off<br> delay (400); // delays .4 seconds<br> count ++; // counter starts countin up<br> }<br> delay(500); // delays .5 seconds<br> }<br> resetPassword(); // conduct resetPassword proccess<br> }</p> <p>void resetPassword(){<br> password.reset(); // password for keypad reset<br> currentPasswordLength=0; // password goes to zero inputs<br> }</p> <p>void lockSafe(){<br> Serial.println("SAFE LOCKED!"); // safe locked printed on serial monitor<br> Serial.println(); // space printed<br> myServo.write(90); // servo motor moves back to 90 degrees<br> digitalWrite(relayLED, LOW); // LED panel turns off<br> }<br> <img src="https://steemitimages.com/DQmV3BZNvWSuHJqCtoGX75bVvbixP9bMNziw1ghDyLyYyUq/wiring%20diagram.PNG" alt="wiring diagram.PNG"></p>