Radio Frequency Identification (RFID) uses radio to identify and track tags. This PiicoDev RFID module has a radio transducer in it that's able to energize this passive tag wirelessly and interrogate it for its unique ID and also exchange data.
We're going to connect our PiicoDev RFID module to a Raspberry Pi today and we'll run some example code to read the IDs of some tags and even exchange data with them in some mini projects. Let's get started!
To follow along, you'll need a Raspberry Pi. I'm using a Raspberry Pi 4 Model B today and this has already been set up to run like a desktop computer. If you're following along, it's best if you already know how to do this. Check out our Getting Started Workshop if you need help.
You'll of course need a PiicoDev RFID module and an adapter for Raspberry Pi as well as a packet of cable to connect everything together. Have at least one RFID tag to work with. I've got a few here and RFID tags come in all different shapes, sizes, and standards. For best results, we recommend using an NTAG 213 type of tag. I'm also using a Classic or Myfair tag today which will still work with most examples. Check the article for the latest compatibility information.
Connect your PiicoDev adapter to your Pi and on a Raspberry Pi 4, the Ethernet arrow on the adapter will point towards the Ethernet socket. On a Raspberry Pi 3B, this will be the USB connector instead. Connect your Picotf cable to one of the sockets on the adapter and connect the other end to your RFID module. I've just mounted everything to this PiicoDev platform to keep it nice and stable.
Power up your Pi connected to the internet and we'll just do a bit of housekeeping before we can get started. Open the Preferences Raspberry Pi Configuration menu and make sure that we have the I²C interface enabled. That's under the Interfaces tab. Just make sure we have it enabled.
Have it enabled there? Next, open up Thonny IDE from the programming menu and make sure we have the PiicoDev package installed. Go to Tools > Manage Packages and search for PicoDev. Here it is, and just install or upgrade as necessary if you need to upgrade to the latest edition.
Your RFID module has an address switch near the bottom edge labelled ASW. Make sure that both the switches here are in the off position, that's with the switches in the lower position. In the article for this tutorial, find the first example and right-click that Read ID link and save link as. I'm going to save this example to a PiicoDev directory in my home directory.
Back in Thonny, navigate to this directory and open that file. This is a simple example that just reads the ID of an RFID tag. We start by importing the package to drive the RFID module and a sleep function. Next, we call the initialization function to initialize the RFID and we call that object RFID. In the infinite loop, we check if a tag is present and if there is a tag present, we call Read ID which will return a string of that tag's ID. That gets assigned to the ID variable which is then just printed after a short delay. The whole thing just loops again.
Run your program and grab a tag and we can see we're prompted to place a tag near the module. If I bring that in close, we can see a printout of this tag's ID. It's this string of letters and numbers, each pair here is separated by a colon. If I try with another tag, we can see that that should be a separate unique ID. If I try with a classic tag, it's interesting to note that the ID is a shorter string but it still works.
If we want to get more detailed information about our tag, we can comment out this line, the Read ID line, and I'll uncomment the line below it. I'm using Alt+3 and Alt+4 to comment and uncomment. This is exactly the same except the function.
Is now being passed an argument `detail=true`, this means we want more details. I'll run the script with `Ctrl+R` and this time when I scan my first tag, we have something called `success` which is `true`. We have the `id` in an integer format which is very useful if we just want to work with numbers. We also have the `id` in that string format which is separated by colons and we have the `type` which here has been identified as `ntag`.
If I bring in the classic then we can see that the `type` has been identified as `classic` and of course it has the shorter ids associated with it. Now if I read continuously and then remove the tag in the middle of a read, we can see that the `success` flag is `false`. So this is telling us whether a read was complete or not. We can see because we have a `false` here, we know that we don't have any valid data in our `id` string or in our `id` integers. So this can be quite a useful feature to interrogate this `success` flag.
It's quite common to see RFID being used for access control. This is where a door might have a special piece of electronics on it where only authorized users with a special tag, when scanned, can pass through that door. Users without a special tag are denied entry.
This next example is kind of like a starter project for an access control project. Find example 2 and right click that link, save link as. I'll save it in the same place. Back in Thonny, we can open up `access_control.py` and this is pretty similar to the code we were already running. You can see we've got the same setup, there's `if tag present then read id`. The main difference is this little bit of logic down the bottom. Let's just run the script and see what happens. I'm being prompted to hold a tag near the module and we can see I have my tag `id` being printed and access is denied.
In the infinite loop, we're checking if `id` is in authorized users and this
There is a way in Python to check if a string exists in a list. We have an empty list called "authorized users" which currently has an empty string. If we copy the ID from the shell and paste it into "authorized users" and rerun the script, when we scan the tag it says "access granted". If we take an unauthorized tag and scan it, we will still see "access denied". Because "authorized users" is a list, we can add more entries to it. We can take the code for our blue tag, take that ID, and just append or add it to the list with a comma. When we open the quotes, paste that ID, close the quotes, and run the script with ctrl r, our blue tag should work as well.
A really cool feature of these tags is that they can be programmed to be interactive. This means that when you hold them against a smart reader, like a smartphone, there can be special code on the tag that instructs the reader to perform some action. This could be opening a web address or composing an email to a specific recipient, or even a geographical location provided by some coordinates.
Let's take it for a spin. We can find the next example, the right URI example, right click and save as. In the infinite loop, after we check if a tag is present, we're using the right URI function. This is the function that will load that special link onto the tag, with special information about web addresses or email addresses. We're writing a variable called "web address" which is currently defined along with a few other URIs. The URI scheme for a web address is "https" or "http". We've got a geolocation here and the URI scheme for that is "geo mail 2".
We can be used for email addresses and tel is being used for phone numbers. This is currently set up to write this web address to the tag, so let's take it for a spin. I'll run the code and scan my tag. The write was successful, so I'll get out my phone and hold the tag to the back of the phone. Immediately, we're opening up a web page and this looks like it is the just a collection of PiicoDev guides. How good is that? Let's try writing this geo location. I'll copy that variable, paste it into write URI, rerun the script, use the same tag. The write is successful. Now, when I hold the tag to the back of my phone, we immediately jump into a menu to choose what app we want to open. I'll open maps and we have jumped to that exact coordinate. How nice!
There are a bunch of other URIs that you can write to a tag. Full link is in the article. So, the last example was all about using special data with a special reader, but what if we just want to write our own data to the tag and then read it back using this RFID module? Well, it turns out we can write text and numbers to our tags. Find the next example which is all about text and save that link. Open up the write text example in funny and you can see here we're going to write the string "Hello World" to the tag using the write text function. I'll run the script, use the tag, and we can see that the text in the tag is "Hello World". So, we can actually write uppercase letters, lowercase letters, and some ASCII characters as well and there's no funny business going on here. The text that's showing up on the screen is actually coming from the read text function. So, we're calling read text, assigning that to data, and then just printing data. You can store up to 143 characters of text on one of these.
So, that was text, but of course we can write numbers as well. Find the write numbers example, right click and save that link, open up in funny.
Funny and of course it looks pretty similar. This time we're just using the right number function. We're going to write 123 456 and this second argument is the location to store that number. Let's run the script, hold our tag and we can see the number has been read from the tag. We have a read back here where we're reading into data and then printing data.
So what's up with this slot argument here? Well, slots are what we're calling safe places to read and write data on a tag. There are loads of other locations on a tag where you can read and write data, but if you do it wrong it can actually break your tag. There are 36 slots numbered 0 to 35 and they can all hold some pretty large integers, so you can do quite a lot with them.
Finally, we're going to work with multiple RFID modules. It's actually possible to connect up to four PiicoDev RFID modules on the same PiicoDev bus. The only requirement is that they all have unique address switch positions. You might recall at the start of the tutorial we set the address switch on our first unit to be both off. For my second RFID module, I'm just turning on ASW1, that's the first switch on the left.
Return to the article and find the multiple readers example. Save that link and open in Thony. This example is basically the same as the very first example that we did looking at IDs. This time we are able to do it with two modules. The difference is in how we initialize the modules. Now when we initialize the first module, we're initializing it with the argument ASW equals and then a list of integers. These integers are the positions of the ASW switches. Zero means off and one means on. So for our first module we have both switches off (0, 0) and for the second module we have the first switch on and the second switch off (1, 0).
We get to call these RFID readers anything we like, so I'm calling them reader.
A and Reader B to distinguish them. In the infinite loop, we check if a tag is present on Reader A and read its ID. We then check if a tag is present on Reader B and read its ID as well. If we run the script and take our tag and hold it to Reader A, we should see RFID A and the ID of the blue tag. If we hold it to the second reader, we get RFID B reading the same ID. We are able to work with two readers independently.
For more advanced users that would rather just work with I2C addresses, there is a table to decode the I2C address from the switch position on the back. We can see that our first module with off off would be using address 2C and our second module with the switches on off would be using address 2D. We can just enter those in under the address argument instead and it will work just the same.
There you have it: a bunch of starter projects and examples for getting started with the PiicoDev RFID module. We did basic ID reading and then did a basic access control project so we could differentiate between tags. We even made interactive tags that can take us to specific web pages or locations on the planet. You could use the data storage examples if you wanted to make your own vending machine and keep some kind of balance on the tag with how much credit the user has.
If you make something cool from these projects, we'd love to hear from you on the Core Electronics forums or if you just have some questions, let us know. We're full-time makers and happy to help. Until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.