Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
Werzaire
Published © GPL3+

RFID NeoPixel access project

Card access with pixel feedback, red for wrong, green for right.

BeginnerFull instructions provided1 hour5,614
RFID NeoPixel access project

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
This is the base chipset I used to make this project.
×1
NeoPixel Ring: WS2812 5050 RGB LED
Adafruit NeoPixel Ring: WS2812 5050 RGB LED
Ring with 24 pixels, see: https://www.adafruit.com/product/1586
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
Connected them between breadboard and components. See Schematics further down.
×12
USB-A to B Cable
USB-A to B Cable
To power and upload code to Arduino UNO
×1
RFID reader (generic)
In my project, I used the RFID-RC522. I bought it through a Norwegian company at this URL: https://www.kjell.com/no/produkter/elektro-og-verktoy/arduino/moduler/rfid-leser-for-arduino-p87046
×1
KW9Z-T2X0 (RFID cards)
×1

Software apps and online services

Arduino IDE
Arduino IDE
I coded and uploaded the code with the Arduino IDE software from Windows Store.

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
I had to solder the RFID-RC522 to make sure it had 100 % connection.

Story

Read more

Schematics

Image Schematics

A plain PNG picture of the connections

Fritzing Schematics

Open in Fritzing (http://fritzing.org/download/) to see schematics in several views.

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.
#include <Adafruit_NeoPixel.h>
#include <MFRC522.h>
/**
 * Remember to import the following libraries from the 
 * Library Manager:
 * - Adafruit NeoPixel v. 1.10.5 by Adafruit (https://github.com/adafruit/Adafruit_NeoPixel)
 * - MFRC522 v. 1.4.10 by GithubCommunity (https://github.com/miguelbalboa/rfid)
 */

// Setup of configurations
const int      delayval  =  50; // Time each pixel pauses to do it's duty.
const int      PIN       =  6; // Data output to Adafruit NeoPixel
const int      NUMPIXELS =  24; // Number of pixels in the ring ^
const int      SS_PIN    =  10; // Data PIN for MFRC522
const int      RST_PIN   =  9; // Reset PIN for MFRC522
const int      TIME      =  150; // Time between each NeoPixel blink.

// Defining needed variables for the NeoPixel: pixels
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

/**
 * Setting up color definitions for easier use later.
 * They're defined as RGB (Red, Green, Blue) and values
 * are between 0 and 255. 
 * 0 being no intensity.
 * 255 for full intensity.
 */
const uint32_t red       =  pixels.Color(255, 0, 0);
const uint32_t green     =  pixels.Color(0, 255, 0);
const uint32_t yellow    =  pixels.Color(255, 255, 0);
const uint32_t blank     =  pixels.Color(0, 0, 0);

/**
 * Prepares the RFID with the assigned Arduino ports
 */
MFRC522 rfid(SS_PIN, RST_PIN); // Prepares PINS

/**
 * Function setup:
 * Description: Main function for Arduino programming language. It only
 *              runs once during startup of Arduino.
 *              Here it initates the console output for our reading
 *              convenience. It also initialises the Neopixel ring
 *              and sets the intensity to 20 out off
 */
void setup() {
  Serial.begin(9600);
  Serial.println("Starting all...\n\n");
  pixels.begin();
  pixels.setBrightness(20); // This is to make sure the lights won't burn up.
  SPI.begin(); // Make the NeoPixel and microcontroller communicate
  rfid.PCD_Init(); 
}

/**
 * Function loop:
 * Desc: This part is the one that repeats over and over again.
 * It involves the process behind the whole script.
 */
void loop() {
  /**
   * Sets all NeoPixels to yellow at the start of the loop.
   */
  setAll(yellow);
  
  /*
  * For as long as no card is readable by the scanner, it stops further
  * code execution.
  */
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()){
    return;
  }
  /**
   * It reads the RFID card type here, if it does not match any
   * known types, it stops code execution and returns to loop.
   */
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
      piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
      piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
  /**
   * Makes output from the RFID reader readable by the code.
   */
  String strID = "";
  for (byte i = 0; i < 4; i++) {
    strID +=
      (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
      String(rfid.uid.uidByte[i], HEX) +
      (i != 3 ? ":" : "");
  }
  strID.toUpperCase();
  
  /**
   * Starts validating if the card is accepted or not.
   * For more, see the function and card ID's on line 152
   */
  if (validateCard(strID)) {
    Serial.println("*** Access granted! ***");
    Serial.print("*** The source was just accessed by a card with this ID: ");
    Serial.println(strID);
    Serial.print("");
    /**
     * Gives visual feedback through the Adafruit NeoPixel (24)
     * by blinking between green and off 10 times.
     */
    for(int i=0;i<10;i++){ 
      setAll(green);
      delay(TIME);
      setAll(blank);
      delay(TIME);
    }
    return;
  } else {
    Serial.println("*** Access denied! ***");
    Serial.print("*** You tried a card with this id: ");
    Serial.println(strID);
    Serial.println("");
    /**
     * Gives visual feedback through the Adafruit NeoPixel (24)
     * by blinking between red and off 4 times.
     */
    for(int i=0;i<4;i++){ 
      setAll(red);
      delay(TIME);
      setAll(blank);
      delay(TIME);
    }
    return;
  }
}

/**
 * Function setAll:
 * Description: Sets all pixels as defined in the
 * configuration
 */
void setAll(uint32_t color) {
  for(int i=0;i<NUMPIXELS;i++) {
    pixels.setPixelColor(i, color);
    pixels.show();
  }
}

/**
 * Function validateCard:
 * Description: Checks ID of card and returns either
 * true or false depending on the card ID.
 */
bool validateCard(String usercard) {
  // Insert your own card ID's here, insert more with else if under
  if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* Insert your own card ID here */
    return true;
  } else if(usercard.indexOf("AA:BB:CC:DD") >= 0) { /* If you have more cards, add another ID here */
    return true;
  } else return false; /* Couldn't match any of the above, no valid cards */
}

Credits

Werzaire

Werzaire

0 projects • 2 followers

Comments