My Cart

How to Identify People Using Smart Cards Kit (RFID 125KHz)

Difficulty Level: Expert -

1. Introduction

This tutorial allows the user to indentify different cards associated to several people. If they are close to an antenna, RFID card will save how many times that person has been there.

Ingredients:

    - 1 x Smart Cards Kit (RFID 125KHz):
    • 1x Platform
    • 1x RFID 125KHz
    • 1x GPRS antenna
    • 1x RGB LED
    • 3x 220Ω resistors
    • 1x Buzzer
    • 1x LCD
    • 1x Potentiometer
    • 1x Breadboard
    • 1x External power supply
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the RFID 125kHz Shield, consult the main tutorial.

Step 1: Connection

Connect the RFID module with its antenna to the Xbee Shield and then, connect the shield to the Arduino. Connect the RGB LED with the resistors, the buzzer, the potentiometer and the LCD in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the 5 volt supply and ground. A potentiometer will adjust LCD contrast. Connect left terminal of the potentiometer to 5 volt and the right terminal to GND. Connect the central leg of the potentiometer to the third pin of the LCD display. Follow the wire color diagram to connect all LCD pins. Connect pin 6,10 and 11 to the RGB LED with a resistor in each pin and power it with 5 volt in the longest leg of the RGB LED. Finally, connect positive terminal of the buzzer, marked with a "+", to a resistor and then to pin 9 and the remaining terminal of the buzzer to GND.

WARNING: In Raspberry Pi, GPIO voltage levels are 3.3 V and are not 5 V tolerant. There is no over-voltage protection on the board. Digital inputs use a 3V3 logic level and are not tolerant of 5V levels, such as you might find on a 5V powered Arduino. Extreme caution when working with GPIO, you may damage your Raspberry Pi, your equipment and potentially yourself and others. Doing so is at your own risk!

Step 2: The Code

Arduino:

Code:
/*  
 *  RFID 125kHz Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel MartĂ­
 */
 			
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 8, 5, 4, 3, 2);


//define constants
#define RED 6
#define GREEN 10
#define BLUE 11
#define BUZZ 9

struct RGB {

  uint8_t r;
  uint8_t g;
  uint8_t b;

};


//colours

RGB orange = {0, 225, 255};
RGB lime = {64, 0, 255};
RGB cyan = {255, 79, 9};
RGB off = {255, 255, 255};
RGB red = {0, 255, 255};
RGB white = {0, 0, 0};
RGB purple = {100, 255, 100};


//Integer to hold commands received from serial monitor
int received = 0;
byte userIdentifier[3][4];
byte auxID[4];
byte card = -1;
char userName[3][16] = {{'L', 'e', 'w', 'i', 's', ' ', 'M', 'a', 'r', 'v', 'i', 'n', ' ', ' ', ' ', ' '},
  {'M', 'a', 'r', 'k', ' ', 'B', 'e', 'a', 'n', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {'G', 'e', 'o', 'r', 'g', 'e', ' ', 'S', 'l', 'a', 'u', 'g', 'h', 't', 'e', 'r'}
};
byte timeshere[3];
byte acces_level[3];


void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUZZ, OUTPUT);
  
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  
  analogWrite(BUZZ, 128);
  delay(100);
  analogWrite(BUZZ, 0);

  lcd.begin(16, 2);
  lcd.print("SM125 125khz");
  lcd.setCursor(0, 2);
  lcd.print("RFID Start");
  delay(2000);
  Serial.begin(19200);
  setParam();
  delay(200);
  //flush serial manually
  while (Serial.available() > 0) {
    Serial.read();
  }
  //***************************************************
  //***************************************************
  //before using this example remeber to write userIdentifier within cards
  //intialize users
  //user 1
  userIdentifier[0][0] = 0x15;
  userIdentifier[0][1] = 0x75;
  userIdentifier[0][2] = 0x57;
  userIdentifier[0][3] = 0x51;
  //user 2
  userIdentifier[1][0] = 0x22;
  userIdentifier[1][1] = 0x22;
  userIdentifier[1][2] = 0x22;
  userIdentifier[1][3] = 0x22;
  //user 3
  userIdentifier[2][0] = 0x87;
  userIdentifier[2][1] = 0x65;
  userIdentifier[2][2] = 0x43;
  userIdentifier[2][3] = 0x21;

  for (byte i = 0; i < 3; i++) {
    timeshere[i] = 0;
  }
  acces_level[0] = 1;
  acces_level[1] = 2;
  acces_level[2] = 3;
  setAutoMode();//keeps shield reading to detect whenever a card waves close to the antenna
  lcd.clear();
  lcd.print("Wave your card");
}

void loop() {

  while (Serial.available()) {
    card = getUID();
    if (card != 255) {
      lcd.clear();
      lcd.print("Correct card...");
      delay(500);
      lcd.clear();
      lcd.print("User:");
      lcd.setCursor(0, 2);
      for (byte i = 0; i < 16; i++) {
        lcd.print(userName[card][i]);
      }
      delay(2000);
      timeshere[card]++;
      lcd.clear();
      lcd.print("You've been here");
      lcd.setCursor(0, 2);
      lcd.print(timeshere[card]);
      lcd.setCursor(4, 2);
      lcd.print("times today");
      delay(2000);


      lcd.clear();
      lcd.print("Acces level:");
      lcd.setCursor(0, 2);
      lcd.print(acces_level[card], DEC);
      delay(1500);
      switch (acces_level[card]) {

        case 1:
          lcd.clear();
          lcd.print("Acces denied!");
          setColour(red.r,red.g,red.b);
          delay(1000);
          analogWrite(BUZZ, 78);
          delay(1000);
          analogWrite(BUZZ, 0);
          break;

        case 2:

          lcd.clear();
          lcd.print("Acces granted!");
          lcd.setCursor(0, 2);
          lcd.print("Go ahead!");
          setColour(cyan.r, cyan.g, cyan.b);
          delay(2000);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          delay(100);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          break;

        case 3:

          lcd.clear();
          lcd.print("Total acces!");
          lcd.setCursor(0, 2);
          lcd.print("Go ahead!");
          setColour(purple.r, purple.g, purple.b);
          delay(2000);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          delay(100);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          break;
      }
    }
    lcd.clear();
    lcd.print("Wave your card");
  }
}

// ***This cards do not have UID, we will save different data in their block2
// so we can identify them***
byte getUID() {

  if (Serial.read() == 0x01) { //fixed header value
    lcd.clear();
    delay(10);
    byte len = Serial.read();//message lenght
    delay(10);
    for (byte j = 0; j < 5; j++) {
      Serial.read();//first 2 blocks will be our "UIDCard"
      delay(10);
    }
    /* for (byte i = 0; i < 4; i++) {
       Serial.read();//ignore config block

     }*/
    for (byte j = 0; j < 4; j++) {
      auxID [j] = Serial.read();//first 2 blocks will be our "UIDCard"
    }
    //discard other data
    delay(500);
    while (Serial.available() > 0) {
      Serial.read();
    }
    byte user = -1;
    //compare block 2 with each "UID" above
    for (byte i = 0; i < 3; i++) {
      for (byte j = 0; j < 4; j++) {

        if (auxID[j] == userIdentifier[i][j]) {
          user = i;
        } else {
          j = 4;
        }

      }

    }
    return user;

  }
  return -1;
}


//**********************************************************************
//turns on led with a selected colour
void setColour(uint8_t r, uint8_t g, uint8_t b) {

  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);

}



byte setParam () {
  //Calculate checksum of all bytes except header
  byte msglength = 0x06;
  byte checksum = 0x01 + msglength + 0x40 + 0x32 + 0x64 + 0x5A + 0x1E + 0x60;
  
  //Build and execute command
  byte paramcommand[10] = {0xFF, 0x01, msglength, 0x40, 0x32, 0x64, 0x5A, 0x1E, 0x60, checksum};
        
        // send the request bytes
        for (byte i=0; i<10; i++) {
          Serial.write(paramcommand[i]);
        }
  //flush buffer *critical*
  while (Serial.available() > 0) {
    Serial.read();
  }
   //give some time to module to do its stuff     
        delay(500);
        
	//verify successful completion of command
  if (Serial.read() == 0xFF) {
    if (Serial.read() == 0x01) {
      if (Serial.read() == 0x01) {
        if (Serial.read() == 0x99) {
          if (Serial.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}

 byte setAutoMode () {
	//Calculate checksum of all bytes except header
	byte msglength = 0x09;
	byte checksum = 0x01 + msglength + 0x87 + 0x01 + 0x01 + 0x03;

        //Build and execute command
	byte cmdToExecute[13] = {0xFF, 0x01, msglength, 0x87, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, checksum};
        
        // send the request bytes
        for (int m=0; m<13; m++) {
			Serial.write(cmdToExecute[m]);
        }
        //flush buffer *critical*
  while (Serial.available() > 0) {
    Serial.read();
  }
        delay(500);
        
	//verify successful completion of command
	  if (Serial.read() == 0xFF) {
    if (Serial.read() == 0x01) {
      if (Serial.read() == 0x01) {
        if (Serial.read() == 0x99) {
          if (Serial.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}
        

Raspberry Pi:

Code:
/*  
 *  RFID 125kHz Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria
 */
 
#include "arduPi.h"


//define constants
#define RED 6
#define GREEN 10
#define BLUE 11
#define BUZZ 9


//Integer to hold commands received from serial monitor
int received = 0;
byte userIdentifier[3][4];
byte auxID[4];
byte card = -1;
char userName[3][16] = {{'L', 'e', 'w', 'i', 's', ' ', 'M', 'a', 'r', 'v', 'i', 'n', ' ', ' ', ' ', ' '},
  {'M', 'a', 'r', 'k', ' ', 'B', 'e', 'a', 'n', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {'G', 'e', 'o', 'r', 'g', 'e', ' ', 'S', 'l', 'a', 'u', 'g', 'h', 't', 'e', 'r'}
};
byte timeshere[3];
byte acces_level[3];


byte getUID();
byte setParam ();
byte setAutoMode ();


void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUZZ, OUTPUT);
  
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);

  digitalWrite(BUZZ, HIGH);
  delay(100);
  digitalWrite(BUZZ, 0);
  delay(100);
  digitalWrite(BUZZ, HIGH);
  delay(100);
  digitalWrite(BUZZ, 0);

  printf("SM125 125khz RFID Start\n");
  delay(2000);
  Serial.begin(19200);
  setParam();
  delay(200);
  //flush serial manually
  while (Serial.available() > 0) {
    Serial.read();
  }
  //***************************************************
  //***************************************************
  //before using this example remeber to write userIdentifier within cards
  //intialize users
  //user 1
  userIdentifier[0][0] = 0x15;
  userIdentifier[0][1] = 0x75;
  userIdentifier[0][2] = 0x57;
  userIdentifier[0][3] = 0x51;
  //user 2
  userIdentifier[1][0] = 0x22;
  userIdentifier[1][1] = 0x22;
  userIdentifier[1][2] = 0x22;
  userIdentifier[1][3] = 0x22;
  //user 3
  userIdentifier[2][0] = 0x87;
  userIdentifier[2][1] = 0x65;
  userIdentifier[2][2] = 0x43;
  userIdentifier[2][3] = 0x21;

  for (byte i = 0; i < 3; i++) {
    timeshere[i] = 0;
  }
  acces_level[0] = 1;
  acces_level[1] = 2;
  acces_level[2] = 3;
  setAutoMode();//keeps shield reading to detect whenever a card waves close to the antenna
  
  printf("Wave your card\n");
}

void loop() {

  while (Serial.available()) {
    card = getUID();
    if (card != 255) {
      
      printf("Correct card...\n");
      delay(500);
      
      printf("User: ");
	  for (byte i = 0; i < 16; i++) {
        printf("%c", userName[card][i]);
      }
      printf("\n");
      delay(2000);
      timeshere[card]++;
      
      printf("You've been here %d times today\n", timeshere[card]);
      delay(2000);


      
      printf("Access level: %d\n", acces_level[card]);
      delay(1500);
      switch (acces_level[card]) {

        case 1:
          
          printf("Access denied!\n");

          //Red LED
          digitalWrite(RED, 0);
          digitalWrite(GREEN, 1);
          digitalWrite(BLUE, 1);
  
          digitalWrite(BUZZ, HIGH);
          delay(1000);
          digitalWrite(BUZZ, 0);
          
          //Wait and turn off LED
          delay(2000);
          digitalWrite(RED, HIGH);
          digitalWrite(GREEN, HIGH);
          digitalWrite(BLUE, HIGH);
          break;

        case 2:

          
          printf("Access granted! Go ahead!\n");
          
          //Blue LED
          digitalWrite(RED, 1);
          digitalWrite(GREEN, 1);
          digitalWrite(BLUE, 0);
          
          digitalWrite(BUZZ, HIGH);
          delay(100);
          digitalWrite(BUZZ, 0);
          delay(100);
          digitalWrite(BUZZ, HIGH);
          delay(100);
          digitalWrite(BUZZ, 0);
          
          //Wait and turn off LED
          delay(2000);
          digitalWrite(RED, HIGH);
          digitalWrite(GREEN, HIGH);
          digitalWrite(BLUE, HIGH);
          break;

        case 3:

          
          printf("Total access! Go ahead!\n");
          
          //Green LED
          digitalWrite(RED, 1);
          digitalWrite(GREEN, 0);
          digitalWrite(BLUE, 1);
          
          
          digitalWrite(BUZZ, HIGH);
          delay(100);
          digitalWrite(BUZZ, 0);
          delay(100);
          digitalWrite(BUZZ, HIGH);
          delay(100);
          digitalWrite(BUZZ, 0);
          
          //Wait and turn off LED
          delay(2000);
          digitalWrite(RED, HIGH);
          digitalWrite(GREEN, HIGH);
          digitalWrite(BLUE, HIGH);
          break;
      }
    }
    
    printf("Wave your card\n");
  }
}

// ***This cards do not have UID, we will save different data in their block2
// so we can identify them***
byte getUID() {

  if (Serial.read() == 0x01) { //fixed header value
    
    delay(10);
    byte len = Serial.read();//message lenght
    delay(10);
    for (byte j = 0; j < 5; j++) {
      Serial.read();//first 2 blocks will be our "UIDCard"
      delay(10);
    }
    /* for (byte i = 0; i < 4; i++) {
       Serial.read();//ignore config block

     }*/
    for (byte j = 0; j < 4; j++) {
      auxID [j] = Serial.read();//first 2 blocks will be our "UIDCard"
    }
    //discard other data
    delay(500);
    while (Serial.available() > 0) {
      Serial.read();
    }
    byte user = -1;
    //compare block 2 with each "UID" above
    for (byte i = 0; i < 3; i++) {
      for (byte j = 0; j < 4; j++) {

        if (auxID[j] == userIdentifier[i][j]) {
          user = i;
        } else {
          j = 4;
        }

      }

    }
    return user;

  }
  return -1;
}


//**********************************************************************


byte setParam () {
  //Calculate checksum of all bytes except header
  byte msglength = 0x06;
  byte checksum = 0x01 + msglength + 0x40 + 0x32 + 0x64 + 0x5A + 0x1E + 0x60;
  
  //Build and execute command
  byte paramcommand[10] = {0xFF, 0x01, msglength, 0x40, 0x32, 0x64, 0x5A, 0x1E, 0x60, checksum};
        
        // send the request bytes
        for (byte i=0; i<10; i++) {
          Serial.write(paramcommand[i]);
        }
  //flush buffer *critical*
  while (Serial.available() > 0) {
    Serial.read();
  }
   //give some time to module to do its stuff     
        delay(500);
        
	//verify successful completion of command
  if (Serial.read() == 0xFF) {
    if (Serial.read() == 0x01) {
      if (Serial.read() == 0x01) {
        if (Serial.read() == 0x99) {
          if (Serial.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}

byte setAutoMode () {
	//Calculate checksum of all bytes except header
	byte msglength = 0x09;
	byte checksum = 0x01 + msglength + 0x87 + 0x01 + 0x01 + 0x03;

        //Build and execute command
	byte cmdToExecute[13] = {0xFF, 0x01, msglength, 0x87, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, checksum};
        
        // send the request bytes
        for (int m=0; m<13; m++) {
			Serial.write(cmdToExecute[m]);
        }
        //flush buffer *critical*
  while (Serial.available() > 0) {
    Serial.read();
  }
        delay(500);
        
	//verify successful completion of command
   if (Serial.read() == 0xFF) {
    if (Serial.read() == 0x01) {
      if (Serial.read() == 0x01) {
        if (Serial.read() == 0x99) {
          if (Serial.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}




int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
  
/*  
 *  RFID 125kHz Module
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 8, 5, 4, 3, 2);


//define constants
#define RED 6
#define GREEN 10
#define BLUE 11
#define BUZZ 9

struct RGB {

  uint8_t r;
  uint8_t g;
  uint8_t b;

};


//colours

RGB orange = {0, 225, 255};
RGB lime = {64, 0, 255};
RGB cyan = {255, 79, 9};
RGB off = {255, 255, 255};
RGB red = {0, 255, 255};
RGB white = {0, 0, 0};
RGB purple = {100, 255, 100};


//Integer to hold commands received from serial monitor
int received = 0;
byte userIdentifier[3][4];
byte auxID[4];
byte card = -1;
char userName[3][16] = {{'L', 'e', 'w', 'i', 's', ' ', 'M', 'a', 'r', 'v', 'i', 'n', ' ', ' ', ' ', ' '},
  {'M', 'a', 'r', 'k', ' ', 'B', 'e', 'a', 'n', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
  {'G', 'e', 'o', 'r', 'g', 'e', ' ', 'S', 'l', 'a', 'u', 'g', 'h', 't', 'e', 'r'}
};
byte timeshere[3];
byte acces_level[3];


void setup() {
  // put your setup code here, to run once:
  analogWrite(BUZZ, 128);
  delay(100);
  analogWrite(BUZZ, 0);

  lcd.begin(16, 2);
  lcd.print("SM125 125khz");
  lcd.setCursor(0, 2);
  lcd.print("RFID Start");
  delay(2000);
  Serial.begin(19200);
  Serial1.begin(19200);
  setParam();
  delay(200);
  //flush serial manually
  while (Serial1.available() > 0) {
    Serial1.read();
  }
  //***************************************************
  //***************************************************
  //before using this example remeber to write userIdentifier within cards
  //intialize users
  //user 1
  userIdentifier[0][0] = 0x15;
  userIdentifier[0][1] = 0x75;
  userIdentifier[0][2] = 0x57;
  userIdentifier[0][3] = 0x51;
  //user 2
  userIdentifier[1][0] = 0x22;
  userIdentifier[1][1] = 0x22;
  userIdentifier[1][2] = 0x22;
  userIdentifier[1][3] = 0x22;
  //user 3
  userIdentifier[2][0] = 0x87;
  userIdentifier[2][1] = 0x65;
  userIdentifier[2][2] = 0x43;
  userIdentifier[2][3] = 0x21;

  for (byte i = 0; i < 3; i++) {
    timeshere[i] = 0;
  }
  acces_level[0] = 1;
  acces_level[1] = 2;
  acces_level[2] = 3;
  setAutoMode();//keeps shield reading to detect whenever a card waves close to the antenna
  lcd.clear();
  lcd.print("Wave your card");
}

void loop() {

  while (Serial1.available()) {
    card = getUID();
    if (card != 255) {
      lcd.clear();
      lcd.print("Correct card...");
      delay(500);
      lcd.clear();
      lcd.print("User:");
      lcd.setCursor(0, 2);
      for (byte i = 0; i < 16; i++) {
        lcd.print(userName[card][i]);
      }
      delay(2000);
      timeshere[card]++;
      lcd.clear();
      lcd.print("You've been here");
      lcd.setCursor(0, 2);
      lcd.print(timeshere[card]);
      lcd.setCursor(4, 2);
      lcd.print("times today");
      delay(2000);


      lcd.clear();
      lcd.print("Acces level:");
      lcd.setCursor(0, 2);
      lcd.print(acces_level[card], DEC);
      delay(1500);
      switch (acces_level[card]) {

        case 1:
          lcd.clear();
          lcd.print("Acces denied!");
          setColour(red.r,red.g,red.b);
          delay(1000);
          analogWrite(BUZZ, 78);
          delay(1000);
          analogWrite(BUZZ, 0);
          break;

        case 2:

          lcd.clear();
          lcd.print("Acces granted!");
          lcd.setCursor(0, 2);
          lcd.print("Go ahead!");
          setColour(cyan.r, cyan.g, cyan.b);
          delay(2000);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          delay(100);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          break;

        case 3:

          lcd.clear();
          lcd.print("Total acces!");
          lcd.setCursor(0, 2);
          lcd.print("Go ahead!");
          setColour(purple.r, purple.g, purple.b);
          delay(2000);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          delay(100);
          analogWrite(BUZZ, 128);
          delay(100);
          analogWrite(BUZZ, 0);
          break;
      }
    }
    lcd.clear();
    lcd.print("Wave your card");
  }
}

// ***This cards do not have UID, we will save different data in their block2
// so we can identify them***
byte getUID() {

  if (Serial1.read() == 0x01) { //fixed header value
    lcd.clear();
    delay(10);
    byte len = Serial1.read();//message lenght
    delay(10);
    for (byte j = 0; j < 5; j++) {
      Serial1.read();//first 2 blocks will be our "UIDCard"
      delay(10);
    }
    /* for (byte i = 0; i < 4; i++) {
       Serial.read();//ignore config block

     }*/
    for (byte j = 0; j < 4; j++) {
      auxID [j] = Serial1.read();//first 2 blocks will be our "UIDCard"
    }
    //discard other data
    delay(500);
    while (Serial1.available() > 0) {
      Serial1.read();
    }
    byte user = -1;
    //compare block 2 with each "UID" above
    for (byte i = 0; i < 3; i++) {
      for (byte j = 0; j < 4; j++) {

        if (auxID[j] == userIdentifier[i][j]) {
          user = i;
        } else {
          j = 4;
        }

      }

    }
    return user;

  }
  return -1;
}


//**********************************************************************
//turns on led with a selected colour
void setColour(uint8_t r, uint8_t g, uint8_t b) {

  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);

}



byte setParam () {
  //Calculate checksum of all bytes except header
  byte msglength = 0x06;
  byte checksum = 0x01 + msglength + 0x40 + 0x32 + 0x64 + 0x5A + 0x1E + 0x60;
  
  //Build and execute command
  byte paramcommand[10] = {0xFF, 0x01, msglength, 0x40, 0x32, 0x64, 0x5A, 0x1E, 0x60, checksum};
        
        // send the request bytes
        for (byte i=0; i<10; i++) {
          Serial1.write(paramcommand[i]);
        }
  //flush buffer *critical*
  while (Serial1.available() > 0) {
    Serial1.read();
  }
   //give some time to module to do its stuff     
        delay(500);
        
	//verify successful completion of command
  if (Serial1.read() == 0xFF) {
    if (Serial1.read() == 0x01) {
      if (Serial1.read() == 0x01) {
        if (Serial1.read() == 0x99) {
          if (Serial1.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}

 byte setAutoMode () {
	//Calculate checksum of all bytes except header
	byte msglength = 0x09;
	byte checksum = 0x01 + msglength + 0x87 + 0x01 + 0x01 + 0x03;

        //Build and execute command
	byte cmdToExecute[13] = {0xFF, 0x01, msglength, 0x87, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, checksum};
        
        // send the request bytes
        for (int m=0; m<13; m++) {
			Serial1.write(cmdToExecute[m]);
        }
        //flush buffer *critical*
  while (Serial1.available() > 0) {
    Serial1.read();
  }
        delay(500);
        
	//verify successful completion of command
	  if (Serial1.read() == 0xFF) {
    if (Serial1.read() == 0x01) {
      if (Serial1.read() == 0x01) {
        if (Serial1.read() == 0x99) {
          if (Serial1.read() == 0x9B) {
            return 0;
          }
          else return 1;
        }
        else return 1;
      }
      else return 1;
    }
    else return 1;
  }
  else return 1;
}
		

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: