Buy from USA | Buy from UK | Buy from DE | Buy from IT | Buy from FR | Buy from ES | ここでご購入を! |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
RFID reader is a very popular device in security system. It can read the ID of an RFID card and send it to computer system.
In this lesson, we will use OSOYOO RFID reader to make a simple security verify system. This RFID will use a new communication protocol SPI to send data to Pico. So after this lesson, you have learned three kind of communication protocols: SPI, I2C and Serial (RS232). The speed comparison among these three method are: SPI > I2C > Serial.
As we will use many pins in this project, we put the Pico pin map here to help user find the the proper pin location.
In above circuit graph, you can see that:
Pico Pins | RFID Pins |
GP5 | SDA |
GP6 | SCK |
GP7 | MOSI |
GP4 | MISO |
IRQ | |
GND | GND |
GP18 | RST |
3.3V | 3.3V |
Pico Pins | Devices |
GP14 | Red LED through 220ohm resistor |
GP15 | Green LED through 220ohm resistor |
GP16 | Buzzer I/O pin |
You can download lesson 7 python code fromhttps://osoyoo.com/driver/pico/lesson7/pico-lesson7.zip
You can use Thonny to open pico-lesson7.py and load it to Pico later.
Here is the full code with comments:
import time from machine import I2C, Pin, SPI #import I2C,Pin,SPI library from mfrc522 import MFRC522 #import RFID reader library buzzer= Pin(16, Pin.OUT) #set buzzer to GP16 buzzer.value(1) true = Pin(15, Pin.OUT) #set Green LED to GP15 false = Pin(14, Pin.OUT) #set Red LED to GP14 sck = Pin(6, Pin.OUT) #set RFID sck to GP6 mosi = Pin(7, Pin.OUT) #set RFID mosi to GP7 miso = Pin(4, Pin.OUT) #set RFID miso to GP4 sda = Pin(5, Pin.OUT) #set RFID sda to GP5 rst = Pin(18, Pin.OUT) #set RFID rst to GP18 spi = SPI(0, baudrate=100000, polarity=0, phase=0, sck=sck, mosi=mosi, miso=miso) #initial SPI card1 = "0xe58a6223" #change this value to match your testing RFID card 1 card2 = "0xf765bd60" #change this value to match your testing RFID card 2 while True: rdr = MFRC522(spi, sda, rst) #initialize reader (stat, tag_type) = rdr.request(rdr.REQIDL) #read card ud if stat == rdr.OK: (stat, raw_uid) = rdr.anticoll() if stat == rdr.OK: uid = ("0x%02x%02x%02x%02x" % (raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3])) print(uid) if uid == card1: #if ID matches card 1, buzzer beep once, turn on Green LED print("card 1 detected!") buzzer.value(0) time.sleep(0.3) buzzer.value(1) true.value(1) time.sleep(1) true.value(0) time.sleep(1) elif uid == card2: print("card 2 detected!") #if ID matches card 2, buzzer beep twice, turn on Green LED buzzer.value(0) time.sleep(0.3) buzzer.value(1) time.sleep(0.3) buzzer.value(0) time.sleep(0.3) buzzer.value(1) true.value(1) time.sleep(1) true.value(0) time.sleep(1) else: #if ID doesn't match any card, long beep, turn on Red LED print("invalid card!") buzzer.value(0) time.sleep(2) buzzer.value(1) false.value(1) time.sleep(0.1) false.value(0) time.sleep(0.1) false.value(1) time.sleep(0.1) false.value(0) time.sleep(0.1) false.value(1) time.sleep(0.1) false.value(0) time.sleep(1)
Step 1: Connect Pico board to one of USB ports in your PC
Step 2: If you haven’t installed Thonny Software or don’t know how to use Thonny IDE, please read lesson 1
Step 3: Now open the Thonny Python IDE, click Run to select MicroPython for Raspberry Pi Pico as Interpreter :
Also please select the COM port which your Pico board is connected :
After that, click OK to save the setting.
Step 4: Now download the code from https://osoyoo.com/driver/pico/lesson7/pico-lesson7.zip , save it to your local PC, then unzip the file. You will find two files mfrc522.py and pico-lesson7.py. Use Thonny to open the mfrc522.py Library file
Step 5 Then File -> Save as
Step 6 Then select Raspberry Pi Pico as destination.
Step 7 ,Write the file name :mfrc522.py.After that, click OK to save the setting.
Then Use Thonny to open another file pico-lesson7.py ,click the little ► button to run the Python code.
You need to prepare 3 different RFID cards. Touch each card on the reader, you will see its ID like 0xd71d9a52 and “invalid card ” in the Shell window. Also red LED is turned on and buzzer has a long beep. This is because that your current card IDs don’t match any ID in line 14,15. Please change the line 14,15 to the ID of your card 1 and card 2 ID, then click ► button to run again.
Now you will see that card 1 will trig message “card 1 detected”, 1 short beep and green light.
When you put card 2 to reader, Shell window will show “card 2 detected”, 2 short beep and green light .
After the operation is complete, press Ctrl+C to end the command.
DownLoad Url osoyoo.com