My code stack after some time (example 1 day) after reading MFRC522 in loop mode. At the beginning it works without problems, after some time is not working anymore. I can't figure out why. Can you help me?

#!/usr/bin/env python

import os
import time
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import array as arr

pinList = [5,24,0,2,3,4,27,25]
rfidList = [1081714680121,871849641659,504025229752,275844158012,648722309022,118668796333,737282492797]
id  = 0

while True:
    reader = SimpleMFRC522()
    id, text = reader.read()
    if id:
        if id == 828191200458:
            for i in pinList:
                os.system("sudo gpio mode "+str(i)+" out")

            for i in pinList:
                os.system("sudo gpio mode "+str(i)+" in")

        for i in range(len(rfidList)):
            if id == rfidList[i]:
                os.system("sudo gpio mode "+str(pinList[i])+" out")
            os.system("sudo gpio mode "+str(pinList[i])+" in")

Thank You!

The link of MFRC522 is HERE

  • 1
    How exactly is it "not working"? Does it crash? does it get stuck? Where? Commented Nov 6, 2019 at 18:54
  • 1
    The code works great for 1 day. Reads every card, really, but after 1 day or after 5 hours, i don't know exactly when, stops reading without error or anything else. It simply stops reading, if i reboot the raspberry get back to work again, i can't figure out why
    – Simon D.
    Commented Nov 6, 2019 at 20:23

1 Answer 1


At a guess it is because the code runs out of memory.

The code reader = SimpleMFRC522() creates a new object every time around the loop. I suspect the old copy is hanging around in memory and not being garbage collected.

That line should be before the while loop.

Also the call to GPIO.cleanup() is almost certainly wrong and should probably be removed.

  • If i put outside the reader = SimpleMFRC522(), after 30 minutes it stops reading. The GPIO.cleanup() is for refresh. It's wrong?
    – Simon D.
    Commented Nov 6, 2019 at 20:19
  • The whole structure seems suspect to me. However to find out I would need to look at SimpleMFRC522 which I'm not going to do (and you haven't provided a link to it anyway).
    – joan
    Commented Nov 6, 2019 at 20:23
  • The SimpleMFRC522 is the classic SimpleMFRC522 [here] (github.com/pimylifeup/MFRC522-python/blob/master/mfrc522/…) but if you're not going to watch it, i can just thank you for the reply and the interest.
    – Simon D.
    Commented Nov 6, 2019 at 20:28
  • You should really add that information to your question.
    – joan
    Commented Nov 6, 2019 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.