|Buy from USA||Buy from UK||Buy from DE||Buy from IT||Buy from FR||Buy from ES||ここでご購入を！|
An RFID reader is a very popular device in security systems. It can read the ID of an RFID card and send it to a computer system.
In this lesson, we will use the OSOYOO RFID reader to create a simple security verification system. This RFID reader will use a new communication protocol called SPI to send data to the Pico. After completing this lesson, you will have learned about three types of communication protocols: SPI, I2C, and Serial (RS232). The speed comparison among these three methods is as follows: SPI > I2C > Serial.
Since we will be using many pins in this project, we have included the Pico pin map here to help users locate the proper pins.
In above circuit graph, you can see that:
|Pico Pins||RFID Pins|
|GP14||Red LED through 220ohm resistor|
|GP15||Green LED through 220ohm resistor|
|GP16||Buzzer I/O pin|
You can download lesson 7 Python code from https://osoyoo.com/driver/pico/lesson7/pico-lesson7.zip.
Later, you can use Thonny to open pico-lesson7.py and load it to Pico.
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, raw_uid, raw_uid, raw_uid)) 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 the Pico board to one of the USB ports on 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 and click “Run” to select MicroPython for Raspberry Pi Pico as the interpreter:
Also, please select the COM port to which your Pico board is connected:
After that, click “OK” to save the settings.
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 click “File” -> “Save as”.
Step 6: Then select “Raspberry Pi Pico” as the destination.
Step 7: Write the file name as “mfrc522.py”. After that, click “OK” to save the settings.
Then use Thonny to open another file, pico-lesson7.py, click the little ► button to run the Python code.
You need to prepare three different RFID cards. Touch each card on the reader, and you will see its ID, like “0xd71d9a52” and “invalid card”, in the shell window. Also, the red LED will turn on, and the buzzer will have a long beep. This is because your current card IDs don’t match any IDs in lines 14 and 15. Please change lines 14 and 15 to the IDs of your card 1 and card 2, then click the ► button to run again.
Now you will see that when you put card 1 on the reader, it will trigger the message “card 1 detected”, one short beep, and a green light. When you put card 2 on the reader, the shell window will show “card 2 detected”, two short beeps, and a green light.
After the operation is complete, press Ctrl+C to end the command.
|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|
|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|
|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|
|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|
DownLoad Url osoyoo.com