David Díaz.

Playing with Arduinos, RFID cards and Ruby

This is probably one of the coolest things I’ve worked on in a while and I also happen to have time to write a blog post about it, which is great, I love working with hardware from time to time.

It definitely isn’t the biggest programming challenge I’ve had but it is pretty cool and I am certain others might find it interesting. Yesterday, I was helping my sister with a project she was doing for her professional practice (she is a very weird kind of engineer who specializes in automation stuff, she is great at it), she was basically asked to open a door with a RFID card.

To do this, she had a very simple plan, we plug an RFID card reader into a computer which then sends the RFID over to a computer and then tells an Arduino to open the door. She used this RFID Reader (RFID Reader ID-20 (125 kHz)) which is pretty inexpensive and easy to plug into a computer.

This was fairly straight-forward for her, she mounted the RFID card reader into a board and plugged it into her computer along with her Arduino. She then fired up putty and set it to read the RFID card reader’s serial port (COM 9, in our case) which showed us the RFID of any card we read. 

This is where I came in, she isn’t that much of a coder so she asked me for help on how to finish the connection between the RFID reader and the Arduino. This is what I came up in Ruby:

require "serialport"

@rfid_reader = SerialPort.new 4 # Open COM 5
@arduino = SerialPort.new 2 # Open COM 3

@valid_ids = ['4500B8D690BB']

def readRFID
  rfid = @rfid_reader.gets
  rfid.slice(0) if rfid.size == 13
  rfid.slice(0..1) if rfid.size == 14

  return rfid

puts "Waiting for input..."
loop do
  rfid = readRFID # Get the RFID of the current card
  if !rfid.empty?
    if @valid_ids.include? rfid # is the ID valid?
      puts "RFID: " + rfid

      # Send the arduino a signal so it opens the lock.
      @arduino.write "1"
      puts "Invalid RFID: " + rfid
      @arduino.write ""
  @arduino.write ""

I was actually going to use C++ but I prefer using Ruby whenever possible. This is a very simple code snippet but it did the job wonderfully. The code is documented and very simple, I used the fabulous ruby-serialport gem which, took care of all the heavy lifting for me.

This was only half of the work though, we also needed to get the Arduino to wait for an input coming from the computer and do something whenever a value is received, in this case we wanted to open a lock so that’s what we’ll do.

void setup () {
  Serial.begin (9600);
  pinMode (12, OUTPUT);

void serialEvent() {
  digitalWrite(12, HIGH);
  digitalWrite(12, LOW);

The code is even simpler than the Ruby one running on the computer. All it does is wait for there to be an input and then activate a pin when there is one. Ruby is pretty much doing all of the actual processing and telling the Arduino when and what to do.

Finally, all we had to do was fire up the terminal and run our Ruby program which would then wait for there to be an input form the RFID reader. Whenever there’s an input it’ll log it and then, if the RFID is registered, tell the Arduino to open the lock. And that’s basically it, it made a nice problem to solve for fun.

Have a comment? Feel free to email me.
Did you enjoy this post? Buy me a coffee ☕️.