0

I wanted to connect 2 RC522 to my Raspberry Pi. I Followed the instructions from here. But then I got the error message:

/usr/local/lib/python3.9/dist-packages/mfrc522/MFRC522.py:151: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(pin_rst, GPIO.OUT)
Traceback (most recent call last):
  File "/home/pi/Desktop/rfid.py", line 63, in <module>
    data = nfc.read("reader1")
  File "/home/pi/Desktop/rfid.py", line 39, in read
    if not self.selectBoard(rid):
  File "/home/pi/Desktop/rfid.py", line 35, in selectBoard
    GPIO.output(self.boards[loop_id], loop_id == rid)
RuntimeError: The GPIO channel has not been set up as an OUTPUT

Can someone help me what I am doing wrong?

5
  • I wanted to connect 2 RC522 to my Raspberry Pi ... did you actually connect them? ... if yes, then how did you connect them?
    – jsotola
    Jun 10 at 0:25
  • I connected them like that: 2 RFID 1 Raspberry Pi
    – Martin
    Jun 10 at 8:56
  • Your MFRC522.py might not be using default SPI module and config. The SPI module seems an old, third party might not be what is in your Rpi OS. I once spent hours trying to adapt but got a mess and gave up. You might like to search this forum for my unsuccessful attempts and relay my work.
    – tlfong01
    Jun 11 at 3:19
  • Or you might need to fiddle some hardwiring setting (eg. Chip Select) on your RF522 modules. Ref: stackoverflow.com/questions/61165652/…
    – tlfong01
    Jun 11 at 3:26
  • Thx for the answers. i found out what was wrong. I will write it to my answer that I can maybe help others.
    – Martin
    Jun 12 at 21:49

1 Answer 1

0

after a long search I found a working answer for the error: "RuntimeError: The GPIO channel has not been set up as an OUTPUT":

The problem really is as it says. The used GPIO Pins (here 5 and 6) are not set as an output. To do that at the beginnig of the script you have to add:

GPIO.setmode(GPIO.BCM)               
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)

So the whole script would look like(for two RC522 readers):

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import spidev

GPIO.setmode(GPIO.BCM)               
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)

class NFC():
    def __init__(self, bus=0, device=0, spd=1000000):
        self.reader = SimpleMFRC522()
        self.close()
        self.bus
        self.boards = {}
        
        self.bus = bus
        self.device = device
        self.spd = spd

    def reinit(self):
        self.reader.READER.spi = spidev.SpiDev()
        self.reader.READER.spi.open(self.bus, self.device)
        self.reader.READER.spi.max_speed_hz = self.spd
        self.reader.READER.MFRC522_Init()

    def close(self):
        self.reader.READER.spi.close()

    def addBoard(self, rid, pin):
        self.boards[rid] = pin

    def selectBoard(self, rid):
        if not rid in self.boards:
            print("readerid " + rid + " not found")
            return False

        for loop_id in self.boards:
            GPIO.output(self.boards[loop_id], loop_id == rid)
        return True

    def read(self, rid):
        if not self.selectBoard(rid):
            return None

        self.reinit()
        cid, val = self.reader.read_no_block()
        self.close()

        return val

    def write(self, rid, value):
        if not self.selectBoard(rid):
            return False

        self.reinit()
        self.reader.write_no_block(value)
        self.close()
        return True


if __name__ == "__main__":
    nfc = NFC()
    nfc.addBoard("reader1",5)
    nfc.addBoard("reader2",6)

    data = nfc.read("reader1")
    nfc.write("reader2",data)

I hope this will help others :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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