A few months ago, I bought a Raspberry Pi, and it’s been the best recent purchase I made, by far. Not so much because I have that many things to do with it, but because it features a few GPIO pins, which you can control with software and make output ones and zeroes (basically high and low voltage).

This has opened a whole new world to me, as, before this, software and hardware were completely separate and never did the twain meet. All of a sudden, though, I can now program things like LEDs to turn on and off, interface with sensors and read their values, or even transmit a horrible FM wave I can play on my radio.

While I’m waiting for my RF transmitter/receiver to arrive in the mail (so I can control my garage door and RF power sockets remotely), I bought a few IR LEDs and photodiodes (IR sensors), and thought I’d see what I could do with them. After a lot of research, I managed to control my TV remotely, through a web interface. The ratio of fun to usefulness on this is pretty much infinite, as it is probably the most useless and fun thing I’ve ever done. In this post, I will teach you all about it.

Transmitting IR signals with the Raspberry Pi

If all you want to do is transmit IR, you can do it very simply. First of all, let me warn you: You can’t manually modulate the GPIO pins at 38 KHz, as 20 μsec accuracy is really hard to achieve with a Linux userland program. I tried to do it, and could get within +- 40 μsec, but this wasn’t enough. Instead, you can use LIRC, which is included in the Raspbian repositories, to do this. All I did was connect an 1.6V IR LED to a GPIO pin and the ground, and that’s it (the voltage is fine, as you just pulse the LED, but I’m not sure if I have to add a resistor in series, I suck at EE).

Here is Alex Bain’s great guide on how to install LIRC on the Raspberry Pi, it also includes instructions for a receiver. The receiver is very easy to add, I will cover it in the next section. Another post has more information, including PyLIRC, and a schematic,

Using the Raspberry Pi as an IR receiver

This is pretty much the easy part. You can get an IR receiver and hook it up to the 3.3V pin, the ground and an input GPIO pin and LIRC will learn your remote’s signals so you can transmit them with your transmitter above.

There is some information about the connection on this Adafruit post, a bit more information on this stm labs post, and a whole lot more info in Alex Bain’s post above.

Delving into the actual signals

Things so far have been pretty easy, and not much fun. To learn about how your remote actually works and what the intimate details of the IR protocol are, you can use Ondřej Staněk’s excellent IR protocol analyzer. This little gem will allow you to record IR signals with your sound card, simply by hooking up an IR photodiode/phototransistor up to it, and give you lots of information about the protocol, the timings, what gets transmitted and how.

It has been amazingly helpful in helping me understand how the protocol works, and to be able to work with LIRC files easily by hand, rather than record them automatically.

Given the fact that you can pretty much record IR signals using a sound card, you will be tempted to think that you can connect your IR LED to the sound card’s output and play them back, thus controlling your devices. Well, it turns out you can. I haven’t managed to do it (my LEDs won’t even turn on), but many pages detail it and it has worked for many people.

The LIRC project includes a very good guide on this, with a pretty brilliant technique to double your output by using two leds on opposite sides of the sine wave. meltwater’s post on the Raspberry Pi forums gives some information on how to manually modulate the actual waveform that the sound card should output, and a few other pages corroborate this.

If you know why my LEDs won’t turn on at all with output from the soundcard (not even when playing songs, so it’s a voltage/current thing, rather than a software thing), I would appreciate it if you could answer my StackOverflow question or leave a comment here.

I hope you found this post useful. I’ll write up a new one on my RF transmitter/receiver and my garage door escapades, if the former ever arrives. Follow me on Twitter or use my RSS feed here to be informed when that happens.