Authorized Online Retailers:

Buy from USA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP

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.

  1. Raspberry Pi Pico board and microUSB cable
  2. A computer to run Thonny Python IDE
  3. A breadboard
  4. LED x 2 pc
  5. 220 Ω resistor x 2 pc
  6. Negative enabled Buzzer
  7. OSOYOO RFID reader x 1pc
  8. Some jumper wires

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 .

Learn More about parts SKU:2021005900

No. Picture Product Name Link
1 Raspberry Pi Pico Board https://osoyoo.store/products/raspberry-pi-pico-flexible-microcontroller-board-based-on-the-raspberry-pi-rp2040-dual-core-arm-cortex-m0-processor-1-pc?variant=39875307864175
2 Servo motor https://osoyoo.store/products/micro-servo-sg90-blue-for-arduino-v2-0-robot-carmodel-lacc200610?variant=31648847560815
3 Infrared Sensor Module https://osoyoo.store/products/hc-sr501-pir-motion-sensor-for-arduino?variant=31957812838511
4 RFID Module and card https://osoyoo.store/products/osoyoo-rfid-module?variant=31970646130799
5 Push Buttons and Hats https://osoyoo.store/products/push-button-and-hats-5pcs?variant=39879253131375
6 LED(6 x White, 6 x Red, 6 x Yellow, 6x Green) https://osoyoo.store/products/led-packs-for-arduino?variant=31957816442991
7 Servo motor https://osoyoo.store/products/sg90-micro-servo-for-arduino-raspberry-pi-robot?variant=31957821096047
8 Piezo Buzzer Module https://osoyoo.store/products/piezo-buzzer-module-1pcs?variant=39879251656815
9 I2C LCD Display(16×2) https://osoyoo.store/products/i2c-lcd1602-display?variant=39875437101167
10 Philips Screwdriver https://osoyoo.store/products/phillips-screwdriver?variant=31930635780207https://osoyoo.store/products/single-channel-relay-module-for-arduino-raspberry-pi?variant=31955954401391
11 Solderless Prototype Breadboard https://osoyoo.store/products/solderless-prototype-breadboard-1pcs?variant=39879255457903
12 Potentionmeter (10K adjustable resistor) https://osoyoo.store/products/potentionmeter-10k-adjustable-resistor-3pcs?variant=39879281016943
13 40Pin M to M Jumper Wires https://osoyoo.store/products/40pin-m-to-m-jumper-wires?variant=39879256539247
14 20Pin M to F Jumper wires 15cm https://osoyoo.store/products/20pin-15cm-female-to-female-cable?variant=31930663305327