


RFID Button: Having a diameter of 30mm, this button holds a unique 32 bit ID. This ID is read by the ID-12LA RFID, or any other device using 125kHZ module. The unique serial number of each button makes them useful as a key system, or to track individual objects. Refer to the second image in the previous step for more information.

ATMEGA32U2 USB Dev Board: This device is made compatible for the RFID reader (ID-12) that we are using to do this tutorial. It contains an Atmega32u2 microcontroller and an extra pin that can be used to control other sensors. Also the board has an LED and Buzzer to indicate whether the reader made a scan or not.

This sensor is used to measure distance by using ultrasonic waves. Ultrasonic describes sound that has a frequency above human ear limit, which is about 20,000Hz. The device can measure a distance from about 2cm (0.8in) to 3m (3.3yd). It works by sending an ultrasonic wave, and then provides an output pulse to approximate the time it took for the echoed wave to return to the sensor. The distance is the result of the rate of speed multiplied with how long it took for the wave to hit the object and to echo back to the sensor. These kinds of sensors are widely used in robotic applications.
Distance = Speed of Light * Time
1. Place a black and red wire on the breadboard. Connect the red wire to 5V on the Arduino while the black wire is connected to the GND.

2. Place the Ping Ultrasonic sensor on the breadboard. Connect the left most pin to the GND, the middle to the power supply (+5V), and the right most pin to digital pin 6 on the Arduino.

3. Attach ID-12 reader to the Atmega32U2 USB Dev board and place it on the breadboard.
Then connect from the board to the Arduino/ breadboard as follows:

4. Place the LEDs on the breadboard and connect the resistors from the GND to the short pin of the LED.
Connect the long pin to the digital pin 7 for the Red LED and digital pin 9 for the Green LED.
/*Using RFID ID-12 to read unique character that is assigned to each RFID button or tag*/ char val = 0; //variable to store the char read from the RFID buttonvoid setup() {Serial.begin(9600); //connect to the serial port} void loop () {//read the serial portif(Serial.available() > 0)val = Serial.read(); //read from one char from the ID-12 and store it Serial.print(val); //display the char in the serial monitor}}
/*This program uses ID-12 reader to read any RFID button or card, and Identify if this ID is known in the system*/ // include a header file to........... #include SoftwareSerial mySerial(3,2); // virtual serial port ////////////////////////////////////////////////////////////////// int R_LED= 7; // pin attached to the red LED int G_LED = 9; int LEDpin= 13; int Reset = 12; int Sonic_pin = 6; // pin from the ultrasonic sensor ///////////////////////////////////////////////////////////// char val = 0; // what if it's byte val...how many is it storing // because I already read and stored the ID's and I assigned it to the people char id_tag1[] ="78003BDE66FB"; char id_tag2[] = "78003BF78B3F"; char* nam_tag[] = {"Derek", "Jay"}; char IDstring[13]; int i=0; int p =0; //////////////////////////////////////////////////////////// void setup() { Serial.begin (9600); // begin serial communication mySerial.begin(9600); pinMode (Sonic_pin, INPUT); pinMode (R_LED, OUTPUT); pinMode (G_LED, OUTPUT); pinMode (Reset, OUTPUT); pinMode (LEDpin, OUTPUT); digitalWrite(LEDpin ,LOW); } void loop() { //calculate the distance measured from the ping ultrasonic sensor until the target is less than 4cm away float distance; do{ float time; // send out a pulse tone by creating pinMode(Sonic_pin, OUTPUT); digitalWrite(Sonic_pin, LOW); delay(2); digitalWrite(Sonic_pin, HIGH); delay(5); digitalWrite(Sonic_pin, LOW); //let the ultrasonic sensor be an input to take back the echoed wave pinMode(Sonic_pin, INPUT); time = pulseIn(Sonic_pin, HIGH); // get the echoed duration in microsecond distance = SecToCm(time); // call function to calculate the distance delay(1000);} while (distance>4.0); Serial.println("Hello, please swipe your ID"); //Serial.println(" "); delay(2000); //Open_Door(); Red_tag(); Iden_tag(); resetID(); Serial.println(" "); } //////////////////////////////////////////////////////////////////////////// void Access() { // turns the green LED on if the ID is known to the system digitalWrite(G_LED, HIGH); delay(2000); digitalWrite(G_LED, LOW); } /////////////////////////////////////////////////////////////////////////////// void Denied() { // turns the red LED on if the ID is not known to the system for(int i=0; i<3; i++) { digitalWrite(R_LED, HIGH); delay(1000); digitalWrite(R_LED, LOW); delay(300); } } ////////////////////////////////////////////////////////////////////////////// float SecToCm(float time) { // convert the time measured into distance in centimeter //the ultrasonic read 29 microseconds per centimeter return time/29.0/2.0 ; } /////////////////////////////////////////////////////////////////////////////////////////////// void Red_tag() { // if there is a radio frequency available from let the ID-12 read each char and store the value while (mySerial.available() >0) { //for(p=0;p<13;p++) { val= mySerial.read(); IDstring[p] = val; //Serial.println(val); p++; } } p=0; delay(500); Serial.print("ID Number: "); delay(500); // print out on the serial monitor the RFID unique ID stored in string for ( i=0; i<13;i++) { Serial.print(IDstring[i]); } Serial.println(); delay(1000); Serial.print("Please wait while checking"); Serial.println(); } //////////////////////////////////////////////////////////////////////////////// void Iden_tag() { boolean reading = true; //to check if the ID is known boolean reading1 = false;// to check if the first ID is not known boolean reading2 = false;// to check if the second ID is not known for (int i=0; i<12;i++)// comparing the new button read to the ID 2 stored in the system { if (IDstring[i+1] != id_tag2[i]) { reading2= true; //indicate that the ID is not the same reading = false; // break; } } if (reading==true) { delay (1500); Serial.println("Access granted"); Serial.print("ID belongs to "); Serial.println(nam_tag[1]); Access(); // call a function to light up the green LED delay(2000); } reading = true; for (int i=0; i<12;i++)// why 12 not 13 try to figure out { if (IDstring[i+1] != id_tag1[i]) { reading1= true; reading = false; //break; } } { if (reading==true) { delay (1500); Serial.println("Access granted"); Serial.print("ID belongs to "); Serial.println(nam_tag[0]); Access(); delay(2000); } if (reading1== true && reading2 ==true) { delay(1000); Denied(); Serial.println("Access Denied"); Serial.println("Please try again "); } } } /////////////////////////////////////////////////////////// void resetID() { for(int i = 0; i < 13; i++){ IDstring[i] = 0; } }
The first part of the code includes the serial header file that is used to implement serial communication between the Arduino and RFID reader module. We are then going to create an object, in this case “mySerial”, to assign the communication pins. Then we define the global variable pin that is used by the LED and ping ultrasonic sensor. Also, as a global variable, we have created a character array to hold the two ID numbers that we got from the previous code as well as two names that are assigned for each ID number. These variables can be used by any function declard in this program.
Inside the setup function we have defined the serial communication between the computer and the Arduino as well as ID-12 and Arduino using same frequency band (9600). Then we defined which pin is used as an output or input. When we look at the loop function, the first thing we did was use another loop known as do while loop. Inside this loop, we calculated the distance at least once and checked if the target is located less than 4cm away. In order to calculate the distance, we first have to send out a pitched tone from the ultrasonic sensor. And this is done by sending Low-High-Low sequence by triggering digital pin 6. After it hit a target and returned back to the sensor, the sensor will output echo pulse. Using pulseln(), we can measure the echoed time pulse in microsecond and then converted it into distance. According to the datasheet for ping ultrasonic sensor, the speed of the sound is 340m/s and that means there are 29 seconds per centimeter. Calling out function Red_tag, we are going to scan and store any RFID button. In order to do this, we created a new array to hold on to each character. The Iden_tag function is going to compare the button that was stored on to the new array and the Id_tag created at the beginning of the code. Then we are going to see the output on the serial monitor as well as the LEDs.

Above is a screenshot of the serial monitor displaying the output.
If the detector doesn’t work after running the code, check the connection as well as any mistyping when writing the code. As we conclude this tutorial, we can now use this project for monitoring any object with a RFID tag.
Ready to get started? DFRobot online store has all the products you need to create your own project. While you're there, check out the other tutorials we have available for you! Also, if you haven't already, explore more projects here.
If you have any questions about this tutorial, do not hesitate to post a comment, shoot us an email, or post it in our forum.
Thanks for reading!
Source: Instructables