Radio Frequency IDentification (RFID) uses electromagnetic fields to identify and communicate with tags. Each RFID module emits radio waves that energise the passive tag, which then responds with its own identifying number or data, as requested.
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.
To follow along, it's best to have:
- A MicroBit V2
- A PiicoDev RFID Module
- A PiicoDev Adapter for MicroBit
- At least one RFID Tag. We recommend NTAG213 (or NTAG203) tags for best results. Classic (aka. "Mifare") tags also work, though with limited features.
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
Contents
- Connect the RFID Module to your micro:bit
- Download MicroPython Modules
- Example 1: Read Tag ID and Type
- Example 2: Access Control (Starter Project)
- Example 3: Using Multiple RFID Modules
- Conclusion
- Resources
Connect the RFID Module to your micro:bit
Plug your micro:bit into the Adapter (with buttons facing up), connect your RFID Module to the Adapter with the PiicoDev cable, and finally connect your micro:bit to your computer with a USB lead.
On the RFID Module - make sure both Address Switches (ASW) are in the OFF position. The ASW switch may be covered by a thin, orange film - carefully peel this away if necessary.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download MicroPython modules
We will need three files to easily read data from the Magnetometer:
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_RFID.py (right-click, "save link as")
- Download the example script: read_id.py (right-click, "save link as" and save as "main.py")
It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev
Example 1 - Read Tag ID and Type
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
Open Thonny, connect to your micro:bit and navigate to where you stored the files from the last step. Right-click each file and select "Upload to micro:bit" to upload them to your micro;bit (Hint: View the files menu with View > Files)
Open main.py and run the script (click the Green Play Button, or Ctrl+R). You will be prompted to hold a tag near the RFID Module - do so and you ought to see a string of characters display in the Shell - this is the unique ID of your tag. If you have other compatible tags handy, try reading their IDs and observe how they are different.
Above: Three different tags are held against the sensor - one after the other. Their unique IDs are printed into the shell. The first two tags are NTAG213, and the third is a 'Classic' which has a shorter ID string.
Remix - Tag Type (Detailed Read)
Example 2 - Access Control (Starter Project)
RFID is often used for Access Control eg. permitting authorised personnel into a building. The following program simulates this behaviour. Since we can identify unique tags, we can keep a list of authorised tags. When our program sees an authorised tag it can perform one behaviour ("Access Granted"), and perform a different behaviour ("Access Denied") if the tag is not recognised.
Copy the following code into main.py.
from PiicoDev_RFID import PiicoDev_RFID from PiicoDev_Unified import sleep_ms import music rfid = PiicoDev_RFID(suppress_warnings=True) print('Place tag near the PiicoDev RFID Module') print('') # A list of authorised users. Add your tag ID as a string to this list. eg: authorised_users = ['##:##:##:##:##:##:##'] authorised_users = [''] while True: if rfid.tagPresent(): # if an RFID tag is present id = rfid.readID() # get the id print(id) # print the id if id in authorised_users: # check if the tag is in the authorised-user list print('Access Granted!\n') music.play(music.BA_DING) # Your code here for authorised users else: print('*** ACCESS DENIED ***\n') sadTune = ["D#","C"] # a descending tone music.play(sadTune) # Your code here for unauthorised users sleep_ms(1000) sleep_ms(10)
Run the script and hold your tag to the RFID Module. Your tag is not yet recognised so the program prints "ACCESS DENIED".
Now try copying your tag's ID string from the shell, and pasting it into the authorised_users list. Re-run the program and try scanning your tag again - you ought to see "Access Granted!"
Example 3: Using Multiple RFID Modules
It's possible to connect up to four RFID Modules on a PiicoDev bus - they will each need unique Address Switch (ASW) settings. Until now we've been working with one module with both address switches in the OFF position. To add a second module, set its ASW1 to the ON position. Now, when we initialise both sensors, we can explicitly include the ASW settings in the initialisation function using the asw argument. The asw argument is just a list of the switch positions (in ascending order) where 1 is ON and 0 is OFF. Note: the ASW switch positions are only read on powerup - if you change the ASW switch positions while a module is powered, you will need to remove and reconnect power to the module for the new address to be used.
Above: The left panel shows the procedure for changing the second module's address. (Left) Address Switch (ASW) in the default state, with both switches OFF. (Middle) Use a fine implement like tweezers or a toothpick to set ASW1 to the ON position. (Right) With the new ASW configuration, the module should be initialised with asw=[1,0]
The following example initialises two RFID Modules (with ASW [0,0] and ASW [1,0]) and reads tag IDs from them independently.
# Read from two RFID modules independently from PiicoDev_RFID import PiicoDev_RFID from PiicoDev_Unified import sleep_ms readerA = PiicoDev_RFID(asw=[0,0], suppress_warnings=True) # Initialise the first RFID module with both address switches OFF readerB = PiicoDev_RFID(asw=[1,0], suppress_warnings=True) # Initialise the second RFID module with the first address switch ON print('Place tag near one of the PiicoDev RFID Modules') print('') while True: if readerA.tagPresent(): # if an RFID tag is present on reader A id = readerA.readID() # get the id print('RFID A: ' + id) # print the id on the left side of the shell if readerB.tagPresent(): # if an RFID tag is present on reader B id = readerB.readID() # get the id print(30*' ' + 'RFID B: ' + id) # print the id on the right side of the shell sleep_ms(500)
Conclusion
We've used a PiicoDev RFID Module to read the unique ID from a compatible tag, and completed a simple starter project to perform different actions depending on what tag is scanned (access control). We've completed further examples to read and write user-data to tags including numbers, text, and even interactive URIs. By setting unique addresses using the Address Switch (ASW), we can connect up to four RFID Modules to the same PiicoDev bus.