User Tools

Site Tools


pico_with_rfid-rcs522

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pico_with_rfid-rcs522 [2026/09/01 21:37] – [Practical Example] walkeradminpico_with_rfid-rcs522 [2026/09/01 21:53] (current) – [Some test Python Scripts] walkeradmin
Line 92: Line 92:
 ---- ----
 ==== Some test Python Scripts ==== ==== Some test Python Scripts ====
 +\\ 
 +<color #ed1c24>*****NOTE THE DRIVER ONLY WORKS WITH 4 BYTE UID TAGS AT THIS TIME, WE ARE LOOKING FOR AN UPDATED DRIVER*****</color>
 +\\ 
 \\  \\ 
 We will start with two scripts. We will start with two scripts.
Line 223: Line 226:
 \\  \\ 
 \\  \\ 
-We are going to create 3 RFID tags.+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 #ed1c24>rfid_read.py</color> 
 +  * <color #ed1c24>rfid_write.py</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. 
 +\\  
 +\\  
 +{{ :pico-non-wireless-pinout-with-leds-traffic-light-order.png?300 |}} 
 +\\  
 +If you used the same GPIO Pins that we used in this example to connect the RFID Reader/Writer, then the pins for the LEDs above will be free. 
 +\\  
 +\\  
 +If you want to plug in the LEDs, then <color #ed1c24>see this wiki page</color> [[traffic_light_leds_using_array|here]] and use the code to test your LEDs (you should see a traffic light sequence). 
 +\\  
 +\\  
 +---- 
 +==== 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. 
 + 
 +<code:python | download> 
 +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,sck=18,miso=16,mosi=19,cs=17,rst=9) 
 + 
 +# Function to turn off all LEDs 
 +def all_off(): 
 +    for light in lights: 
 +        light.off() 
 + 
 +# Read the RFID card/tag/sticker 
 +def read(): 
 +    print("Reading...Please place the card...")  # print a message telling the user to tap the card. 
 +    id, text = reader.read()   # read in two fields from the tag, id and text. 
 +    print("ID: %s\nText: %s" % (id,text))   # print the result in Thonny 
 +#   return id, text 
 +    return text.strip()  # this returns the contents of 'text' to the calling code, but strips off any crap like spaces. 
 +                         # 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' to turn off all LEDs 
 +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 == "RED": 
 +        all_off() 
 +        RED.on() 
 +    elif tag == "AMBER": 
 +        all_off() 
 +        AMBER.on() 
 +    elif tag == "GREEN": 
 +        all_off() 
 +        GREEN.on() 
 +       
 +    time.sleep(1) # The delay is to stop the tag being read multiple times per second. 
 +</code> 
 + 
 +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.1788298679.txt.gz · Last modified: by walkeradmin