Musings of ls6

Arduino and NFC - part 2

This is the continuation of the story of connecting unexpensive NFC readers to Arduino. Part 1 explains how a Mifare reader from Stronglink can be easily connected. This time I’ll show you how to connect more than one of them.

Hardware configuration

The SL018 sports two jumpers which let you select the address the reader will use on an I2C bus. This means that you can connect up to four devices on a single bus and that before you connect more than one you have to solder one or more of these jumpers. Take look at the manual for details.

How is it going to work

Marc’s RFDuino can poll one reader at a time so we need a way to figure out when to poll which reader. Fortunately Stronglink’s readers feature a pin that signals whether a card has been found in the field. On the SL030 this is pin number 5 called “Out”, and we will connect it to one of the Arduino pins. Now, we will only poll the reader for card data once the reader have signalled that the card is actually available.

Software

So, the implementation of the above method works roughly like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//if the board has signalled that it found a tag
if(!digitalRead(reader1OutPin))
  {
    //query tag data
    rfid1.seekTag();

    //while we are waiting for data
    while(!rfid1.available()) {
    //break if the tag has been removed
    if (digitalRead(reader1OutPin)) {
      break;
    };
};
tagString = rfid1.getTagString();
Serial.println(tagString);


We can add as many segments like this in the loop() part of the Arduino code and each segment supporting a particular reader will ‘trigger’ whenever that reader fill find a card.

The code where we are waiting for data could be written more naturally as:

1
while(!digitalRead(reader1OutPin) || !rfid1.available());

but for some reason that I didn’t have the time to investigte does not work.

So, how to make it work exaclty?

Just go to Marc’s github repository and peek into the RFIDuino/SL018/examples/twoReaders directory. It contains a twoReaders file that I have written and which Marc included in his repository. Please note that Marc updated his library for Arduino IDE 1.0 but there is nothing IDE-specific in this example. If you have an older version of the RFDUino library just change the file extension of my code accordingly.

Here’s the full example code for your convenience:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 *  @title:  StrongLink SL018/SL030 RFID 2-reader demo
 *  @author: lukasz.szostek@gmail.com based on code of:
 *  @author: marc@marcboon.com
 *  @see:    http://www.stronglink.cn/english/sl018.htm
 *  @see:    http://www.stronglink.cn/english/sl030.htm
 *
 *  Arduino to SL018/SL030 wiring:
 *  A4/SDA     2     3
 *  A5/SCL     3     4
 *  5V         4     -
 *  GND        5     6
 *  3V3        -     1
 *  5          1     5 // first reader
 *  4          1     5 // second reader
 */

#include <Wire.h>
#include <SL018.h>

//pins to listen for the RFID board signalling that it has detected a tag
int reader1OutPin = 5;
int reader2OutPin = 4;
const char* tagString;

SL018 rfid1;
SL018 rfid2;

void setup()
{
  //make sure these two addresses match your reader configuration
  rfid1.address = 0x50;
  rfid2.address = 0x52;
  pinMode(reader1OutPin, INPUT);
  pinMode(reader2OutPin, INPUT);
  Wire.begin();
  Serial.begin(57600);

  // prompt for tag
  Serial.println("Show me your tag");
}

void loop()
{
  //if the board has signalled that it found a tag
  if(!digitalRead(reader1OutPin))
  {
    //query tag data
  rfid1.seekTag();
  
  /* for some reason the loop defined as:
    while(!digitalRead(reader1OutPin) || !rfid1.available());
 does not work so we need a workaround:*/

    //while we are waiting for data
    while(!rfid1.available()) {
      //break if the tag has been removed
      if (digitalRead(reader1OutPin)) {
        break;
      };
    };
    tagString = rfid1.getTagString();
    Serial.print("Reader 1 found: ");
    Serial.println(tagString);
    //wait a while before querying the tag again
    delay(1500);
  };

  //"in parallel" we wait for the other reader
  if(!digitalRead(reader2OutPin))
  {
    rfid2.seekTag();
    while(!rfid2.available()) {
      if (digitalRead(reader2OutPin)) {
        break;
      };
    };
    tagString = rfid2.getTagString();
    Serial.print("Reader 2 found: ");
    Serial.println(tagString);
    delay(1500);
  };
}

Good luck and have fun! If you have some improvements to this code for Marc’s (preferably) or my repository on git hub and help pushing this library forward.