Components and supplies
Arduino UNO
RFID Module (Generic)
Relay Module (Generic)
Male/Female Jumper Wires
Project description
Code
RFID-Relay
arduino
This is really an RFID Door Lock code. I just used it to power my PC on.
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-Relay
arduino
This is really an RFID Door Lock code. I just used it to power my PC on.
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
MFRC522 Library
Downloadable files
Visual Wiring Diagram
I made this using Photoshop. The wires have various colors to avoid confusion.
Visual Wiring Diagram
Visual Wiring Diagram
I made this using Photoshop. The wires have various colors to avoid confusion.
Visual Wiring Diagram
Comments
Only logged in users can leave comments
Heathen_Hacks-v2
0 Followers
•0 Projects
Table of contents
Intro
30
0