Subscribe via RSS

Tracking trains with an Arduino and RFID

So, you're now controlling your trains with your Wii Nunchuck and an Arduino... What if you get tired and want them to run by themselves? This next project will help you with one piece of the puzzle, as long as the trains are still moving :)

Whilst looking for the Wii Nunchuck Adapter at Toys Downunder, I came across RFID 'buttons' and an RFID reader that were compatible with the Arduino. Since I have the Arduino Mega, I knew I'd have pins/serial lines to spare, so I went ahead and added them to my order.



I bought the ID-12 RFID Reader and 2 RFID Buttons. I chose against purchasing the breakout board, as after reading the comments it didn't look like it would fit the ID-12 (only fitting the ID-20.) I've since asked Toys Downunder to correct this comment in case it really does, as soldering these readers is a pain. (Note, they have since updated the notes and the ID-12 does indeed fit. They also offered me a free one! I might act on that next time I go shopping there!)



Wiring up the reader was going to be pretty simple. I'd see examples of it wired here for a door lock and in the datasheet. I was a little confused as to which 'output' to use; there are two provided and I guessed right the first time (pin 9: Inverted TTL Data.) This output was fed into an RX port on one of the 3 surplus Serial ports on the Arduino Mega (Serial1).



After a quick bit of coding, I had the RFID reader picking up the RFID button reliably. You can find instructions on this from the Arduino site for another type of reader, but below is a quick code listing for getting it to work on the Arduino Mega. I wrote this with guidance from the previous door lock link and the Arduino site as well.

bool newData = false;
int cardData[16];
int idx=0;
void setup() { 
  Serial.begin(9600);  // Hardware serial for Monitor 9600bps
  Serial.println("Starting Reader..."); //show some activity
  Serial1.begin(9600); //ID-12 uses 9600!
}
void loop() { 
  while (Serial1.available() > 0) {
    cardData[idx] = Serial1.read();
    idx++;
    if (idx == 16) {
      newData = true;
      idx = 0;
    }
  }
  if (newData){
    newData = false;
    Serial.print("Found RFID device: ");
    for (int i = 0; i < 16; i++) Serial.print(cardData[i]);
    Serial.println(".");
  }
} 

I've since read that external antennas can be hooked up, but creating one of these seems to be quite involved. Even though this ID-12 was the smallest reader available on Toys Downunder, there (after reading the datasheet) seems to be a slightly smaller one. It happens to be smaller since it doesn't include an internal antenna. Either way, if you choose to make your own, you may well be able to lay it in the trackbed. Otherwise, you can just shove a reader like this ID-12 into a trackside building or at the back of a engine shed to know which loco has arrived.



Initial testing showed that the Buttons could be read through my Thunderbird B-Train Shorty with motorised chassis; although the chassis wasn't powered at the time. I'll show the reader installed on a model railway in a future article.

avatar

About stevenh

Trains... trains... trains... + Electronics + Japan.
Comments (7) Trackbacks (0)
  1. Oh, very nice. I would have thought the buttons too large, but apparently not. Have you learned whether they work through a powered motor yet? Would any of the smaller form factors work? I’ve seen them in pills here:
    http://www.sparkfun.com/commerce/product_info.php?products_id=9416
    They are a bit more expensive, but easier to integrate into trains, I would think. But I wonder what their limits are.

    • Don,

      The buttons are probably just a little too large… as you can see, they are ‘just’ thinner than the width of the WAMU. You’d also have to grind away the brake-gear under the WAMU to fit it as low as possible.

      Meanwhile, the pills you have pointed out look great, but they have a read distance of 10mm!? I can’t imagine wanting something that close to the rails… the loading gauge wouldn’t work.

      I intend on getting the reader into a station platform tonight and hopefully getting the results on here.

  2. Surely the read distance could be extended with a reader with a better antenna, yeah? Did you say there was a way to connect an external antenna to your reader unit? Might be a good way to go.

  3. nice.
    MERG also offers for their members those reader & a concentrator and breaktout bords. Thought it might be interesting for you: http://www.merg.org.uk/MERG%20KITS%20LEAFLET2008RFID1.pdf
    price for members is 12.5 GBP per reader
    I got membership to get access to their kits.
    It is also most likely that the dataformat produced by their readers get recognized by JMRI & rocrail software soon.

    • Lothar,

      Thanks for the link! The RFID Concentrator would work perfectly here too. I was concerned that they’d wanted one COM Port per reader, but the concentrator is great and could easily communicate with the Arduino too. My main goal in this whole process is to have the Arduino doing most of the work; so that I can turn off the PC and just let the railway operate alone.

      Steven.

  4. G’day,

    Is it possible to use a bar code reader and a fibre cable (with reflective sensor attached) instead of RFID?

    • I imagine so… I’ve never used a barcode reader, but as long as you have your barcodes aligned under the vehicles (they could also be quite short) then you should be fine.

      On that note you could also have magnets and a reed switch if you kept the clearances absolute.
      Then you can just determine the vehicle by something similar to morse code.


Leave a comment


*

No trackbacks yet.