Components and supplies
NeoPixel Ring: WS2812 5050 RGB LED
Breadboard (generic)
USB-A to B Cable
Arduino UNO
Jumper wires (generic)
RFID reader (generic)
KW9Z-T2X0 (RFID cards)
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Arduino Uno Code
arduino
Here's the code, with an edit since last time. Removed unnecessary code and commented it further to make more sense to less experienced people.
1#include <Adafruit_NeoPixel.h> 2#include <MFRC522.h> 3/** 4 * Remember to import the following libraries from the 5 * Library Manager: 6 * - Adafruit NeoPixel v. 1.10.5 by Adafruit (https://github.com/adafruit/Adafruit_NeoPixel) 7 * - MFRC522 v. 1.4.10 by GithubCommunity (https://github.com/miguelbalboa/rfid) 8 */ 9 10// Setup of configurations 11const int delayval = 50; // Time each pixel pauses to do it's duty. 12const int PIN = 6; // Data output to Adafruit NeoPixel 13const int NUMPIXELS = 24; // Number of pixels in the ring ^ 14const int SS_PIN = 10; // Data PIN for MFRC522 15const int RST_PIN = 9; // Reset PIN for MFRC522 16const int TIME = 150; // Time between each NeoPixel blink. 17 18// Defining needed variables for the NeoPixel: pixels 19Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); 20 21/** 22 * Setting up color definitions for easier use later. 23 * They're defined as RGB (Red, Green, Blue) and values 24 * are between 0 and 255. 25 * 0 being no intensity. 26 * 255 for full intensity. 27 */ 28const uint32_t red = pixels.Color(255, 0, 0); 29const uint32_t green = pixels.Color(0, 255, 0); 30const uint32_t yellow = pixels.Color(255, 255, 0); 31const uint32_t blank = pixels.Color(0, 0, 0); 32 33/** 34 * Prepares the RFID with the assigned Arduino ports 35 */ 36MFRC522 rfid(SS_PIN, RST_PIN); // Prepares PINS 37 38/** 39 * Function setup: 40 * Description: Main function for Arduino programming language. It only 41 * runs once during startup of Arduino. 42 * Here it initates the console output for our reading 43 * convenience. It also initialises the Neopixel ring 44 * and sets the intensity to 20 out off 45 */ 46void setup() { 47 Serial.begin(9600); 48 Serial.println("Starting all...\ 49\ 50"); 51 pixels.begin(); 52 pixels.setBrightness(20); // This is to make sure the lights won't burn up. 53 SPI.begin(); // Make the NeoPixel and microcontroller communicate 54 rfid.PCD_Init(); 55} 56 57/** 58 * Function loop: 59 * Desc: This part is the one that repeats over and over again. 60 * It involves the process behind the whole script. 61 */ 62void loop() { 63 /** 64 * Sets all NeoPixels to yellow at the start of the loop. 65 */ 66 setAll(yellow); 67 68 /* 69 * For as long as no card is readable by the scanner, it stops further 70 * code execution. 71 */ 72 if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()){ 73 return; 74 } 75 /** 76 * It reads the RFID card type here, if it does not match any 77 * known types, it stops code execution and returns to loop. 78 */ 79 MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); 80 if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 81 piccType != MFRC522::PICC_TYPE_MIFARE_1K && 82 piccType != MFRC522::PICC_TYPE_MIFARE_4K) { 83 Serial.println(F("Your tag is not of type MIFARE Classic.")); 84 return; 85 } 86 /** 87 * Makes output from the RFID reader readable by the code. 88 */ 89 String strID = ""; 90 for (byte i = 0; i < 4; i++) { 91 strID += 92 (rfid.uid.uidByte[i] < 0x10 ? "0" : "") + 93 String(rfid.uid.uidByte[i], HEX) + 94 (i != 3 ? ":" : ""); 95 } 96 strID.toUpperCase(); 97 98 /** 99 * Starts validating if the card is accepted or not. 100 * For more, see the function and card ID's on line 152 101 */ 102 if (validateCard(strID)) { 103 Serial.println("*** Access granted! ***"); 104 Serial.print("*** The source was just accessed by a card with this ID: "); 105 Serial.println(strID); 106 Serial.print(""); 107 /** 108 * Gives visual feedback through the Adafruit NeoPixel (24) 109 * by blinking between green and off 10 times. 110 */ 111 for(int i=0;i<10;i++){ 112 setAll(green); 113 delay(TIME); 114 setAll(blank); 115 delay(TIME); 116 } 117 return; 118 } else { 119 Serial.println("*** Access denied! ***"); 120 Serial.print("*** You tried a card with this id: "); 121 Serial.println(strID); 122 Serial.println(""); 123 /** 124 * Gives visual feedback through the Adafruit NeoPixel (24) 125 * by blinking between red and off 4 times. 126 */ 127 for(int i=0;i<4;i++){ 128 setAll(red); 129 delay(TIME); 130 setAll(blank); 131 delay(TIME); 132 } 133 return; 134 } 135} 136 137/** 138 * Function setAll: 139 * Description: Sets all pixels as defined in the 140 * configuration 141 */ 142void setAll(uint32_t color) { 143 for(int i=0;i<NUMPIXELS;i++) { 144 pixels.setPixelColor(i, color); 145 pixels.show(); 146 } 147} 148 149/** 150 * Function validateCard: 151 * Description: Checks ID of card and returns either 152 * true or false depending on the card ID. 153 */ 154bool validateCard(String usercard) { 155 // Insert your own card ID's here, insert more with else if under 156 if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* Insert your own card ID here */ 157 return true; 158 } else if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* If you have more cards, add another ID here */ 159 return true; 160 } else return false; /* Couldn't match any of the above, no valid cards */ 161}
Arduino Uno Code
arduino
Here's the code, with an edit since last time. Removed unnecessary code and commented it further to make more sense to less experienced people.
1#include <Adafruit_NeoPixel.h> 2#include <MFRC522.h> 3/** 4 * Remember to import the following libraries from the 5 * Library Manager: 6 * - Adafruit NeoPixel v. 1.10.5 by Adafruit (https://github.com/adafruit/Adafruit_NeoPixel) 7 * - MFRC522 v. 1.4.10 by GithubCommunity (https://github.com/miguelbalboa/rfid) 8 */ 9 10// Setup of configurations 11const int delayval = 50; // Time each pixel pauses to do it's duty. 12const int PIN = 6; // Data output to Adafruit NeoPixel 13const int NUMPIXELS = 24; // Number of pixels in the ring ^ 14const int SS_PIN = 10; // Data PIN for MFRC522 15const int RST_PIN = 9; // Reset PIN for MFRC522 16const int TIME = 150; // Time between each NeoPixel blink. 17 18// Defining needed variables for the NeoPixel: pixels 19Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); 20 21/** 22 * Setting up color definitions for easier use later. 23 * They're defined as RGB (Red, Green, Blue) and values 24 * are between 0 and 255. 25 * 0 being no intensity. 26 * 255 for full intensity. 27 */ 28const uint32_t red = pixels.Color(255, 0, 0); 29const uint32_t green = pixels.Color(0, 255, 0); 30const uint32_t yellow = pixels.Color(255, 255, 0); 31const uint32_t blank = pixels.Color(0, 0, 0); 32 33/** 34 * Prepares the RFID with the assigned Arduino ports 35 */ 36MFRC522 rfid(SS_PIN, RST_PIN); // Prepares PINS 37 38/** 39 * Function setup: 40 * Description: Main function for Arduino programming language. It only 41 * runs once during startup of Arduino. 42 * Here it initates the console output for our reading 43 * convenience. It also initialises the Neopixel ring 44 * and sets the intensity to 20 out off 45 */ 46void setup() { 47 Serial.begin(9600); 48 Serial.println("Starting all...\ 49\ 50"); 51 pixels.begin(); 52 pixels.setBrightness(20); // This is to make sure the lights won't burn up. 53 SPI.begin(); // Make the NeoPixel and microcontroller communicate 54 rfid.PCD_Init(); 55} 56 57/** 58 * Function loop: 59 * Desc: This part is the one that repeats over and over again. 60 * It involves the process behind the whole script. 61 */ 62void loop() { 63 /** 64 * Sets all NeoPixels to yellow at the start of the loop. 65 */ 66 setAll(yellow); 67 68 /* 69 * For as long as no card is readable by the scanner, it stops further 70 * code execution. 71 */ 72 if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()){ 73 return; 74 } 75 /** 76 * It reads the RFID card type here, if it does not match any 77 * known types, it stops code execution and returns to loop. 78 */ 79 MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); 80 if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && 81 piccType != MFRC522::PICC_TYPE_MIFARE_1K && 82 piccType != MFRC522::PICC_TYPE_MIFARE_4K) { 83 Serial.println(F("Your tag is not of type MIFARE Classic.")); 84 return; 85 } 86 /** 87 * Makes output from the RFID reader readable by the code. 88 */ 89 String strID = ""; 90 for (byte i = 0; i < 4; i++) { 91 strID += 92 (rfid.uid.uidByte[i] < 0x10 ? "0" : "") + 93 String(rfid.uid.uidByte[i], HEX) + 94 (i != 3 ? ":" : ""); 95 } 96 strID.toUpperCase(); 97 98 /** 99 * Starts validating if the card is accepted or not. 100 * For more, see the function and card ID's on line 152 101 */ 102 if (validateCard(strID)) { 103 Serial.println("*** Access granted! ***"); 104 Serial.print("*** The source was just accessed by a card with this ID: "); 105 Serial.println(strID); 106 Serial.print(""); 107 /** 108 * Gives visual feedback through the Adafruit NeoPixel (24) 109 * by blinking between green and off 10 times. 110 */ 111 for(int i=0;i<10;i++){ 112 setAll(green); 113 delay(TIME); 114 setAll(blank); 115 delay(TIME); 116 } 117 return; 118 } else { 119 Serial.println("*** Access denied! ***"); 120 Serial.print("*** You tried a card with this id: "); 121 Serial.println(strID); 122 Serial.println(""); 123 /** 124 * Gives visual feedback through the Adafruit NeoPixel (24) 125 * by blinking between red and off 4 times. 126 */ 127 for(int i=0;i<4;i++){ 128 setAll(red); 129 delay(TIME); 130 setAll(blank); 131 delay(TIME); 132 } 133 return; 134 } 135} 136 137/** 138 * Function setAll: 139 * Description: Sets all pixels as defined in the 140 * configuration 141 */ 142void setAll(uint32_t color) { 143 for(int i=0;i<NUMPIXELS;i++) { 144 pixels.setPixelColor(i, color); 145 pixels.show(); 146 } 147} 148 149/** 150 * Function validateCard: 151 * Description: Checks ID of card and returns either 152 * true or false depending on the card ID. 153 */ 154bool validateCard(String usercard) { 155 // Insert your own card ID's here, insert more with else if under 156 if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* Insert your own card ID here */ 157 return true; 158 } else if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* If you have more cards, add another ID here */ 159 return true; 160 } else return false; /* Couldn't match any of the above, no valid cards */ 161}
Downloadable files
Fritzing Schematics
Open in Fritzing (http://fritzing.org/download/) to see schematics in several views.
Fritzing Schematics
Image Schematics
A plain PNG picture of the connections
Image Schematics
Fritzing Schematics
Open in Fritzing (http://fritzing.org/download/) to see schematics in several views.
Fritzing Schematics
Comments
Only logged in users can leave comments