其他分享
首页 > 其他分享> > 树莓派上使用RC522

树莓派上使用RC522

作者:互联网

本文全部参考快递盒+Python制作的门禁系统 | 树莓派ID卡读取与写入 | RFID RC522 | Raspberry Pi RFID RC522_哔哩哔哩_bilibiliID卡模块型号: RFID RC522树莓派型号: Raspberry Pi 3B +最终代码:https://github.com/Hubert-HuJianhua/RPI_Access_control_system_mRFC522命令顺序:sudo raspi-configsudo apt-get install python3-dev python3-pipsudo pip3 installhttps://www.bilibili.com/video/BV1tf4y1x7om?spm_id_from=333.999.0.0

一、使能SPI与I2C

接线:图片来自基于RFID RC522模块制作树莓派通读卡器 – 八色木RC522 RFID模块在单片机开发中常用来作为读卡器使用,在本文中我们把它连接到树莓派,然后就可以制作读卡器了,比如读取门禁卡的UID。本项目只是一个读卡器的雏形,如果需要运用到实际项目中,还需要你的更 …icon-default.png?t=M276https://www.basemu.com/rc522-rfid-tag-reading-with-the-raspberry-pi-1.html

 

二、安装mfrc522

sudo pip3 install mfrc522

三、输入

from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
    text = input('New data:')
    print("write")
    reader.write(text)
    print("success")
finally:
    GPIO.cleanup()

然后在界面输入随便的数字

四、验证

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()


while True:
    id, text = reader.read()
    print(id, text)

五、RC522 控制舵机

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import time
import re
from threading import Thread

rfid = 0
servoPIN = 17  #BCM 17

GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50)
p.start(2.5)
reader = SimpleMFRC522()

def data():
    global rfid
    while True:
        id, text = reader.read()
        clean_text = re.findall('\d+', text)
        match = int(clean_text[0])
        rfid = match
def lock():
    global rfid
    while True:
        print("rfid = ", rfid)
        if rfid == 1234 :
            p.ChangeDutyCycle(2.5)
            time.sleep(3)
            rfid = 0
        else:
            p.ChangeDutyCycle(6)
            time.sleep(0.5)


try:

    t1= Thread(target=data)
    t2 = Thread(target=lock)
    t1.start()
    t2.start()


except:
    p.stop()
    GPIO.cleanup()

标签:树莓,text,rfid,RC522,使用,GPIO,import,SimpleMFRC522
来源: https://blog.csdn.net/qq_44941069/article/details/123590351