Components and supplies
Relay (generic)
Arduino UNO
Resistor 221 ohm
Resistor 2.21k ohm
Breadboard (generic)
Male/Female Jumper Wires
Jumper wires (generic)
General Purpose Transistor NPN
Resistor 10k ohm
Resistor 1k ohm
RFID Module (Generic)
HC-05 Bluetooth Module
Apps and platforms
Arduino IDE
MIT App Inventor 2
Project description
Code
LED ON & OFF CODE
arduino
1int BLU; 2 3int led = 8;// add the pin number that you want to control. 4 5void setup() 6 7{ 8 9 Serial.begin(9600); 10 11 pinMode(led, OUTPUT); 12 13} 14 15void loop() { 16 17 while (Serial.available()) { 18 19 BLU = Serial.read(); 20 21 Serial.println(BLU); 22 { 23 if (BLU == 1) 24 25 digitalWrite(led, HIGH); 26 } 27 28 { 29 if (BLU == 0) 30 31 digitalWrite(led, LOW); 32 } 33 34 } 35 36} 37
RFID + RELAY CODE
arduino
1/* 2 * ---------------------------------------------------------------------------- 3 * This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid 4 * for further details and other examples. 5 * 6 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it. 7 * 8 * This sketch show a simple locking mechanism using the RC522 RFID module. 9 * ---------------------------------------------------------------------------- 10 * Typical pin layout used: 11 * ----------------------------------------------------------------------------------------- 12 * MFRC522 Arduino Arduino Arduino Arduino Arduino 13 * Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro 14 * Signal Pin Pin Pin Pin Pin Pin 15 * ----------------------------------------------------------------------------------------- 16 * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST 17 * SPI SS SDA(SS) 10 53 D10 10 10 18 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 19 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 20 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 21 * 22 */ 23#include <SPI.h> 24#include <MFRC522.h> 25 26#define RST_PIN 9 // Configurable, see typical pin layout above 27#define SS_PIN 10 // Configurable, see typical pin layout above 28 29MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. 30 31String read_rfid; // Add how many you need and don't forget to include the UID. 32String ok_rfid_1="e199312d"; // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to. 33String ok_rfid_2="fbecb673"; // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally. 34int lock = 7; // For the Card. 35int lock2 = 7; // For the Keyfob. 36/* 37 * Initialize. 38 */ 39void setup() { 40 Serial.begin(9600); // Initialize serial communications with the PC 41 while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) 42 SPI.begin(); // Init SPI bus 43 mfrc522.PCD_Init(); // Init MFRC522 card 44 45 //Choose which lock below: 46 pinMode(lock, OUTPUT); 47 pinMode(lock2, OUTPUT); 48} 49/* 50 * Helper routine to dump a byte array as hex values to Serial. 51 */ 52void dump_byte_array(byte *buffer, byte bufferSize) { 53 read_rfid=""; 54 for (byte i = 0; i < bufferSize; i++) { 55 read_rfid=read_rfid + String(buffer[i], HEX); 56 } 57} 58 59void open_lock() { 60 //Use this routine when working with Relays and Solenoids etc. 61 digitalWrite(lock,HIGH); 62 delay(500); 63 digitalWrite(lock,LOW); 64} 65 66 67void close_lock2() { // You can also just use the card to shutdown your PC. This is just for those moments that you really need to shut it down quickly. 68 //Use this routine when working with Relays and Solenoids etc. 69 digitalWrite(lock2, HIGH); 70 delay(5000); 71 digitalWrite(lock2,LOW); 72} 73 74 75void loop() { 76 77 // Look for new cards 78 if ( ! mfrc522.PICC_IsNewCardPresent()) 79 return; 80 81 // Select one of the cards 82 if ( ! mfrc522.PICC_ReadCardSerial()) 83 return; 84 85 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); 86 Serial.println(read_rfid); 87 if (read_rfid==ok_rfid_1) { 88 //ok, open the door. 89 open_lock(); 90 } 91 92 Serial.println(read_rfid); 93 if (read_rfid==ok_rfid_2) { 94 //ok, open the door. 95 close_lock2(); 96 } 97 //Add below as many "keys" as you want 98 //if (read_rfid==ok_rfid_2) { 99 //also ok, open the door 100 // open_lock(); 101 //} 102 // else not needed. Anything else is not ok, and will not open the door... 103} 104
RFID + HC-05 + RELAY CODE
arduino
1/* 2 ---------------------------------------------------------------------------- 3 This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid 4 for further details and other examples. 5 6 NOTE: The library file MFRC522.h has a lot of useful info. Please read it. 7 8 This sketch show a simple locking mechanism using the RC522 RFID module. 9 ---------------------------------------------------------------------------- 10 Typical pin layout used: 11 ----------------------------------------------------------------------------------------- 12 MFRC522 Arduino Arduino Arduino Arduino Arduino 13 Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro 14 Signal Pin Pin Pin Pin Pin Pin 15 ----------------------------------------------------------------------------------------- 16 RST/Reset RST 9 5 D9 RESET/ICSP-5 RST 17 SPI SS SDA(SS) 10 53 D10 10 10 18 SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 19 SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 20 SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 21 22*/ 23 24#include <SPI.h> 25#include <MFRC522.h> 26#define RST_PIN 9 // Configurable, see typical pin layout above 27#define SS_PIN 10 // Configurable, see typical pin layout above 28MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. 29 30String read_rfid; // Add how many you need and don't forget to include the UID. 31String ok_rfid_1 = "e199312d"; // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to. 32String ok_rfid_2 = "fbecb673"; // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally. 33int switch1 = 7; // For the Card. 34int switch2 = 7; // For the Keyfob. 35int BlueCom; // Bluetooth Command. 36int Thing = 8; // Pin number where the Thing is connected. 37 38void setup() { 39 40 Serial.begin(9600); // Initialize serial communications with the PC 41 while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) 42 SPI.begin(); // Init SPI bus 43 pinMode(Thing, OUTPUT); // Set Thing Pin as OUTPUT. 44 pinMode(switch1, OUTPUT); // RFID Card to turn ON PC. 45 pinMode(switch2, OUTPUT); // KeyFob to shut it down. 46 47 48} 49/* 50 Helper routine to dump a byte array as hex values to Serial. 51*/ 52void dump_byte_array(byte *buffer, byte bufferSize) { 53 read_rfid = ""; 54 for (byte i = 0; i < bufferSize; i++) { 55 read_rfid = read_rfid + String(buffer[i], HEX); 56 } 57} 58 59void On_Switch() { // Use this routine when working with Relays and Solenoids etc. 60 digitalWrite(switch1, HIGH); 61 delay(1000); 62 digitalWrite(switch1, LOW); 63} 64 65void Off_Switch() { // You can also just use the card to shutdown your PC. This is just for those moments that you really need to shut it down quickly. 66 digitalWrite(switch2, HIGH); // Use this routine when working with Relays and Solenoids etc. 67 delay(5000); 68 digitalWrite(switch2, LOW); 69} 70 71void BlueOn() { 72 73 digitalWrite(Thing, HIGH); 74 mfrc522.PCD_Init(); 75} 76 77void BlueOff() { 78 79 digitalWrite(Thing, LOW); 80 mfrc522.PCD_Reset(); 81} 82 83void loop() { 84 85 while (Serial.available()) { 86 BlueCom = Serial.read(); 87 Serial.println(BlueCom); 88 { 89 90 if (BlueCom == 1) 91 BlueOn(); 92 } 93 { 94 95 if (BlueCom == 0) 96 BlueOff(); 97 } 98 } 99 100 // Look for new cards 101 if (!mfrc522.PICC_IsNewCardPresent()) 102 return; 103 104 // Select one of the cards 105 if (!mfrc522.PICC_ReadCardSerial()) 106 return; 107 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); 108 109 Serial.println(read_rfid); 110 if (read_rfid == ok_rfid_1) { //ok, turn ON PC. 111 digitalWrite(switch1, HIGH); 112 delay(1000); 113 digitalWrite(switch1, LOW); 114 } 115 Serial.println(read_rfid); 116 if (read_rfid == ok_rfid_2) { //ok, turn OFF PC. 117 digitalWrite(switch2, HIGH); 118 delay(5000); 119 digitalWrite(switch2, LOW); 120 } 121} 122
RFID + HC-05 + RELAY CODE
arduino
1/* 2 ---------------------------------------------------------------------------- 3 4 This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid 5 6 for further details and other examples. 7 8 NOTE: The library file MFRC522.h 9 has a lot of useful info. Please read it. 10 11 This sketch show a simple locking 12 mechanism using the RC522 RFID module. 13 ---------------------------------------------------------------------------- 14 15 Typical pin layout used: 16 ----------------------------------------------------------------------------------------- 17 18 MFRC522 Arduino Arduino Arduino Arduino Arduino 19 20 Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro 21 Micro 22 Signal Pin Pin Pin Pin Pin Pin 23 24 ----------------------------------------------------------------------------------------- 25 26 RST/Reset RST 9 5 D9 RESET/ICSP-5 RST 27 28 SPI SS SDA(SS) 10 53 D10 10 10 29 30 SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 31 32 SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 33 34 SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 35 36*/ 37 38#include 39 <SPI.h> 40#include <MFRC522.h> 41#define RST_PIN 9 // Configurable, 42 see typical pin layout above 43#define SS_PIN 10 // Configurable, 44 see typical pin layout above 45MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 46 instance. 47 48String read_rfid; // Add how many you need and 49 don't forget to include the UID. 50String ok_rfid_1 = "e199312d"; // This 51 is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also 52 be used to shut it down if you want to. 53String ok_rfid_2 = "fbecb673"; // 54 This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown 55 your PC normally. 56int switch1 = 7; // For the Card. 57int 58 switch2 = 7; // For the Keyfob. 59int BlueCom; // 60 Bluetooth Command. 61int Thing = 8; // Pin number where the 62 Thing is connected. 63 64void setup() { 65 66 Serial.begin(9600); // 67 Initialize serial communications with the PC 68 while (!Serial); // 69 Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) 70 71 SPI.begin(); // Init SPI bus 72 pinMode(Thing, OUTPUT); 73 // Set Thing Pin as OUTPUT. 74 pinMode(switch1, OUTPUT); // 75 RFID Card to turn ON PC. 76 pinMode(switch2, OUTPUT); // KeyFob to shut 77 it down. 78 79 80} 81/* 82 Helper routine to dump a byte array as hex values 83 to Serial. 84*/ 85void dump_byte_array(byte *buffer, byte bufferSize) { 86 read_rfid 87 = ""; 88 for (byte i = 0; i < bufferSize; i++) { 89 read_rfid = read_rfid 90 + String(buffer[i], HEX); 91 } 92} 93 94void On_Switch() { // 95 Use this routine when working with Relays and Solenoids etc. 96 digitalWrite(switch1, 97 HIGH); 98 delay(1000); 99 digitalWrite(switch1, LOW); 100} 101 102void Off_Switch() 103 { // You can also just use the card to shutdown your PC. This is 104 just for those moments that you really need to shut it down quickly. 105 digitalWrite(switch2, 106 HIGH); // Use this routine when working with Relays and Solenoids etc. 107 delay(5000); 108 109 digitalWrite(switch2, LOW); 110} 111 112void BlueOn() { 113 114 digitalWrite(Thing, 115 HIGH); 116 mfrc522.PCD_Init(); 117} 118 119void BlueOff() { 120 121 digitalWrite(Thing, 122 LOW); 123 mfrc522.PCD_Reset(); 124} 125 126void loop() { 127 128 while (Serial.available()) 129 { 130 BlueCom = Serial.read(); 131 Serial.println(BlueCom); 132 { 133 134 135 if (BlueCom == 1) 136 BlueOn(); 137 } 138 { 139 140 if (BlueCom 141 == 0) 142 BlueOff(); 143 } 144 } 145 146 // Look for new cards 147 if 148 (!mfrc522.PICC_IsNewCardPresent()) 149 return; 150 151 // Select one of the 152 cards 153 if (!mfrc522.PICC_ReadCardSerial()) 154 return; 155 dump_byte_array(mfrc522.uid.uidByte, 156 mfrc522.uid.size); 157 158 Serial.println(read_rfid); 159 if (read_rfid == ok_rfid_1) 160 { //ok, turn ON PC. 161 digitalWrite(switch1, HIGH); 162 delay(1000); 163 164 digitalWrite(switch1, LOW); 165 } 166 Serial.println(read_rfid); 167 if (read_rfid 168 == ok_rfid_2) { //ok, turn OFF PC. 169 digitalWrite(switch2, HIGH); 170 delay(5000); 171 172 digitalWrite(switch2, LOW); 173 } 174} 175
LED ON & OFF CODE
arduino
1int BLU; 2 3int led = 8;// add the pin number that you want to control. 4 5void setup() 6 7{ 8 9 Serial.begin(9600); 10 11 pinMode(led, OUTPUT); 12 13} 14 15void loop() { 16 17 while (Serial.available()) { 18 19 BLU = Serial.read(); 20 21 Serial.println(BLU); 22 { 23 if (BLU == 1) 24 25 digitalWrite(led, HIGH); 26 } 27 28 { 29 if (BLU == 0) 30 31 digitalWrite(led, LOW); 32 } 33 34 } 35 36} 37
RFID + RELAY CODE
arduino
1/* 2 * ---------------------------------------------------------------------------- 3 4 * This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid 5 6 * for further details and other examples. 7 * 8 * NOTE: The library file MFRC522.h 9 has a lot of useful info. Please read it. 10 * 11 * This sketch show a simple 12 locking mechanism using the RC522 RFID module. 13 * ---------------------------------------------------------------------------- 14 15 * Typical pin layout used: 16 * ----------------------------------------------------------------------------------------- 17 18 * MFRC522 Arduino Arduino Arduino Arduino Arduino 19 20 * Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro 21 Micro 22 * Signal Pin Pin Pin Pin Pin Pin 23 24 * ----------------------------------------------------------------------------------------- 25 26 * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST 27 28 * SPI SS SDA(SS) 10 53 D10 10 10 29 30 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 31 32 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 33 34 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 35 36 * 37 */ 38#include <SPI.h> 39#include <MFRC522.h> 40 41#define RST_PIN 9 42 // Configurable, see typical pin layout above 43#define SS_PIN 10 44 // Configurable, see typical pin layout above 45 46MFRC522 mfrc522(SS_PIN, 47 RST_PIN); // Create MFRC522 instance. 48 49String read_rfid; // 50 Add how many you need and don't forget to include the UID. 51String ok_rfid_1="e199312d"; 52 // This is for my main RFID Card. aka. The one I will be using to turn on 53 my PC. Can also be used to shut it down if you want to. 54String ok_rfid_2="fbecb673"; 55 // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. 56 Just shutdown your PC normally. 57int lock = 7; // For the 58 Card. 59int lock2 = 7; // For the Keyfob. 60/* 61 * Initialize. 62 63 */ 64void setup() { 65 Serial.begin(9600); // Initialize serial communications 66 with the PC 67 while (!Serial); // Do nothing if no serial port is 68 opened (added for Arduinos based on ATMEGA32U4) 69 SPI.begin(); // 70 Init SPI bus 71 mfrc522.PCD_Init(); // Init MFRC522 card 72 73 //Choose 74 which lock below: 75 pinMode(lock, OUTPUT); 76 pinMode(lock2, OUTPUT); 77} 78/* 79 80 * Helper routine to dump a byte array as hex values to Serial. 81 */ 82void dump_byte_array(byte 83 *buffer, byte bufferSize) { 84 read_rfid=""; 85 for (byte i = 0; i < bufferSize; 86 i++) { 87 read_rfid=read_rfid + String(buffer[i], HEX); 88 } 89} 90 91void 92 open_lock() { 93 //Use this routine when working with Relays and Solenoids etc. 94 95 digitalWrite(lock,HIGH); 96 delay(500); 97 digitalWrite(lock,LOW); 98} 99 100 101void 102 close_lock2() { // You can also just use the card to shutdown your PC. 103 This is just for those moments that you really need to shut it down quickly. 104 105 //Use this routine when working with Relays and Solenoids etc. 106 digitalWrite(lock2, 107 HIGH); 108 delay(5000); 109 digitalWrite(lock2,LOW); 110} 111 112 113void 114 loop() { 115 116 // Look for new cards 117 if ( ! mfrc522.PICC_IsNewCardPresent()) 118 119 return; 120 121 // Select one of the cards 122 if ( ! mfrc522.PICC_ReadCardSerial()) 123 124 return; 125 126 dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); 127 128 Serial.println(read_rfid); 129 if (read_rfid==ok_rfid_1) { 130 //ok, 131 open the door. 132 open_lock(); 133 } 134 135 Serial.println(read_rfid); 136 137 if (read_rfid==ok_rfid_2) { 138 //ok, open the door. 139 close_lock2(); 140 141 } 142 //Add below as many "keys" as you want 143 //if (read_rfid==ok_rfid_2) 144 { 145 //also ok, open the door 146 // open_lock(); 147 //} 148 // 149 else not needed. Anything else is not ok, and will not open the door... 150} 151
Downloadable files
LED ON & OFF SCHEMATIC
LED ON & OFF SCHEMATIC
AT COMMANDS [PASSWORD CHANGE] SCHEMATIC
AT COMMANDS [PASSWORD CHANGE] SCHEMATIC
MFRC522 RFID Library by Mr. Miguel Balboa
https://github.com/miguelbalboa/rfid
RFID + HC-05 + RELAY SCHEMATIC
RFID + HC-05 + RELAY SCHEMATIC
MFRC522 RFID Library by Mr. Miguel Balboa
https://github.com/miguelbalboa/rfid
LED ON & OFF SCHEMATIC
LED ON & OFF SCHEMATIC
AT COMMANDS [PASSWORD CHANGE] SCHEMATIC
AT COMMANDS [PASSWORD CHANGE] SCHEMATIC
Comments
Only logged in users can leave comments