Smart Cards (RFID and NFC)

Smart Cards (RFID and NFC)

One of the very cool technologies that the Cube makes it easy to play with is Radio Frequency Identification or RFID for short, and its even more useful but specialized close relative Near Field Communications (NFC).

RFID allows wireless communication between an RFID reader and a battery-less microchip (or RFID tag as its often known) at a distance from a few centimeters up to 25 or so meters (depending). Each tag usually has a unique identification number, and using a specialized subset of the RFID protocol called NFC (Near Field Communications) some of the smarter cards (like the ones in our kits) also allow storing and retrieving small amounts (roughly 1000 bytes, depending on the card) of information to and from the card over much shorter distance of 1 to 5 centimeters.

Sparkfun has a great article covering the basics of RFID.

RFID tag electronics

The tags do not have batteries, but instead have an antenna that harvests enough energy from the radio pulses put out by the RFID reader to briefly powerup the chip and reply to the reader before powering down. The antenna is not a big clunky thing as you may be imagining, but is often as thin as a sheet of paper and coiled around the tiny tag chip for a very compact piece of electronics. This means there are no moving parts, and since there’s no battery, the tags can work forever (well, indefinitely to be more precise); and since tags can be manufactured for as cheap as a few cents, they can even be disposable.

So why is this useful, and what can you do with it?

RFID tags are enormously useful! Because they can be powered wirelessly by a reader from a distance, have unique identifiers, can store information, and are really cheap to produce, they have a huge range of applications and are available in a dazzling variety of shapes and sizes:

  • Tracking
    • RFID tags are used in airports across the world to track luggage. When you check-in, you get a disposable tag with a unique identifier, attached to your luggage which allows automated reading and tracking of your luggage as it moves along conveyor belts or is loaded/offloaded during its journey
    • Similarly, pallets and other shipping items around the world are tracked using RFID tags
    • RFID tags inside a disposable wristband are popular for patient tracking in hospitals or timing marathon runners, or when made ruggedized and waterproof inside a cheap silicone wristband handy for tracking or validating outdoor concertgoers or kids at waterparks.
  • Asset management
    • In forestry applications, you get ‘smart nails’ – a little RFID chip inside a fibreglass nail that can be embedded in trees throughout the forest to enable future identification and tracking
    • Many underground pipes and cables are now manufactured with RFID chips embedded at intervals inside the pipe or cable. This allows future maintenance or location work to be made much easier as a technician can simply scan with a reader from the surface to identify exactly which cables are below them, to whom those cables belong, when they were installed, and any other information related to the tag’s identity. Since the tags are not powered, then can provide this smart capability essentially forever.
  • Security
    • Hotels around the world issue RFID cards to guests that allow guests to open the electronic locks on their room doors as a replacement for keys. If a card gets lost, or the stay has been expired, no problem – the lock is simply re-configured to reject the old RFID tag number and only accept replacement ones
  • Many cellphones have RFID (more specifically NFC) tags and reader inside them
  • Your bank card almost certainly includes an NFC tag

Using your RFID tags

There are several different RFID systems available that run at different radio frequencies (you can think of this like channels on a radio). If you have an 860MHz reader, it will not be able to read a tag designed for 13.56MHz or 433MHz.

The reader and card you get from us are the common and popular 13.56MHz system that is also used for NFC communications, so is compatible with your phone, hotel room cards, and your credit card!

The reader unit has a red connector so prefers to be connected to the red Port A on your cube. Cards are read and written by bringing them to within 1 or 2 centimeters of the marked flat part of the reader (with the red RFID symbol) – or more conveniently just ‘tapping’ the reader with the card.

If you want to see what the RFID chip and antenna inside the cards look like, just hold the card up to a bright light (the torch on your cellphone should be fine) and look for the long loop of the antenna and a dark ‘block’ where the chip itself is)!

RFID reader Unit
RFID reader Unit
RFID Cards
RFID Cards

UIFlow blocks

UIFlow makes available four blocks for reading and writing to or from the supplied smart cards.

UIFlow RFID programming blocks
UIFlow RFID programming blocks

‘card near’ block

Starting from the third block in the screenshot above, the ‘card near’ block returns a True or False value every time you call it to sense if there is an RFID tag within the range of the reader. You typically use this block as the test condition of an If block, and often you will want to briefly flash the LED bars or play a sound to give feedback to the user that the card has been detected.

‘card uid’ block

The bottom ‘card uid’ block returns the Unique ID (uid) of the card being read. This unique id is not a decimal number as you might expect, but is rather a hexadecimal number (a number that uses 16 as its base instead of 10 like most of us are used to) consisting of the digits 0-9 and letters a-f. For the cards we supply, the uids are 10 characters long and look something like ‘ec7cc39ccf’ as shown in the example below.

Using just the ‘card uid’ block you can already build a bunch of useful access control and tracking applications!

UIFlow program to read Unique ID (uid) of card
UIFlow program to read Unique ID (uid) of card
Card Unique ID (uid)
Card Unique ID (uid)

‘read string’ and ‘write’ string blocks

These two blocks let you store and retrieve information to the smart cards using NFC communications (a specialized subset of the more general RFID).

Data is stored on the cards we supply in chunks of only 16 characters at a time. There are 62 available chunks, with each chunk having an address number. The read and write blocks both have an addr field that accepts a number indicating the address of the chunk you want to read or write from. So for example, you may store the person’s name at address 1, their surname at address 2, and their card money balance at address 3.

Data is always stored on the card as a string data type. If you try write a number such as 29, the code in the block will silently convert this number into the string ’29’ which to a computer is a different thing to the number 29. When you read the value of a chunk, you also get back a string – so if you are expecting a number, you must take care to use the ‘convert to float’ block in the Math menu to explicitly convert the string to a number that your code can reliably work with. If you don’t do this conversion, your code will still run, but you will get subtle and confusing logic errors because your code will invisibly try to do arithmetic or logic operations on the string value of the number you think you have and give incorrect results.

RFID example application

Above is an example RFID application with a load capability to set a starting balance (just a number) on a card, then a read capability to reads the balance from a presented card, deduct 10, and store the resulting number back on the card. The video below shows the example app in action.

Some items of interest in this example:

There is a Boolean logic variable called ‘loading’ that keeps track of what mode the application is in – loading or reading. If loading, then we show the buttons to set and save the balance, and if reading then we automatically deduct and save the new balance.

At the end of the first if block, there is a ‘repeat while card near’ loop with a wait inside it. All that loop does is wait for the person to remove their card – otherwise the main yellow loop would run several hundred times in the time it takes a human to tap the card! So this loop makes sure that we only deduct the balance once, and waits for the card to be removed before the loop can continue

Challenge yourself!

Why not try build these projects:

  • Try make a simple lock out of a servo motor (have it move a door bolt for example), where the servo only opens the door if a card with the right UID or person’s name, or minimum credit balance is detected.
  • Have your desktop lighting match your mood – colour code your cards with some stickers, and have your LED light strip light up with the corresponding colour when that card it tapped. Think about extending this to other things in your room that you could control

Related Articles

Responses