print("script starting")
from time import sleep, sleep_ms
from machine import Pin, SPI, PWM
from mfrc522 import MFRC522
sck = Pin(18)
mosi = Pin(23)
miso = Pin(19)
spi = SPI(
baudrate=100000,
polarity=0,
phase=0,
sck=sck,
mosi=mosi,
miso=miso
)
CS_PIN = 5
rdr = MFRC522(spi, CS_PIN)
print("reader initialized")
servo = PWM(Pin(32), freq=50)
def servo_angle(angle):
duty = int(40 + (angle / 180) * (115 - 40))
servo.duty(duty)
def gate_open():
servo_angle(90)
def gate_close():
servo_angle(0)
gate_close()
def uid_to_string(uid_bytes):
return "".join("{:02X}".format(b) for b in uid_bytes)
print("scan an RFID tag...")
while True:
stat, tag_type = rdr.request(rdr.REQIDL)
if stat == rdr.OK:
stat, raw_uid = rdr.anticoll()
if stat == rdr.OK:
uid_str = uid_to_string(raw_uid[:4])
print("card detected, UID:", uid_str)
print("opening gate")
gate_open()
sleep(3)
print("closing gate")
gate_close()
sleep_ms(1000)
elif stat != rdr.OK:
print("no card detected UID:", uid_str)
sleep_ms(50)Loading
mfrc522
mfrc522