Minecraft is a fantastic game that lets you play in a virtual 'sandbox' to unleash your creativity and build amazing structures.  The Raspberry Pi has a special version of Minecraft that can actually be controlled using the Python programming language.  In this guide I'll show you how to interact with Minecraft in a fun new way using near-field communication (NFC).  By swiping a paper block against a NFC reader you can create blocks in the Minecraft world and bridge the gap between the real and virtual worlds!

Before you get started you'll want to familiarize yourself with a few guides:

If you're new to the Raspberry Pi you'll want to check out some of the Pi getting started guides like how to install an operating system and connect to the Pi with SSH.

When you're ready to get started, continue on to learn what parts you'll need and how to connect them to the Raspberry Pi.

Hardware

You'll need the following hardware and tools to build this project:

  • Raspberry Pi.  Any model that can run Minecraft should work, but I recommend a Raspberry Pi 2 because it has the best performance in Minecraft.
  • PN532 NFC/RFID controller breakout board.  Make sure to use the breakout board version as the Arduino shield version is not compatible with this project (it uses an I2C interface that isn't supported by the project's software).
  • MiFare Classic NFC tags.  There are lots of different NFC tag types so be sure to get 'MiFare Classic' tags (with either 1K or 4K of space) to work with the code in this project.  You'll want small tags like stickers or keychains that can easily fit inside a small paper box.  This MiFare classic tag assortment has 4 small tags that fit great in small paper boxes (plus 1 larger card that could fit in a bigger box or item).
  • Raspberry Pi power supply, micro SD card, and GPIO breakout wires.  You need the basic components to power up and run a Raspberry Pi, and access the GPIO pins on the Pi.  If you're starting from scratch this Raspberry Pi starter kit has everything you need (including a Pi).  If you have the Pi but not much else, this starter kit without the Pi has all the accessories you need.
  • USB keyboard, mouse, and HDMI cable.  To play Minecraft on the Pi you need to hook up a keyboard, mouse, and monitor to the Pi.  This computer add-on pack for the Pi has everything you need.
  • Computer monitor or television.  You'll need something to display the Pi's HDMI output, like a computer monitor or television.  A small HDMI display is a nice option for a dedicated Raspberry Pi monitor.  Minecraft Pi edition doesn't currently have audio support so don't worry about hooking up speakers to the Pi.
  • Soldering equipment like a soldering iron and solder.  The PN532 breakout needs to have a few headers soldered to it so you need basic soldering equipment.  If you're new to soldering don't worry it's easy to learn--just check out this handy soldering guide and video.
  • Printer, paper, scissors, and tape or glue.  If you want to build little papercraft Minecraft blocks you'll need a printer and material for assembling the blocks.

Wiring

First make sure to assemble your PN532 breakout by following its guide here.  The headers and jumpers need to be soldered to the breakout so that it can connect to a breadboard and the Raspberry Pi.

You'll also need to change the jumpers on the breakout so that the board is configured for SPI communication.  Move SEL0 to OFF and SEL1 to ON like the picture below shows:

Then wire up the PN532 breakout to the Raspberry Pi as shown in the diagram below:

Make the connections as follows:

  • PN532 3.3V to Raspberry Pi 3.3V (red wire).
  • PN532 SCK to Raspberry Pi GPIO #25 (yellow wire).
  • PN532 MISO to Raspberry Pi GPIO #24 (green wire).
  • PN532 MOSI to Raspberry Pi GPIO #23 (orange wire).
  • PN532 SSEL to Raspberry Pi GPIO #18 (blue wire).
  • PN532 GND to Raspberry Pi GND (black wire).

This wiring will connect the PN532 to the Raspberry Pi using a 'software SPI' connection that just uses digital input and output pins for the connection.  Software SPI is simple, flexible and fast enough for our needs in this project.

Also note that unlike connecting to an Arduino, you don't need to use any level shifters to convert 3.3V to 5V and vice versa.  The Raspberry Pi GPIO runs at 3.3V and can connect directly to the PN532!

Once the PN532 is wired to the Raspberry Pi, continue on to learn how to install a Python library that talks to the device.

The PN532 can connect to the Pi in a number of ways, but for this project a SPI connection is used for communication.  To use the PN532 with a SPI connection you'll need to install the Adafruit PN532 Python library.

If you're curious another way to connect the PN532 to a Pi is with a serial connection and to use libnfc like this tutorial shows.  However using the serial connection on the Pi can be a little problematic because the kernel normally uses the Pi's serial port for outputting debug information on boot.  The PN532 also supports the I2C protocol, but it can be a little tricky to use with the Pi since it uses clock stretching.  To avoid all these issues this project and library uses a SPI connection to the Pi.

Also if you're new to using the Raspberry Pi be sure to first read guides on setting up the Raspberry Pi and connecting to its terminal to run commands.

Once you're ready to install the library make sure the Raspberry Pi is connected to the internet (with a wired or wireless network connection) and is running the latest Raspbian operating system.  Then follow the steps below to install the library.

First boot up the Pi and connect to its terminal with either a keyboard and monitor, or through the network with SSH.  Then run the following commands:

sudo apt-get update
sudo apt-get install build-essential python-dev git
cd ~
git clone https://github.com/adafruit/Adafruit_Python_PN532.git
cd Adafruit_Python_PN532
sudo python setup.py install

You can ignore warnings about packages that are already installed.  If everything was installed successfully you should see something like the following at the terminal:

If the installation stops with an error message, go back and carefully check that all the commands above were executed and try again.

Read MiFare Card Example

Once the library has been installed you can run an example to test detecting and reading a MiFare classic card.  To run the example navigate to the examples subdirectory and run the readmifare.py script by executing:

cd examples
sudo python readmifare.py

You should see the example start and wait for a MiFare card to be swiped, like below:

If you see an error message, carefully check the wiring between the PN532 is exactly as shown in the previous page and try again.

Now place a MiFare card above the PN532 antenna and notice information about the card is printed to the screen:

Almost like magic the PN532 wirelessly reads information from the MiFare card!  Notice the UID (unique identifier) for the card is printed, and a few bytes of data from block 4 of the card are also displayed (the actual data you see might be different for your card).

When you remove the card from the PN532 the information about the card will stop being displayed.  You can press Ctrl-C to quit the readmifare.py example at any time.

That's all there is to installing and using the PN532 library!  Currently the library is somewhat limited in functionality and only exposes detecting, authenticating, reading, and writing MiFare classic cards.  To learn how to use the library open the readmifare.py example in a text editor and examine the comments.  The interface to the library is very similar to the PN532 Arduino library.

If you have more advanced NFC needs be sure to check out the libnfc library and using it with the Raspberry Pi.

Continue on to learn how to control Minecraft with NFC tags!

Once the PN532 Python library is installed you're ready to make NFC tags that build blocks in Minecraft!

There are two Python scripts included in the PN532 examples.  One is mcpi_write.py and it's used to write a Minecraft block type to a MiFare classic card.  The other script is mcpi_listen.py which listens for Minecraft blocks swiped against the PN532 and creates them in a running Minecraft game.

Since you'll be running Minecraft on the Pi you'll want to make sure the Pi is connected to a monitor and has a mouse and keyboard connected to it.  If your Pi isn't connected to these devices yet, power it down and connect them, then power it back up so the monitor, keyboard, and mouse are working.  Instead of connecting to the Pi over a network connection with SSH, use the mouse, keyboard, and monitor to directly control the terminal on the Raspberry Pi.

If your Raspberry Pi doesn't automatically boot to the graphical X windows environment then login to the Pi and run the following command to start the graphical environment:

Now open a terminal in the X windows environment by clicking the Raspberry Pi Menu in the upper left and drilling in to the Accessories -> Terminal submenu.  This terminal will be used to run the python scripts that follow.

Writing Minecraft Blocks to NFC Cards

Start by running the following commands to navigate to the PN532 examples folder and start the mcpi_write.py script:

cd ~/Adafruit_Python_PN532/examples
sudo python mcpi_write.py

Once the script starts running it will walk you through choosing a Minecraft block type and writing it to a MiFare classic card.  The first thing the script will display is a message telling you to place a card on the PN532:

Place a NFC tag / card on the PN532 and you should see the program detects it and moves to the next step:

Notice the warning to not remove the card until you have finished writing.  If the card is removed during the writing process it could be corrupted!

Now you need to pick a block type to write to the card.  At the menu you can type L and press enter to have a list of block types displayed.  The complete list looks like:

Number	Block name
------	----------
     0	Air
     1	Bed
     2	Bedrock
     3	Bedrock, invisible
     4	Bookshelf
     5	Brick block
     6	Cactus
     7	Chest
     8	Clay
     9	Coal ore
    10	Cobweb
    11	Cobblestone
    12	Crafting table
    13	Diamond ore
    14	Diamond block
    15	Dirt
    16	Door, iron
    17	Door, wood
    18	Farmland
    19	Fence
    20	Fence gate
    21	Fire
    22	Flower, yellow
    23	Flower, cyan
    24	Furnace, inactive
    25	Furnace, active
    26	Glowstone block
    27	Gold block
    28	Gold ore
    29	Glass
    30	Grass
    31	Grass, tall
    32	Glass pane
    33	Glowing obsidian
    34	Gravel
    35	Ice
    36	Iron block
    37	Iron ore
    38	Ladder
    39	Lapis lazuli ore
    40	Lapis lazuli block
    41	Lava, flowing
    42	Lava, stationary
    43	Leaves
    44	Melon
    45	Moss stone
    46	Mushroom, brown
    47	Mushroom, red
    48	Nether reactor core
    49	Obsidian
    50	Redstone ore
    51	Sand
    52	Sandstone
    53	Sapling
    54	Snow
    55	Snow block
    56	Stairs, wood
    57	Stairs, cobblestone
    58	Stone
    59	Stone, brick
    60	Stone slab
    61	Stone slab, double
    62	Sugar cane
    63	Torch
    64	TNT
    65	Water, flowing
    66	Water, stationary
    67	Wood
    68	Wood planks
    69	Wool

For example to turn the card into a TNT block enter the number for TNT, 64, at the prompt and press enter.  You should see a new menu appear that lists a few sub-types for the TNT block:

Some blocks, like this TNT block, have a subtype that you can choose.  Other blocks have no subtype and will skip straight to the next step.

For the TNT block choose the 'Ready to explode' subtype with number 1 so that the block is active and can be exploded by swiping it with the sword in the game.  Type 1 and press enter to choose the ready to explode subtype.  Now you should see a confirmation screen like:

Now you need to confirm writing the TNT block to the card.  Type Y and press enter to confirm the write (or type N and enter to abort the write and quit).  

Don't worry if you accidentally write the wrong block type to a card.  You can rewrite a new block type to a card at any time by running the mcpi_write.py script again.

After confirming the write you should see a successful card write message like below:

That's all you need to do to write a block type to a card!  After the program quits you can safely remove the card from the PN532 breakout.

If you see an error message during the write, carefully check that the card is directly on top of the PN532's antenna and try running the mcpi_write.py script again.

You can run the mcpi_write.py script again to write other cards, or to change the block type written to a card.  Note that a card can only have one block type written to it at a time.  Also don't use NFC cards that have important data as it will likely be overwritten by the script!

Using Minecraft NFC Blocks

Now for some fun, let's run a script to start using the NFC blocks written in the previous step in Minecraft.  First you'll want to run the mcpi_listen.py script to start the program that will listen for NFC blocks.  Run the following in the same terminal you were using to run the previous script:

sudo python mcpi_listen.py

You should see the script display a message that it's waiting for a card to be swiped:

Even though Minecraft isn't yet running, try swiping a NFC card that was written earlier.  You should see a message displayed with the block type and a warning that Minecraft isn't running:

Now run Minecraft on the Pi to test out the block in the game.  With the terminal running the mcpi_listen.py script still open, click the Raspberry Pi menu in the upper left and drill down to the Games -> Minecraft Pi item.  You should see Minecraft start to load on the screen.

Once Minecraft loads click the Start Game button to start a game and either choose an existing world or create a new one to start the game.  

If you're new to Minecraft or the Raspberry Pi version of Minecraft be sure to read this tutorial on how to play.  Experienced Minecraft players will want to note that the Pi edition of Minecraft only has the creative / building mode and not the survival mode with enemies and combat.

Once you're inside a Minecraft world and can see the player's view you're ready to try swiping one of the NFC cards with a block written to it.  If you place a NFC card on the PN532 you should see your player start to move into the air.  Look directly down at the player's feet and you'll see blocks are being created where the player is standing.  Try moving around the world and you'll see blocks created along your path.  Pull the card off the PN532 and blocks will stop being created.

Woo hoo, now you can create blocks in Minecraft by just swiping a NFC card!  Try swiping other cards with different block types to see those blocks created in the Minecraft world.  If you created ready to explode dynamite blocks then try hitting them with the sword a few times to make them flash and explode.  Let your creativity flow by using NFC blocks to build amazing Minecraft creations!

When you're done playing Minecraft exit out of the program and return to the terminal that's running the mcpi_listen.py script.  Press Ctrl-C to stop the script from running.  Remember if you want to create blocks with NFC in future Minecraft games you must have the mcpi_listen.py script running the background.

Finally to complete the Minecraft building experience try making small papercraft Minecraft blocks that contain NFC tags.  I found this page allows you to build a Minecraft block that can be printed, cut out, and glued or taped together to make a papercraft block.  One thing to note is that the page doesn't seem to have an easy to use printing interface so you might need to take a screenshot of your completed block pattern, crop it in a graphic editor, and print the patten blown up on a piece of paper.  Below you can see a few blocks I printed, cut out, and glued together with NFC cards inside:

Remember NFC cards have a small range and can even be embedded in plastic or other material (but not metal!).  Try putting a NFC card inside a toy or other container to turn it into an object that interacts with Minecraft!

This guide was first published on Mar 24, 2015. It was last updated on Mar 24, 2015.