In this tutorial, we'll get started with using a PiicoDev® RFID module by making some starter projects. Once we're done, you'll be able to read a tag's ID number, and determine whether the tag belongs to an authorised user or not.

Transcript

Radio Frequency Identification (RFID) uses electromagnetic waves to identify and track tags. This RFID module has a radio transponder in it that powers this passive tag remotely and interrogates it for its unique ID. I'm going to show you how to use the PiicoDev RFID module so you can read the identities of tags and get started with your own RFID projects. Let's get started!

To follow along, you'll need a MicroBit V2, a PiicoDev Adapter for MicroBit and RFID module, and a PiicoDev cable to connect everything together. To follow along with this guide, it's best to have some NTAG 213 tags, though it'll also work with Mifare or Classic tags. Check the article for the latest compatibility information. Plug your MicroBit into the PiicoDev Adapter and connect the cable to one of the PiicoDev sockets. Connect the other end to your RFID module. The RFID module has a group of switches labelled ASW. To follow along with this tutorial, make sure both these switches are in the off position (that's where the switches are in the lower position) and I've just mounted everything to this PDF platform to keep it nice and stable.

Next, connect to your computer with a USB cable. We're going to need some files to get started. Find the downloads section and right click each link and select "Save Link As". I'm going to save these to a PiicoDev directory in my documents. This last file, "read_id", I'm going to rename that when I save it to "main.py". We'll be programming in Python for this tutorial. If you haven't used Python and the MicroBit before, check out our guide for getting started there. Or, if you prefer to use an online editor, check out our guide for using python.MicroBit.org to program a Micro Bit.
 
Open Python, connect to your Micro Bit, and navigate to where you saved those three files. We'll upload all of them to the Micro Bit. Click the first and hold down shift, then click the last. That's selected all the files then right click and upload to micro bit. There's our files on the MicroBit and I'll double click main dot py to open it. This is an example that reads the ID of a tag. Let's take a look. We start by importing the functionality for the RFID module and also a sleep function. Then we call the initialization function and we call our RFID module RFID.

Then in the infinite loop we check if a tag is present and if a tag is present we call read ID which returns a string of that tag's ID that gets assigned to the ID variable which we just print. Let's check it out. I'll press ctrl R to run the script. We get a lot of text here but the next thing is place the tag near the RFID module. So I'll take one of my tags and hold it nearby and sure enough in the shell we get a printout of this tag's ID. This is the tag's ID string. If I take another tag and hold it up then sure enough that ID is different to the first.

Now with the classic tag you should see that this has a shorter ID than the NTAG 213. Returning to this big block of text that came out at the start, this is just telling us that we're using a simplified version of the RFID library and to hide this big block of text so that it doesn't appear every time you run the script we can just initialize with this line of code instead. So I'll copy that and replace my initialization. Now when I run the script again it just runs silently and we jump straight to the prompt to hold a RFID tag near the reader.

We can also read a few more details from our tag. I'll comment out the first read line with alt 3 and I'll uncomment the next read line. This is basically the same piece of code we're just calling the function with an argument detail equals true. Now when we run the script and I hold my tag up to the reader this is the output of one of those reads and there's a fair bit more. Going on here, we can see the type of the tag has been identified. We have the formatted ID, this is the ID that we were seeing before. We have success is true, that means that the read was successful. Then there's another version of the ID where the ID is represented just as integers instead of a string of letters and numbers. This second read here, there's no data present and that's because I pulled the tag away before the read was complete. So we can see there's no type, there's no ID, but here we have the success flag as false. So this is telling us that that was a failed read because I moved the tag too quickly for it to be read. That can be quite useful.

Now for another example, RFID is often used for access control. You may have seen this on certain buildings where you can scan a tag and the door will open for you or not if you're not an authorized user. We're going to recreate that same experience. Jump to the access control starter project in the article and copy all that code and I'll just paste it into the main.py file on our MicroBit. Let's run the script and scan a tag. Interesting, we heard that sad tone just now which means that access was denied and we can see that in the shell with access denied. This is the ID of the tag that I just scanned.

Let's take a look at the code. We have the same imports as before but this time we're importing music so we can play some tones on the MicroBit. We initialize with those warnings suppressed and we have something here called authorized users, more on that later. In the infinite loop, we check if a tag is present, we've done that before. If a tag is present, we read that ID and assign it to the ID variable and then we do a check if ID is in authorized users. That's the condition where access is granted, but authorized users is an empty list. Let's try copying our ID string into authorized users and re-running. I'll run the script with Ctrl+R. Now, when I scan my tag, how nice when we get to this if statement. Now, the ID of this tag is defined in the authorized user list and so we fall into the access granted section and we play a much happier tone. If I take another tag and hold that to the reader, this tag is not in the authorized users list and so access is denied.

If you want to add more tags as authorized users, just copy that ID and add it to the list. So, we'll need a comma and then a quote mark and we'll paste in that ID and we'll close the quotes. So now we have two authorized users in that list, so both these tags should work. Here's my first tag, here's my second tag and of course the third one hasn't been defined, denied. This is just one example of how you can use MicroPython to differentiate between different tags. Now, it's possible to have up to four RFID modules all connected on the same PiicoDev bus. For this to work, they all need to have unique address switch settings. That's the ASW switch that we saw at the start of the tutorial. Here's my first module with both switches off. To include my second module, it needs to have a unique address switch setting and so I've just switched the ASW1 switch into the on position. Now we have two modules each with a unique switch combination. Find the example for using multiple RFID modules and copy all that code into main.

Now, in the setup for the code, we're initializing two separate RFID modules and so we have two different calls to PiicoDev RFID, one for reader A and one for reader B. What differentiates these modules is the ASW switch settings which we defined using this argument ASW. ASW is a list of zeros or ones that indicate the positions of the switches. For our first module, where both switches are off, we have zero zero because each are off. For the second module, the first ASW switch is on.

We put a one in that position, now that the readers are initialized we can refer to them as Reader A and Reader B (or whatever other name you want to use). The rest of this example reads the IDs from each module. If there's a tag present at Reader A, then we read the ID. If there's a tag present at Reader B, then we read that ID. If we run this script and hold our tag to Reader A, then we get a printout for RFID A and then the ID. If we hold our tag to the other reader, we get a printout for RFID B. To make it a little easier to read, we have RFID A always printing on the left and RFID B always printing on the right. In that print statement, there's just 30 leading spaces being printed before that string.

So there you have it; reading the unique IDs from RFID tags and a simple starter project for doing some access control. If you make something cool from these starter projects or if you have some questions, let us know in a comment for this article. We're full-time makers and happy to help. Until next time, catch you later!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.