Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active September 12, 2022 17:32
Show Gist options
  • Save IdrisCytron/0c3afadc8d6959e56a011fb7e7d52609 to your computer and use it in GitHub Desktop.
Save IdrisCytron/0c3afadc8d6959e56a011fb7e7d52609 to your computer and use it in GitHub Desktop.
Read and write RFid data using RC522 on Raspberry Pi.
#!/usr/bin/env python
import RPi.GPIO as GPIO
from gpiozero import Button, Buzzer
from time import sleep
from mfrc522 import SimpleMFRC522
import sys
GPIO.setwarnings(False)
sw1 = Button(21)
sw2 = Button(16)
sw3 = Button(20)
buzzer = Buzzer(26)
reader = SimpleMFRC522()
buzzer.beep(0.1, 0.1, 2)
try:
while True:
if sw1.is_pressed:
print("Hold a tag near the reader")
id, text = reader.read()
buzzer.beep(0.1, 0.1, 1)
print("ID: %s\nText: %s" % (id,text))
print()
elif sw2.is_pressed:
text = input('New data:')
print("Now place your tag to write")
reader.write(text)
buzzer.beep(0.1, 0.1, 1)
print("Written")
print()
except KeyboardInterrupt:
GPIO.cleanup()
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment