Talking to Arduino through the Serial Port

By markvillacampa on January 21, 2012 — 2 mins read

I wanted to be able to pass any data to Arduino from my PC or the Internet. There are several ways to do that. The most obvious one is buying an
Ethernet Shield or
Bluetooth module, but they are more expensive than an actual Arduino Uno, so I looked for alternatives.

I wanted to create a web interface to control Arduino, so these
Serial-to-Network libraries seemed like the best solution to do it with PHP since it doesnt support serial connection (well actually there’s a
PHPSerial class for doing that but it didn’t word for me under Mac OSx Lion).

First I tried
SerialIP since it didn’t need any additional software on the host. However the
slattach command was removed in OSx 10.5. I recompiled it from
some source code I found on the Apple Open Source site but that didn’t work either. Compiling
SOCAT and looking for a similar command to attach the serial port to the network seemed too complicated so I passed to the next library.

SerialNet.pde uses Processing, a language based on Java and especially aimed to generate simple graphics, as a proxy. It worked like a charm and I was able to connect to the desired port trough Telnet.

However I wanted a cleaner solution that didn’t need a proxy, and then I found
Ruby SerialPort. It couldn’t be easier. Simply install the gem doing:

sudo gem install ruby-serialport

This is the Ruby code for the “Hello World”, you can find the path to your Arduino under the Tools > Serial Port menu of the Arduino SDK:require ‘rubygems’

require 'serialport'

sp = "/dev/PATH_TO_YOUR_DEVICE", 9600

case ARGV[0]
when 1
  sp.write 1
when 0
  sp.write 0

Now connect a LED between the pins 13 and GND and upload this code to your Arduino:

int ledPin = 13;
int state=0;

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  if (Serial.available() > 0) {
    state =;
    switch(state) {
      case '1':
      case '0':

Now simply run this using your Terminal to turn on the LED:

ruby yourfile.rb 1

And this to turn it off:

ruby yourfile.rb 0

And now you can even easily create a web interface to control your Arduino project using Rails or the Ruby web framework of your choice 🙂

PS: This has been tested under Mac OSx Lion, but should work under Linux and Windows provided that you have Ruby installed and you write the correct path to the Arduino serial port.