# Adafruit NFC/RFID on Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/001/535/medium800/raspberry_pi_MifareCardRead_600w.jpg?1396773159)

Interested in adding some NFC (near-field communication) fun and excitement to your Raspberry Pi? You're in luck!   
  
A big advantage of Linux is that it includes a large number of software “stacks” developed by the open source community, and you get to take advantage of all that hard work simply by using or installing the right library.   
  
NFC is no exception here, with [_libnfc_](https://github.com/nfc-tools/libnfc) having been around for a quite some time—in fact, it's the original reason the NFC Breakout was developed!&nbsp;libnfc is a library for **C programmers**. For Python and CircuitPython, there’s [an equivalent module](https://learn.adafruit.com/adafruit-pn532-rfid-nfc/python-circuitpython).  
  
To get libnfc playing well with your Pi and your Adafruit NFC breakout you'll need to make some minor configuration changes to the system and install some code, but it's pretty painless, and this tutorial will show you everything you need to do to start writing your own NFC-enabled apps on the Pi!

**This guide assumes you have some version of Raspberry Pi OS already running, that the system is network-connected and so forth.&nbsp;We have [a series of tutorials for first-time users](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi/overview) if you need some help with those steps.**

# Adafruit NFC/RFID on Raspberry Pi

## Pi Serial Port

The easiest way to use libnfc with the Adafruit NFC Breakout is via _serial UART,_ since it's well-supported by libnfc out of the box. Unfortunately the serial UART port on the Pi is already dedicated to other purposes, and needs to be freed up for libnfc…  
  
The following steps (based on a clean Raspberry Pi OS installation) should make the serial UART available to us:

### Using the Desktop/GUI “Full” OS

You’ll find these settings in the Raspberry Pi Configuration tool. From the Raspberry menu at the top-left…

Go to the “Interfaces” tab and you’ll see two options “Serial Port” and “Serial Console.” Toggle _both_ of these away from their default states. Serial Port should be _enabled,_ Serial Console _disabled._ Then click “OK.” Reboot when prompted.

_With each new OS release, it’s normal that some configuration options may move to different menus or positions. If you can’t find it where shown, check under the other top-level menu options…even if moved, the name will likely remain similar._

![rfid___nfc_gui-serial-1.png](https://cdn-learn.adafruit.com/assets/assets/000/108/815/medium640/rfid___nfc_gui-serial-1.png?1644360436)

![rfid___nfc_gui-serial-2.png](https://cdn-learn.adafruit.com/assets/assets/000/108/816/medium640/rfid___nfc_gui-serial-2.png?1644360443)

### Using the “Lite” Command-Line OS

These options can be found in the `raspi-config` tool, which must be run as root:

```auto
sudo raspi-config
```

Navigate down to “Interface Options” and then “Serial Port.” Answer “No” to the login shell question, and “Yes” to the serial port hardware.

Navigate back to the main menu, tab to the “Finish” button and reboot when prompted.

_With each new OS release, it’s normal that some configuration options may move to different menus or positions. If you can’t find it where shown, check under the other top-level menu options…even if moved, the name will likely remain similar._

![rfid___nfc_cmdline-serial-1.png](https://cdn-learn.adafruit.com/assets/assets/000/108/817/medium640/rfid___nfc_cmdline-serial-1.png?1644360613)

![rfid___nfc_cmdline-serial-2.png](https://cdn-learn.adafruit.com/assets/assets/000/108/818/medium640/rfid___nfc_cmdline-serial-2.png?1644360618)

![rfid___nfc_cmdline-serial-3.png](https://cdn-learn.adafruit.com/assets/assets/000/108/819/medium640/rfid___nfc_cmdline-serial-3.png?1644360623)

![rfid___nfc_cmdline-serial-4.png](https://cdn-learn.adafruit.com/assets/assets/000/108/820/medium640/rfid___nfc_cmdline-serial-4.png?1644360627)

# Adafruit NFC/RFID on Raspberry Pi

## Building libnfc

# Step One: Prepare for and Download libnfc

Before you can do anything, you will need to get the libnfc library. Make sure you have internet access on your Pi, through Ethernet or WiFi.

If using the full/GUI Raspberry Pi OS, open a terminal window for typing commands.

```xml
cd
sudo apt-get install git autoconf libtool libusb-dev
git clone https://github.com/nfc-tools/libnfc
```

This will create a new directory “libnfc” in your home directory. Answer “Y” when prompted on the apt-get installation.

# Step Two: Set Up libnfc For the Pi

Before libnfc can be built, it needs to be configured for the target system and based on some parameters specific the NFC device you have connected.

libnfc requires a configuration file in a specific location. We’ll start by creating the required folder, then (unrelated) change to the libnfc directory for subsequent steps.

```xml
sudo mkdir -p /etc/nfc/devices.d
cd libnfc
```

Next step varies by target system. This refers to the **machine where the NFC reader will be connected** …occasionally one might set up the OS on one machine, then move the card to a different Pi for use. Follow one or other, not both! If you upgrade or downgrade the Raspberry Pi to a different model later, you _might_ need to recompile everything, depending on which Pi models.

## For Recent Raspberry Pi Models

This step works for Raspberry Pi 400, Pi 4, and Pi 3 models B and B+.&nbsp;It _almost certainly_ works on Compute Module 4 and Pi Zero 2W, but did not have hardware on-hand to confirm (worst case, if later tests fail, you can “make clean” and re-try the alternate instructions below).

Enter this command to install the libnfc serial configuration file for these Pi models:

```auto
sudo cp contrib/libnfc/pn532_uart_on_rpi_3.conf.sample /etc/nfc/devices.d/pn532_uart_on_rpi_3.conf
```

_This should be entered as one continuous long line…it might appear wrapped to two lines in your browser, but enter it as one with a space character in-between. Or use the “Copy Code” button._

## For Earlier Pi Models

This step works for Raspberry Pi 2, Pi 1, and initial Pi Zero models (not Pi Zero 2W). This _may_ work on the wireless Pi Zero W models, but did not have hardware on-hand to confirm (worst case, if later tests fail, you can “make clean” and try the alternate instructions above).

Enter this command to install the libnfc serial configuration file for these systems:

```auto
sudo cp contrib/libnfc/pn532_uart_on_rpi.conf.sample /etc/nfc/devices.d/pn532_uart_on_rpi.conf
```

_This should be entered as one continuous long line…it might appear wrapped to two lines in your browser, but enter it as one with a space character in-between. Or use the “Copy Code” button._

# Step Three: Configure the Library

The next step is to configure the project itself using the 'configure' tool, as follows:

```xml
autoreconf -vis
./configure --with-drivers=pn532_uart --sysconfdir=/etc --prefix=/usr
```

This may take a minute or two to complete, that’s normal. The output will resemble the following:

![](https://cdn-learn.adafruit.com/assets/assets/000/108/822/medium800/rfid___nfc_libnfc-config.png?1644365567)

# Step Four: Build and Install!

Once configured, the following commands then build and install the library:

```auto
make
sudo make install all
```

This may take a few minutes to complete. Some compiler warning messages might be generated along the way…these can be safely ignored.

Once the process is complete, you’re ready to test on actual hardware…

# Adafruit NFC/RFID on Raspberry Pi

## Testing it Out

# Hooking Everything Up

The Adafruit [NFC Breakout](https://www.adafruit.com/products/364) board is much more appropriate with the Pi than the [NFC Shield](https://www.adafruit.com/products/789 "Link: https://www.adafruit.com/products/789"), since the breakout doesn't have 5V level shifting (which means you won't accidentally damage your Pi!), and you have easier access to the bus select pins, etc.  
  
If it isn't already hooked up,&nbsp;you can connect your breakout&nbsp;now using&nbsp;a convenient [Pi Cobbler](http://adafruit.com/products/914), following the image below: There are two places to do this: either the board end header (fits more nicely in a breadboard), or the FTDI header along one side (shown in photo below):

![](https://cdn-learn.adafruit.com/assets/assets/000/001/533/medium800/raspberry_pi_NFCBreakout_Pi_Hookup_1000w.jpg?1396773171)

 **Note:** Make sure that the **SEL0** and **SEL1** jumpers on the NFC breakout are set to **OFF** , which will cause the PN532 to boot into UART mode (rather than SPI and I2C, which aren't currently supported by libnfc). &nbsp;You will need to reset the breakout after changing these pins, which you can do by cycling the power pin.  
  
Use a 5V supply pin from the Pi, and the 5V input on either the FTDI header or board end header, rather than the Pi’s 3.3V supply, since the 3.3V supply is used by the core on the Raspberry Pi and you don't want to pull sharp, heavy loads from it, like when you first enable and charge the near field.

# Read an ISO14443-A (Mifare, etc.) Card with nfc-poll

With libnfc configured, built and installed, you can go back to the command-line, place a card on the reader, and run the following command to get the tags unique ID:

```auto
nfc-poll
```

_nfc-poll should be able to run this way thanks to the “make install” step on the prior page, which puts it in the /user/bin directory. If it doesn’t run, check if you did the install step, or you can find the executable program in&nbsp;__~/libnfc/examples_

A successful run will yield the following:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/511/medium800/raspberry_pi_5_nfc_poll.png?1396773578)

That's it! &nbsp;From here, you can explore some of the other examples in the 'examples' folder, and&nbsp;figure out how to get started writing your own applications based on libnfc! &nbsp;Be sure to have a look at the [libnfc project page](https://github.com/nfc-tools/libnfc "Link: http://www.libnfc.org/documentation/introduction").

## Common Issues

If nfc-poll returns this message:

**pn53x\_check\_communication error**

The culprit is usually one of two things:

- Try swapping the RX and TX wires.
- The particular model of Pi might be an exception to the “Recent” vs “Earlier” rules on the previous page. If the wire swap didn’t fix the issue, try using the opposite library setup steps…do a “make clean” first so that everything’s rebuilt.


## Featured Products

### 13.56MHz RFID/NFC tag assortment - Classic 1K

[13.56MHz RFID/NFC tag assortment - Classic 1K](https://www.adafruit.com/product/365)
One of each of our favorite 13.56MHz RFID/NFC Classic 1K tags - 5 in total!

- [Credit card size](http://www.adafruit.com/products/359)
- [1" diameter 'laundry' clear tag](http://www.adafruit.com/products/361)
- <a...></a...>

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/365)
[Related Guides to the Product](https://learn.adafruit.com/products/365/guides)
### 13.56MHz RFID/NFC Clear Keychain Fob - Classic 1K

[13.56MHz RFID/NFC Clear Keychain Fob - Classic 1K](https://www.adafruit.com/product/363)
This is a blank 13.56MHz RFID/NFC keychain fob - often used for keys but also an easy way to tag something. The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer when placed a couple inches away.  
  
These can be read by almost any 13.56MHz...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/363)
[Related Guides to the Product](https://learn.adafruit.com/products/363/guides)
### 13.56MHz RFID/NFC Bracelet - Classic 1K

[13.56MHz RFID/NFC Bracelet - Classic 1K](https://www.adafruit.com/product/921)
This is a blank 13.56MHz RFID/NFC silicone bracelet. The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer when placed a couple inches away.  
  
These can be read by almost any 13.56MHz RFID/NFC reader but make sure it can handle&nbsp;ISO/IEC...

In Stock
[Buy Now](https://www.adafruit.com/product/921)
[Related Guides to the Product](https://learn.adafruit.com/products/921/guides)
### 13.56MHz RFID/NFC Charm - Classic 1K

[13.56MHz RFID/NFC Charm - Classic 1K](https://www.adafruit.com/product/884)
This is a blank 13.56MHz RFID/NFC embedded in a phone charm&nbsp;- often used for train/bus passes but also found in other systems where a proximity card is desired. The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer when placed a couple inches...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/884)
[Related Guides to the Product](https://learn.adafruit.com/products/884/guides)
### 13.56MHz RFID/NFC Clear Tag - Classic 1K

[13.56MHz RFID/NFC Clear Tag - Classic 1K](https://www.adafruit.com/product/361)
This is a blank 13.56MHz Classic 'laundry' tag - often used for laundry or identification but also found in other systems where a small proximity card is desired. This one is clear! &nbsp;The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer...

In Stock
[Buy Now](https://www.adafruit.com/product/361)
[Related Guides to the Product](https://learn.adafruit.com/products/361/guides)
### 13.56MHz RFID/NFC White Tag - Classic 1K

[13.56MHz RFID/NFC White Tag - Classic 1K](https://www.adafruit.com/product/360)
This is a blank 13.56MHz RFID/NFC laundry tag&nbsp;- often used for laundry but also general tagging. The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer when placed a couple inches away.  
  
These can be read by almost any 13.56MHz RFID/NFC...

In Stock
[Buy Now](https://www.adafruit.com/product/360)
[Related Guides to the Product](https://learn.adafruit.com/products/360/guides)
### 13.56MHz RFID/NFC Card - Classic 1K

[13.56MHz RFID/NFC Card - Classic 1K](https://www.adafruit.com/product/359)
This is a blank 13.56MHz RFID/NFC card - often used for train/bus passes but also found in other systems where a proximity card is desired. The tag contains a small RFID chip and an antenna, and is passively powered by the reader/writer when placed a couple inches away.  
  
These can be...

Out of Stock
[Buy Now](https://www.adafruit.com/product/359)
[Related Guides to the Product](https://learn.adafruit.com/products/359/guides)
### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)

## Related Guides

- [NFC Raspberry Pi Media Player](https://learn.adafruit.com/nfc-raspberry-pi-media-player.md)
- [Mystery Box: Remote Chess Board Puzzle Lock](https://learn.adafruit.com/mystery-box-remote-chess-board-puzzle-lock.md)
- [Raspberry Pi NFC Minecraft Blocks](https://learn.adafruit.com/raspberry-pi-nfc-minecraft-blocks.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Adafruit PN532 RFID/NFC Breakout and Shield](https://learn.adafruit.com/adafruit-pn532-rfid-nfc.md)
- [Collin's Lab: RFID](https://learn.adafruit.com/collins-lab-rfid.md)
- [Cupcade: the Raspberry Pi Micro Arcade Cabinet](https://learn.adafruit.com/cupcade-raspberry-pi-micro-mini-arcade-game-cabinet.md)
- [Adafruit's Raspberry Pi Lesson 2. First Time Configuration](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-2-first-time-configuration.md)
- [Adafruit's Raspberry Pi Lesson 6. Using SSH](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh.md)
- [Run an X-Carve CNC Machine Wirelessly with a Raspberry Pi](https://learn.adafruit.com/control-an-xcarve-cnc-machine-wirelessly-with-a-raspberry-pi.md)
- [Matrix and 7-Segment LED Backpack with the Raspberry Pi](https://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi.md)
- [Running Minecraft on a Raspberry Pi](https://learn.adafruit.com/running-minecraft-on-a-raspberry-pi.md)
- [Adafruit's Raspberry Pi Lesson 12. Sensing Movement](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement.md)
- [Networked Thermal Printer using Raspberry Pi and CUPS](https://learn.adafruit.com/networked-thermal-printer-using-cups-and-raspberry-pi.md)
- [DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging.md)
