pico_with_rfid-rcs522
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| pico_with_rfid-rcs522 [2026/08/26 12:27] – [Using the Read / Write Python Code] walkeradmin | pico_with_rfid-rcs522 [2026/09/01 21:53] (current) – [Some test Python Scripts] walkeradmin | ||
|---|---|---|---|
| Line 89: | Line 89: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| + | {{ : | ||
| ---- | ---- | ||
| ==== Some test Python Scripts ==== | ==== Some test Python Scripts ==== | ||
| + | \\ | ||
| + | <color # | ||
| + | \\ | ||
| \\ | \\ | ||
| We will start with two scripts. | We will start with two scripts. | ||
| Line 110: | Line 114: | ||
| In this / | In this / | ||
| - | * __init__.py | + | * _init__.py |
| * mfrc522.py | * mfrc522.py | ||
| * simple_mfrc522.py | * simple_mfrc522.py | ||
| Line 154: | Line 158: | ||
| **Typical Capacities by Memory Type** | **Typical Capacities by Memory Type** | ||
| - | * Basic / Low-Memory Tags: 64 to 128 bits. These store a basic serial number or electronic product code (EPC) that links to an external database. | + | |
| - | * Medium-Memory Tags: 512 bits to 2 kilobytes (KB). These store extra item details, user data, or small web links (such as NFC NTAG chips with 144 to 888 bytes of user memory). | + | |
| - | * High-Memory Tags: Up to 8 KB or more. Specialized industries like aerospace use these to store complete repair and parts histories directly on the item. | + | |
| Remember: | Remember: | ||
| Line 211: | Line 215: | ||
| \\ | \\ | ||
| Once running, tap your RFID card and you should see the message that you typed in during the rfid_write.py process. | Once running, tap your RFID card and you should see the message that you typed in during the rfid_write.py process. | ||
| + | \\ | ||
| + | \\ | ||
| + | At some point in the future we will come back to this and add some code to allow us to make real world changes when RFID tags are scanned, maybe turn on/off an LED or display an RFID tag ID: on an LCD screen. | ||
| \\ | \\ | ||
| \\ | \\ | ||
| ---- | ---- | ||
| + | ==== Practical Example ==== | ||
| + | \\ | ||
| + | This is a good example to show what you can do reading RFID tags. Now, I want to make something very, very clear. RFID tags have Unique IDs (UID) and you should be using this UID to ensure that only tags you have authorised can be used on your system. For this example, we are not using UIDs, because it's just a simple example of using an RFID tag to do something. | ||
| + | \\ | ||
| + | \\ | ||
| + | I am assuming you have read this entire article, if you have not, you will probably have a lot of issues, so I suggest you have a good read to understand the RFID Python scripts. | ||
| + | \\ | ||
| + | \\ | ||
| + | We are going to create 3 RFID tags. If you look in the previous section above, you can see we have used two Python scripts. | ||
| + | * <color # | ||
| + | * <color # | ||
| + | |||
| + | Initially, we are going to use the rfid_write.py. Using this Python script, create three RFID tags, RED, AMBER and GREEN. | ||
| + | \\ | ||
| + | \\ | ||
| + | What we are going to do is to turn on some LEDs depending on which RFID Tag is scanned. | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== The Hardware ==== | ||
| + | \\ | ||
| + | Connect three LEDs, as shown in the diagram below. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| + | \\ | ||
| + | If you used the same GPIO Pins that we used in this example to connect the RFID Reader/ | ||
| + | \\ | ||
| + | \\ | ||
| + | If you want to plug in the LEDs, then <color # | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
| + | ==== The Python Code ==== | ||
| + | \\ | ||
| + | Below is the Python code we will use for this example, it's an expanded version of the rfid_read.py that we used earlier. | ||
| + | \\ | ||
| + | \\ | ||
| + | What the code does is very simple. | ||
| + | |||
| + | Reads the tag. | ||
| + | Gets the value RED, AMBER or GREEN. | ||
| + | Uses an if, elseif loop to turn on the corresponding LED (and turn the other off). | ||
| + | |||
| + | Here is the code, save it to your Pico as rfidled.py. | ||
| + | |||
| + | < | ||
| + | from mfrc522 import SimpleMFRC522 | ||
| + | import time | ||
| + | from machine import Pin | ||
| + | |||
| + | # Define GPIO pins for the LEDs | ||
| + | RED = Pin(2, Pin.OUT) | ||
| + | AMBER = Pin(6, Pin.OUT) | ||
| + | GREEN = Pin(10, Pin.OUT) | ||
| + | |||
| + | # Put them in an array | ||
| + | lights = [RED, AMBER, GREEN] | ||
| + | |||
| + | # Set the GPIO pins being used | ||
| + | reader = SimpleMFRC522(spi_id=0, | ||
| + | |||
| + | # Function to turn off all LEDs | ||
| + | def all_off(): | ||
| + | for light in lights: | ||
| + | light.off() | ||
| + | |||
| + | # Read the RFID card/ | ||
| + | def read(): | ||
| + | print(" | ||
| + | id, text = reader.read() | ||
| + | print(" | ||
| + | # | ||
| + | return text.strip() | ||
| + | # Even if the tag is short (red) you still get 16 chars back from the read, so we need to strip of what is not required. | ||
| + | |||
| + | |||
| + | # Call function ' | ||
| + | all_off() | ||
| + | |||
| + | # Start a loop (forever) | ||
| + | while True: | ||
| + | tag = read() #c all the function def read() and store the result in the varable tag | ||
| + | print ("the scanned colour is =", tag) #print the contents of the variable tag | ||
| + | | ||
| + | # Ched the read tag colour, turn off all LEDs then turn on the corresponding one. If no tags match, nothing happens. | ||
| + | if tag == " | ||
| + | all_off() | ||
| + | RED.on() | ||
| + | elif tag == " | ||
| + | all_off() | ||
| + | AMBER.on() | ||
| + | elif tag == " | ||
| + | all_off() | ||
| + | GREEN.on() | ||
| + | | ||
| + | time.sleep(1) # The delay is to stop the tag being read multiple times per second. | ||
| + | </ | ||
| + | |||
| + | Run the code, if you have wired your LEDs correctly, and programmed your tags, each LED will come on with each Tag scan. | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
pico_with_rfid-rcs522.1787747274.txt.gz · Last modified: by walkeradmin
