Easy RFID With Phidgets

by InformalAbsence in Circuits > Gadgets

526 Views, 2 Favorites, 0 Comments

Easy RFID With Phidgets

20211114_143423.jpg

RFID is becoming more and more common in today's day and age. It is used in access control systems, like schools, hotels and workplaces. It is used on toll roads and ferries, and most commonly has evolved to be used in your phone, as well as your bank cards.


In this instructable, I will be going over the Phidgets 1024_0 RFID Read-Writer and showing you how to read, write, and code for it using Python. It supports the two most common protocols EM4100 and ISO11785_FDX_B as well as its own Phidget protocol. If you have never heard of those before don't worry, as I will briefly cover them below.


Although the examples given will be written in Python coding with Phidgets is easy enough that you should be able to adapt this to any language supported!

Supplies

20211114_143423.jpg

Setting Up Your Environment

20211114_143552.jpg

First, you will want to open up your IDE or editor of choice and make a new Python3 file with a name that is easy to keep track of. Next you will want to plug the USB cable into the RFID Phidget and into your computer. Doing so should give you a notification that Windows is setting up the 1024_0, but if it doesn't don't worry.

Installing the Phidgets Libraries

If you have never worked with Phidgets before you will need to install the Phidgets Libraries. To do this you will first need to make sure you have the latest version of pip. If you don't when trying to run the install command pip will prompt you to run

python3 -m pip install --upgrade pip 


After updating pip you can install the Phidgets Libraries by running

python3 -m pip install Phidget22Native

Testing the Connection

Capture.PNG

Now that you are ready to go we will first write a small bit of code to read the data from a Phidget Tag. The tags will all come with meaningless data on them, so it will be a good first indication that everything is working right. The API for the 1024_0 can be found by clicking the link in supplies and scrolling to the bottom of the page, there you can see all the supported calls and functions for working with it.


For the code below, I start by enabling the antenna and waiting 1 second. After waiting I read the tag before disabling the antenna again. This will only work if your tag is placed on the RFID Phidget before you run the program, so you should place it there first.


from Phidget22.Devices.RFID import *
import time

# Init Object
rfid = RFID()
# Open Phidget RFID with 1000ms timeout
rfid.openWaitForAttachment(1000)

# Enable Antenna
rfid.setAntennaEnabled(True)
# Sleep 1s
time.sleep(1)
# Read tag
print(rfid.getLastTag())
# Disable Antenna
rfid.setAntennaEnabled(False)


You will notice that the data you get back will look something like this

('5f00dbc416', 1)


The reason is that the getLastTag function returns a tuple, the first parameter being the data from the tag and the second being the protocol used. You can look at the image above from the API to help you decode what protocol your tag is using. You can see the tag I read had the data '5f00dbc416' and was using the EM4100 protocol.

Protocols

The main difference between the protocols used actually relates to the certification that they have, the frequency that they transmit on, and how the data is encoded. Data written using the EM4100 protocol must be in hexadecimal format. If you are using ISO11785 FDX B then the data will be written in decimal format. Finally, the Phidget Tag Protocol allows you to write data in plain text.


For this tutorial, it really doesn't matter which one is used, as the RFID Phidget will look for all three variants, but my card happens to be using the EM4100 protocol so that is what will be demonstrated going forward.

Writing Data

Now that we can read data, let's write it. I will be using the same code provided above as a starting point, and making some modifications from there. Just before we read the tag, I am going to add the write command. It is important to note that data must be written in hexadecimal as I am using the EM4100 protocol.


from Phidget22.Devices.RFID import *
import time


# Init Object
rfid = RFID()
# Open Phidget RFID with 1000ms timeout
rfid.openWaitForAttachment(1000)


# Enable Antenna
rfid.setAntennaEnabled(True)
# Sleep 1s
time.sleep(1)
# Write Tag
rfid.write("0x0000000000", RFIDProtocol.PROTOCOL_EM4100, False)
# Sleep 1s    
time.sleep(5)
# Read tag
print(rfid.getLastTag())
# Disable Antenna
rfid.setAntennaEnabled(False)


The first argument given in the write command is the data, followed by the protocol, and last yet most importantly the lock state. You will want to make sure this is set to False, as otherwise the data that is written will be locked to the card permanently and you won't be able to overwrite it.


After writing you should get output that looks like this

('0000000000', 1)

Using Events

Now that we have read and written data, I will show you an easier way to use the 1024_0. It's not practical to constantly loop through the getLastTag() function, as it would hold up your code, so that's where events come in. Events are super useful, once initialized they sit there quietly in the background waiting for something to happen. When they detect an action has occurred, in this case a tag is within range, they will execute whatever code you have within the handler.


The tag handler below has two parameters passed through that are useful. The first is tag, which contains the data on the tag, and the second is the protocol, which will contain the protocol as decoded in the image on step 3.

from Phidget22.Devices.RFID import *
import time

# Tag Handler
def onTag(self, tag, protocol):
    print("Tag: {}".format(str(tag)))

# Init Object
rfid = RFID()

# Add tag handler
rfid.setOnTagHandler(onTag)

# Open Phidget RFID with 1000ms timeout
rfid.openWaitForAttachment(1000)

# Enable Antenna
rfid.setAntennaEnabled(True)

# Loop forever
while True:
    time.sleep(1)


In my example above I only print the tags data. I encourage you to quickly try and print out the protocol as well. You could even use a dictionary to store the references to different protocols and print them out instead of the number.

LED and Power Control

20211114_143706.jpg
20211114_143759.jpg

The RIFD Phidget also has support for interfacing physically with other devices and includes a built-in LED as well. The three screw terminals which are labelled are used to interface with other outside devices. The RFID Phidget can provide up to 5V DC, as well as drive an external LED.


In the example below I have enabled the use of the onboard LED which is on Channel 2. You will need to import the DigialOuput library in order to interface with the LED. I encourage you to try and write modify the code from step six first, but I have provided it below as well.

from Phidget22.Devices.RFID import *
from Phidget22.Devices.DigitalOutput import *
import time


# Tag Handler
def onTag(self, tag, protocol):
    print("Tag: {}".format(str(tag)))
    led.setState(True)
    time.sleep(2)
    led.setState(False)

# Init Object
rfid = RFID()
led = DigitalOutput()

# Add tag handler
rfid.setOnTagHandler(onTag)

# Set RFID Channel and open Phidget RFID with 1000ms timeout
rfid.setChannel(0)
rfid.openWaitForAttachment(1000)

# Set LED Channel and open
led.setChannel(2)
led.openWaitForAttachment(1000)

# Enable Antenna
rfid.setAntennaEnabled(True)

# Loop forever
while True:
    time.sleep(1)


As you can now see when you bring the tag close, it will not only output the data to your screen but will also turn the onboard LED on for 2 seconds.

Conclusion

There is a lot that can be done with the PhidgetRFID Read-Writer. It is a super powerful self-contained device that was a great introduction to working with RFID, and there are lots of projects that can be done with it.


I have included a fully written-out example that utilizes dictionary lookup, as well as the onboard LED so that you can take a look at it. I encourage you to expand and re-write my code, as it is one of the best ways to learn. Thank you for taking the time to read this instructable, have a great day!