Raspberry Pi: set up NFC - how it works
Nowadays, NFC tags can be found everywhere in everyday life. In this practical tip, you will learn how to set up NFC on your Raspberry Pi.
Raspberry Pi with NFC: the accessories you need
To use NFC with your Raspberry Pi, you need the following accessories:
- A Raspberry Pi (preferably the Raspberry Pi 3, Model B)
- The Mifare RC522 RFID module (including keycard)
- Jumper cables
Connect the NFC module to the Raspberry Pi
Connect the module to your Raspberry Pi as follows:
- SDA → Pin 24 / GPIO8 (CE0)
- SCK → Pin 23 / GPIO11 (SCKL)
- MOSI → Pin 19 / GPIO10 (MOSI)
- MISO → Pin 21 / GPIO9 (MISO)
- IRQ → -
- GND → Pin6 (GND)
- RST → Pin22 / GPIO25
- 3.3V → Pin 1 (3V3)
Configure Raspberry Pi & install software
After you have connected the module correctly, you can start installing the necessary software and correctly configuring the Raspberry Pi.
- Run the sudo nano /boot/config.txt command (without the quotation marks) to edit the configuration file.
- Add a new line at the end of the file and enter "device_tree_param = spi = on" there. Then add another line and enter "dtoverlay = spi-bcm2708" here. Finally save the file with [Ctrl] + [O], [Ctrl] + [X].
- Start the Raspberry Pi configuration with the "sudo raspi-config" command and activate SPI under the "Advanced Options" tab. After that, you will have to restart your Raspberry Pi once.
- With the command "dmesg | grep spi" you can check whether the NFC module has been loaded correctly.
- Now install the required Python packages with the command "sudo apt-get install git python-dev --yes".
- Then run the command "git clone //github.com/lthiery/SPI-Py.git" to copy the Python SPI file.
- Then change to the created directory with the "cd SPI-Py" command and execute the "sudo python setup.py install" command to start the setup.
- Finally, copy the RFID RC522 library: "git clone //github.com/mxgxw/MFRC522-python.git && cd MFRC522-python"
Test the NFC module
If you have installed the necessary components, you can test the NFC module for the first time.
- With the "sudo python Read.py" command, you can run a Python script that controls the NFC module and lets it read out NFC tags.
- Now hold your NFC tag to the module. Has the day been recognized? If so, you've done everything right.
- Tip: With the commands "sudo nano Write.py" and "sudo nano Read.py" you can edit the pre-made Python scripts and copy parts of the program code into your project.