Adventures In Hardware

Using Arduino to control iTunes

Over the weekend, I threw together a little project to control iTunes with any IR remote control using an IR receiver and an Arduino to provide connectivity to the computer.

To begin, Ken Shirriff provided a handy little library to assist in the capture of the IR remote signals named IRremote. With that, the basic Arduino program ends up being just a few lines of code.

        
#include <IRremote.h>

#define IRRECV_PIN 11

IRrecv irrecv(IRRECV_PIN);
decode_results results;

void setup()
{
    Serial.begin(9600);
    irrecv.enableIRIn();
}

void loop()
{
    if (irrecv.decode(&results)) {
        // We will do something with the results
        // structure later.
        
        irrecv.resume();
    }
}
        
    

Since we defined the IRRECV_PIN in the code as pin 11, the output pin from the IR receiver should be connected to it. This partuclar IR receiver spec sheet calls for a resistor between Vcc and the Vin pin and a capacitor between Vin and Gnd, but if using a different device you should check the specs for it. The full schematic is provided below.

The next step is to do something with the results returned by the IRremote library. Ken also provides a handy method to create a FNV hash from the results. You can look at his example project. Using the results of his decodeHash method, we send the value over the USB connection to the computer with the Serial.println Arduino function.

        
void loop()
{
    if (irrecv.decode(&results)) {
        Serial.println(decodeHash(&results), HEX);
        irrecv.resume();
    }
}
        
    

On the Mac side, I turned to Ruby and the ScriptingBridge to watch the USB serial connection for remote codes and signal the results to iTunes. The Serial#gets will return a value each time the Arduino receives a code.

        
require 'serialport'
require 'osx/cocoa'
OSX.require_framework 'ScriptingBridge'

serial = SerialPort.new('/dev/tty.usbmodem621', 9600)
itunes = OSX::SBApplication.applicationWithBundleIdentifier('com.apple.iTunes')

while input = serial.gets.strip
    case input
    # E1DE04A2 was the hash given for the play/pause button on my remote.
    # Different remote vendors will use different codes.    
    when 'E1DE04A2'
        itunes.playpause
    end
end
        
    

The actual Ruby program used to control iTunes is more involved, but I have decided to leave that for another post.

Parts list: