PN532 NFC/RFID module

Elechouse PN532 module

The PN532 is a 13.56 MHz RFID reader chip. It's common and very easy to interface to with the PN532 (About Modules) module.

Wiring Up

Simply connect up I2C as follows:

RFID Espruino
GND GND
VCC BAT
SCL B6
SDA B7

Note: some of these boards have 2 very small switches on them that select the interface type (I2C, SPI, or USART). You need to make sure that I2C is selected.

Software

The Espruino module currently implements only a very small subset of the PN532's functionality, but you can use it as follows.

The code below will light the red, green or blue LED depending on which of three RFID tags is in range:

I2C1.setup({scl:B6, sda:B7});
var nfc = require("PN532").connect(I2C1);
print(nfc.getVersion());
nfc.SAMConfig(); // start listening
setInterval(function() {
 nfc.findCards(function(card) {
  print("Found card "+card);
  card = JSON.stringify(card);
  var leds = [LED1,LED2,LED3];
  if (card=="[147,239,211,128]") digitalWrite(leds,1);
  if (card=="[249,192,235,164]") digitalWrite(leds,2);
  if (card=="[4,99,129,114,72,52,128]") digitalWrite(leds,4);
 });
}, 1000);

Once nfc.SAMConfig() is called, you can then poll the PN532 to see if it has any cards in range using nfc.findCards(...). If a card is in range, it will return an array containing the unique ID of that card.

As another example, the code below will toggle the red LED depending on whether a specific RFID tag is in range or not:

var tagOnePresent = false;
function checkTagsPresence(){
  if(tagOnePresent === true){
    //console.log('tag one is present');
    digitalWrite(LED1, 1);
  } else {
    //console.log('tag one is NOT present');
    digitalWrite(LED1, 0);
  }
}

I2C1.setup({scl:B6, sda:B7});
var nfc = require("PN532").connect(I2C1);
nfc.SAMConfig(); // start listening
setInterval(function() {
  var theCard = false;
  nfc.findCards(function(card) {
    //print("Found card "+card);
    card = JSON.stringify(card);
    if(card == "[4,113,246,113,8,238,0]"){
      //console.log('card found');
      tagOnePresent = true;
      checkTagsPresence();
      // in case we're not called during next iteration, handle beforehand update
      if(backTimeout != -1){ clearTimeout(backTimeout); }
      backTimeout = setTimeout(function(){
        backTimeout = -1;
        //console.log('backtimeout called ..');
        tagOnePresent = false;
        checkTagsPresence();
      }, 1100);
    }
  });
}, 1000);

If you place a card about maximum 2.4 cm above or below the module, the LED will light up. If you remove it, the 'backtimeout' will toggle it off after a little delay

Using

(No tutorials are available yet)

Buying

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.