Posts | Comments

Planet Arduino

Archive for the ‘access’ Category

Nov
19

Arduino Tutorials – Chapter 15 – RFID

125 kHz, access, arduino, EM4100, lesson, RDM630, rfid, RFR101A1M, RFR103B2B, seeedstudio, system, tronixstuff, TTL, tutorial Commenti disabilitati su Arduino Tutorials – Chapter 15 – RFID 

Learn how to use RFID readers with your Arduino. In this instalment we use an RDM630 or RDM6300 RFID reader. If you have an Innovations ID-12 or ID-20 RFID reader, we have a different tutorial for you.

This is chapter fifteen of our huge Arduino tutorial seriesUpdated 19/11/2013

Introduction

RFID – radio frequency identification. Some of us have already used these things, and they have become part of everyday life. For example, with electronic vehicle tolling, door access control, public transport fare systems and so on. It sounds complex – but isn’t.

To explain RFID for the layperson, we can use a key and lock analogy. Instead of the key having a unique pattern, RFID keys hold a series of unique numbers which are read by the lock. It is up to our Arduino sketch to determine what happens when the number is read by the lock.  The key is the tag, card or other small device we carry around or have in our vehicles. We will be using a passive key, which is an integrated circuit and a small aerial. This uses power from a magnetic field associated with the lock. Here are some key or tag examples:

Arduino RFID tags

In this tutorial we’ll be using 125 kHz tags – for example. To continue with the analogy our lock is a small circuit board and a loop aerial. This has the capability to read the data on the IC of our key, and some locks can even write data to keys. Here is our reader (lock) example:

Seeedstudio RFID reader Arduino

These readers are quite small and inexpensive – however the catch is that the loop aerial is somewhat fragile. If you need something much sturdier, consider the ID20 tags used in the other RFID tutorial.

Setting up the RFID reader

This is a short exercise to check the reader works and communicates with the Arduino. You will need:

Simply insert the RFID reader main board into a solderless breadboard as shown below. Then use jumper wires to connect the second and third pins at the top-left of the RFID board to Arduino 5V and GND respectively. The RFID coil connects to the two pins on the top-right (they can go either way). Finally, connect a jumper wire from the bottom-left pin of the RFID board to Arduino digital pin 2:

Arduino RFID reader setup

Next, upload the following sketch to your Arduino and open the serial monitor window in the IDE:

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

int i;

void setup()
{
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC 
}

void loop()
{
  if (RFID.available() > 0) 
  {
     i = RFID.read();
     Serial.print(i, DEC);
     Serial.print(" ");
  }
}

If you’re wondering why we used SoftwareSerial – if you connect the data line from the RFID board to the Arduino’s RX pin – you need to remove it when updating sketches, so this is more convenient.

Now start waving RFID cards or tags over the coil. You will find that they need to be parallel over the coil, and not too far away. You can experiment with covering the coil to simulate it being installed behind protective surfaces and so on. Watch this short video which shows the resulting RFID card or tag data being displayed in the Arduino IDE serial monitor.

As you can see from the example video, the reader returns the card’s unique ID number which starts with a 2 and ends with a 3. While you have the sketch operating, read the numbers from your RFID tags and note them down, you will need them for future sketches.

To do anything with the card data, we need to create some functions to retrieve the card number when it is read and place in an array for comparison against existing card data (e.g. a list of accepted cards) so your systems will know who to accept and who to deny. Using those functions, you can then make your own access system, time-logging device and so on.

Let’s demonstrate an example of this. It will check if a card presented to the reader is on an “accepted” list, and if so light a green LED, otherwise light a red LED. Use the hardware from the previous sketch, but add a typical green and red LED with 560 ohm resistor to digital pins 13 and 12 respectively. Then upload the following sketch:

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

int data1 = 0;
int ok = -1;
int yes = 13;
int no = 12;

// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
int tag1[14] = {2,52,48,48,48,56,54,66,49,52,70,51,56,3};
int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3};
int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons

void setup()
{
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC 
  pinMode(yes, OUTPUT); // for status LEDs
  pinMode(no, OUTPUT);
}

boolean comparetag(int aa[14], int bb[14])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 14 ; cc++)
  {
    if (aa[cc] == bb[cc])
    {
      fg++;
    }
  }
  if (fg == 14)
  {
    ff = true;
  }
  return ff;
}

void checkmytags() // compares each tag against the tag just read
{
  ok = 0; // this variable helps decision-making,
  // if it is 1 we have a match, zero is a read but no match,
  // -1 is no read attempt made
  if (comparetag(newtag, tag1) == true)
  {
    ok++;
  }
  if (comparetag(newtag, tag2) == true)
  {
    ok++;
  }
}

void readTags()
{
  ok = -1;

  if (RFID.available() > 0) 
  {
    // read tag numbers
    delay(100); // needed to allow time for the data to come in from the serial buffer.

    for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
    {
      data1 = RFID.read();
      newtag[z] = data1;
    }
    RFID.flush(); // stops multiple reads

    // do the tags match up?
    checkmytags();
  }

  // now do something based on tag type
  if (ok > 0) // if we had a match
  {
    Serial.println("Accepted");
    digitalWrite(yes, HIGH);
    delay(1000);
    digitalWrite(yes, LOW);

    ok = -1;
  }
  else if (ok == 0) // if we didn't have a match
  {
    Serial.println("Rejected");
    digitalWrite(no, HIGH);
    delay(1000);
    digitalWrite(no, LOW);

    ok = -1;
  }
}

void loop()
{
  readTags();
}

In the sketch we have a few functions that take care of reading and comparing RFID tags. Notice that the allowed tag numbers are listed at the top of the sketch, you can always add your own and more – as long as you add them to the list in the function checkmytags() which determines if the card being read is allowed or to be denied.

The function readTags() takes care of the actual reading of the tags/cards, by placing the currently-read tag number into an array which is them used in the comparison function checkmytags(). Then the LEDs are illuminated depending on the status of the tag at the reader. You can watch a quick demonstration of this example in this short video.

 

Conclusion

After working through this chapter you should now have a good foundation of knowledge on using the inexpensive RFID readers and how to call functions when a card is successfully read. For example, use some extra hardware (such as an N-MOSFET) to control a door strike, buzzer, etc. Now it’s up to you to use them as a form of input with various access systems, tracking the movement of people or things and much more.

And if you enjoyed the tutorial, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop” from No Starch Press.

tronixstuff

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Arduino Tutorials – Chapter 15 – RFID appeared first on tronixstuff.

Feb
25

Learn how to use RFID readers with your Arduino. In this instalment we use the Innovations ID-20 RFID reader. The ID-12 and ID-2 are also compatible. If you have the RDM630 or RDM6300 RFID reader, we have a different tutorial.

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Updated 26/02/2013

RFID – radio frequency identification. Some of us have already used these things, and they have become part of everyday life. For example, with electronic vehicle tolling, door access control, public transport fare systems and so on. It sounds complex – but isn’t. In this tutorial we’ll run through the basics of using the ID-20 module then demonstrate a project you can build and expand upon yourself.

Introduction

To explain RFID for the layperson, we can use a key and lock analogy. Instead of the key having a unique pattern, RFID keys hold a series of unique numbers which are read by the lock. It is up to our software (sketch) to determine what happens when the number is read by the lock.  The key is the tag, card or other small device we carry around or have in our vehicles. We will be using a passive key, which is an integrated circuit and a small aerial. This uses power from a magnetic field associated with the lock. Here are some key or tag examples:

In this tutorial we’ll be using 125 kHz tags – for example. To continue with the analogy our lock is a small circuit board and a loop aerial. This has the capability to read the data on the IC of our key, and some locks can even write data to keys. And out reader is the Innovations ID-20 RFID reader:

Unlike the RDM630 reader in the other RFID tutorial – the ID-20 is a complete unit with an internal aerial and has much larger reader range of around 160 mm. It’s a 5V device and draws around 65 mA of current. If you have an ID-12 it’s the same except the reader range is around 120mm; and the ID-2 doesn’t have an internal aerial. Connecting your ID-20 reader to the Arduino board may present a small challenge and require a bit of forward planning. The pins on the back of the reader are spaced closer together than expected:

… so a breakout board makes life easier:

… and for demonstration and prototyping purposes, we’ve soldered on the breakout board with some header pins:

 The first thing we’ll do is connect the ID-20 and demonstrate reading RFID tags. First, wire up the hardware as shown below:

If you’re using the breakout board shown earlier, pin 7 matches “+/-” in the diagram above. Next, enter and upload the following sketch (download):

// Example 15a.1
#include <SoftwareSerial.h>
SoftwareSerial id20(3,2); // virtual serial port
char i;
void setup() 
{
 Serial.begin(9600);
 id20.begin(9600);
}
void loop () 
{
 if(id20.available()) {
 i = id20.read(); // receive character from ID20
 Serial.print(i); // send character to serial monitor
 Serial.print(" ");
 }
}

Note that we’re using a software serial port for our examples. In doing so it leaves the Arduino’s serial lines for uploading sketches and the serial monitor. Now open the serial monitor window, check the speed is set to 9600 bps and wave some tags over the reader – the output will be displayed as below (but with different tag numbers!):

Each tag’s number starts with a byte we don’t need, then twelve that we do, then three we don’t. The last three aren’t printable in the serial monitor. However you do want the twelve characters that appear in the serial monitor.  While running this sketch, experiment with the tags and the reader… get an idea for how far away you can read the tags. Did you notice the tag is only read once – even if you leave it near the reader? The ID-20 has more “intelligence” than the RDM630 we used previously. Furthermore when a tag is read, the ID-20 sends a short PWM signal from pin 10 which is  just under 5V and lasts for around 230 ms, for example (click image to enlarge):

 This signal can drive a piezo buzzer or an LED (with suitable resistor). Adding a buzzer or LED would give a good notification to the user that a card has been read. While you’re reading tags for fun, make a note of the tag numbers for your tags – you’ll need them for the next examples.

RFID Access System

Now that we can read the cards, let’s create a simple control system. It will read a tag, and if it’s in the list of allowed tags the system will do something (light a green LED for a moment). Plus we have another LED which stays on unless an allowed tag is read.  Wire up the hardware as shown below (LED1 is red, LED2 is green – click image to enlarge):

Now enter and upload the following sketch (download):

// Example 15a.2
#include 
SoftwareSerial id20(3,2); // virtual serial port
// add your tags here. Don't forget to add to decision tree in readTag();
String Sinclair = "4F0023E2129C";
String Smythe = "4F0023CC9737";
String Stephen = "010044523C2B";
String testcard; 
char testtag[12]; 
int indexnumber = 0; 
char tagChar;
void setup() 
{
 Serial.begin(9600);
 pinMode(7, OUTPUT); // this if for "rejected" red LED
 pinMode(9, OUTPUT); // this will be set high when correct tag is read. Use to switch something on, for now - a green LED. 
 id20.begin(9600); 
 digitalWrite(7, LOW);
 digitalWrite(9, LOW);
}
void approved()
// when an approved card is read
{
 digitalWrite(9, HIGH);
 Serial.println("yes"); 
 delay(1000);
 digitalWrite(9, LOW);
}
void notApproved()
// when an unlisted card is read
{
 digitalWrite(7, HIGH);
 Serial.println("no");
 delay(100);
 digitalWrite(7, LOW);
}
void readTag()
{
 tagChar = id20.read();
 if (indexnumber != 0) // never a zero in tag number
 {
 testtag[indexnumber - 1] = tagChar;
 }
 indexnumber++;
 if (indexnumber == 13 ) // end of tag number
 {
 indexnumber = 0;
 testcard = String(testtag);
 if (testcard.equals(Sinclair)) { 
 approved(); 
 } 
 else if (testcard.equals(Smythe)) { 
 approved(); 
 }
 else if (testcard.equals(Stephen)) { 
 approved(); 
 }
 else { 
 notApproved(); 
 }
 }
}
void loop()
{
 readTag();
}

In the function readCard() the sketch reads the tag data from the ID-20, and stores it in an array testtag[]. The index is -1 so the first unwanted tag number isn’t stored in the array. Once thirteen numbers have come through (the one we don’t want plus the twelve we do want) the numbers are smooshed together into a string variable testcard with the function String. Now the testcard string (the tag just read) can be compared against the three pre-stored tags (Sinclair, Smythe and Stephen).

Then it’s simple if… then… else to to see if we have a match, and if so – call the function approved() or disApproved as the case may be. In those two functions you store the actions you want to occur when the correct card is read (for example, control a door strike or let a cookie jar open) or when the system is waiting for another card/a match can’t be found. If you’re curious to see it work, check the following video where we take it for a test run and also show the distances that you have to work with:

Hopefully this short tutorial was of interest. We haven’t explored every minute detail of the reader – but you now have the framework to move forward with your own projects.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Learn how to use RFID readers with your Arduino. In this instalment we use the Innovations ID-20 RFID reader. The ID-12 and ID-2 are also compatible. If you have the RDM630 or RDM6300 RFID reader, we have a different tutorial.

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Updated 26/02/2013

RFID – radio frequency identification. Some of us have already used these things, and they have become part of everyday life. For example, with electronic vehicle tolling, door access control, public transport fare systems and so on. It sounds complex – but isn’t. In this tutorial we’ll run through the basics of using the ID-20 module then demonstrate a project you can build and expand upon yourself.

Introduction

To explain RFID for the layperson, we can use a key and lock analogy. Instead of the key having a unique pattern, RFID keys hold a series of unique numbers which are read by the lock. It is up to our software (sketch) to determine what happens when the number is read by the lock.  The key is the tag, card or other small device we carry around or have in our vehicles. We will be using a passive key, which is an integrated circuit and a small aerial. This uses power from a magnetic field associated with the lock. Here are some key or tag examples:

In this tutorial we’ll be using 125 kHz tags – for example. To continue with the analogy our lock is a small circuit board and a loop aerial. This has the capability to read the data on the IC of our key, and some locks can even write data to keys. And out reader is the Innovations ID-20 RFID reader:

Unlike the RDM630 reader in the other RFID tutorial – the ID-20 is a complete unit with an internal aerial and has much larger reader range of around 160 mm. It’s a 5V device and draws around 65 mA of current. If you have an ID-12 it’s the same except the reader range is around 120mm; and the ID-2 doesn’t have an internal aerial. Connecting your ID-20 reader to the Arduino board may present a small challenge and require a bit of forward planning. The pins on the back of the reader are spaced closer together than expected:

… so a breakout board makes life easier:

… and for demonstration and prototyping purposes, we’ve soldered on the breakout board with some header pins:

 The first thing we’ll do is connect the ID-20 and demonstrate reading RFID tags. First, wire up the hardware as shown below:

If you’re using the breakout board shown earlier, pin 7 matches “+/-” in the diagram above. Next, enter and upload the following sketch (download):

// Example 15a.1
#include <SoftwareSerial.h>
SoftwareSerial id20(3,2); // virtual serial port
char i;
void setup() 
{
 Serial.begin(9600);
 id20.begin(9600);
}
void loop () 
{
 if(id20.available()) {
 i = id20.read(); // receive character from ID20
 Serial.print(i); // send character to serial monitor
 Serial.print(" ");
 }
}

Note that we’re using a software serial port for our examples. In doing so it leaves the Arduino’s serial lines for uploading sketches and the serial monitor. Now open the serial monitor window, check the speed is set to 9600 bps and wave some tags over the reader – the output will be displayed as below (but with different tag numbers!):

Each tag’s number starts with a byte we don’t need, then twelve that we do, then three we don’t. The last three aren’t printable in the serial monitor. However you do want the twelve characters that appear in the serial monitor.  While running this sketch, experiment with the tags and the reader… get an idea for how far away you can read the tags. Did you notice the tag is only read once – even if you leave it near the reader? The ID-20 has more “intelligence” than the RDM630 we used previously. Furthermore when a tag is read, the ID-20 sends a short PWM signal from pin 10 which is  just under 5V and lasts for around 230 ms, for example:

id20pulse

 This signal can drive a piezo buzzer or an LED (with suitable resistor). Adding a buzzer or LED would give a good notification to the user that a card has been read. While you’re reading tags for fun, make a note of the tag numbers for your tags – you’ll need them for the next examples.

RFID Access System

Now that we can read the cards, let’s create a simple control system. It will read a tag, and if it’s in the list of allowed tags the system will do something (light a green LED for a moment). Plus we have another LED which stays on unless an allowed tag is read.  Wire up the hardware as shown below (LED1 is red, LED2 is green – click image to enlarge):

Now enter and upload the following sketch:

// Example 15a.2
#include <SoftwareSerial.h>
SoftwareSerial id20(3,2); // virtual serial port
// add your tags here. Don't forget to add to decision tree in readTag();
String Sinclair = "4F0023E2129C";
String Smythe = "4F0023CC9737";
String Stephen = "010044523C2B";
String testcard; 
char testtag[12]; 
int indexnumber = 0; 
char tagChar;
void setup() 
{
 Serial.begin(9600);
 pinMode(7, OUTPUT); // this if for "rejected" red LED
 pinMode(9, OUTPUT); // this will be set high when correct tag is read. Use to switch something on, for now - a green LED. 
 id20.begin(9600); 
 digitalWrite(7, LOW);
 digitalWrite(9, LOW);
}
void approved()
// when an approved card is read
{
 digitalWrite(9, HIGH);
 Serial.println("yes"); 
 delay(1000);
 digitalWrite(9, LOW);
}
void notApproved()
// when an unlisted card is read
{
 digitalWrite(7, HIGH);
 Serial.println("no");
 delay(100);
 digitalWrite(7, LOW);
}
void readTag()
{
 tagChar = id20.read();
 if (indexnumber != 0) // never a zero in tag number
 {
 testtag[indexnumber - 1] = tagChar;
 }
 indexnumber++;
 if (indexnumber == 13 ) // end of tag number
 {
 indexnumber = 0;
 testcard = String(testtag);
 if (testcard.equals(Sinclair)) { 
 approved(); 
 } 
 else if (testcard.equals(Smythe)) { 
 approved(); 
 }
 else if (testcard.equals(Stephen)) { 
 approved(); 
 }
 else { 
 notApproved(); 
 }
 }
}
void loop()
{
 readTag();
}

In the function readCard() the sketch reads the tag data from the ID-20, and stores it in an array testtag[]. The index is -1 so the first unwanted tag number isn’t stored in the array. Once thirteen numbers have come through (the one we don’t want plus the twelve we do want) the numbers are smooshed together into a string variable testcard with the function String. Now the testcard string (the tag just read) can be compared against the three pre-stored tags (Sinclair, Smythe and Stephen).

Then it’s simple if… then… else to to see if we have a match, and if so – call the function approved() or disApproved as the case may be. In those two functions you store the actions you want to occur when the correct card is read (for example, control a door strike or let a cookie jar open) or when the system is waiting for another card/a match can’t be found. If you’re curious to see it work, check the following video where we take it for a test run and also show the distances that you have to work with:

Hopefully this short tutorial was of interest. We haven’t explored every minute detail of the reader – but you now have the framework to move forward with your own projects.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Arduino tutorial 15a – RFID with Innovations ID-20 appeared first on tronixstuff.



  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • Like Us on Facebook