Wednesday, March 23, 2016

“No Match for a Good [IR] Blaster at Your Side, Kid”

On Saturday a few jumbled thoughts gelled together into an idea. First, I realized that we'd had quite a lot of fun blinking LEDs with our Raspberry Pi; then it connected that most IR emitters are nothing more than an LED, which the Pi should also be able to drive; finally, since Raspbian is a Linux distribution tuned for nerdy maker types, and Linux already has lots of support for working with IR, it should be pretty easy for us to build a Raspberry Pi contraption to control some of the electronics around the house.

Once I'd thought of this idea, I got really excited about it, since it would be a relatively simple extension of some of the work that we'd already done with the Pi but would perform a completely different kind of task: rather than making a simple visual display, we'd be using the Pi to communicate with other electronic devices already present in our home. As I mentioned last time, part of my interest in the Pi — aside from the sheer fun of messing around with exposed programmable I/O pins — is how working with it can help demystify some of the aura of magic that surrounds the hermetically-sealed, hyper-designed devices prevalent in the world today. Before we even started working, I got the chance to explain to my boys the basic idea of how an IR remote works: how a command on the remote (“power,” say) gets converted to a signal conveyed in a series of light pulses from the LED, how the TV performs this process in reverse to convert
light pulses to a command, and how different pulse patterns can be used to communicate different commands to the target device. Add to that the cool factor of seeing something that we soldered together on the kitchen table control a sophisticated piece of consumer electronics, and it was clear that we had a winner of a project on our hands!

For this project I elected to use the same “hello Pi” LED circuit from our previous experiments, which is simply the emitter LED with a current limiting resistor, 100 Ω this time as I wanted minimal current restriction:


Depending on the emitter you're using you might find this results in fairly limited range, and several folks on the web suggest using a transistor circuit to drive the LED from a higher-current power source than the Pi's GPIO pins. It turns out I had a low power emitter/detector pair on hand (from RadioShack!), and, since I wasn't especially concerned with range, we went with the simple route.


At first I thought about programming the emitter directly, but a blog post or two quickly dissuaded me from this idea. It turns out that driving a 38kHz signal from an unsupported user program is difficult or impossible in Linux, so rather than get stuck on fighting with that bit, I elected to take advantage of the vast resources afforded by the community, which had already solved this problem in the form of LIRC, the Linux IR Remote Control project. Not only is there excellent support for IR in general on Linux, some kind soul had already concocted an LIRC driver that works with the Pi's GPIO pins, which is available in the latest Raspbian release with a simple

    sudo apt-get install lirc

to install both LIRC itself and the lirc_rpi driver. The driver allows you to wire simple, readily available IR components to the Pi's GPIO header for a quick, easy, and cheap programmable IR device.

Next we needed to solder the emitter to its wiring. This time I had Asher (7) do the soldering for us: I held the joints, he heated them, and I applied the solder. With some strong warnings and lots of careful guidance, he was able to desolder one joint — we repurposed a wiring harness from our last project — and solder the three new connections we needed without burning himself, me, his brother, the house, or anything else that I could detect. Success! His mother tells me that this experience thrilled and encouraged him, which is the best possible outcome from a project like this: delighting and empowering someone else.




Getting LIRC set up on the Pi is fairly easy thanks to this guide, just make sure you adjust the gpio_out_pin arguments to match the pin you're planning to connect the emitter to (in our case GPIO 17, i.e. physical pin 11). A couple of additional notes on working with LIRC:
  • the version of LIRC packaged in Raspbian is fairly old, so some features mentioned in the LIRC documentation do not work. One specific gotcha is that the support for multiple config files in /etc/lirc/lircd.conf.d/ isn't implemented in this version, so if you want to emulate multiple devices you'll need to combine the device definitions yourself. The include directive is implemented, which is what I used to do this.
  • LIRC's configuration parser is pretty picky — for example, comment lines cannot begin with leading whitespace — and ferreting out configuration problems can be a challenge when the daemon is started automatically at boot. I found it helpful to run LIRC in the foreground (lircd -n) while I messed with the conf files until it was happy.
  • With the default install, restarting LIRC (/etc/init.d/lirc restart or stop then start) would not bring things back up correctly: restarting left irsend unable to find the running lircd. I'm sure this is a simple configuration mismatch somewhere, but my low-tech solution was to simply reboot the Pi when I got the configuration the way I wanted it.
Fiddling around with config files isn't very engaging, so I took over the next bit: finding remote control definition files for the remotes we wanted to emulate. The LIRC project has collected a large number of device definitions from users around the web, and we were able to find a direct match for the remote control for our TV. After some experimentation we also found a remote definition that would talk to the receiver using a slightly different model number than is printed on the actual remote. LIRC also supports reading signal codes using a receiver circuit, but we have not tried that yet.

Now the moment of truth: I held the emitter so it was pointed at the TV while Asher ran the command

    irsend SEND_ONCE samsung KEY_POWER

from the terminal. We both had huge grins as the TV's standby indicator light winked off while it went through its power up sequence and finally ignited the panel. We goofed around running a few more commands at the TV and receiver before declaring this project a success.

What are we going to do next with our IR blaster? Who knows, although I must admit to being tempted by this kit from Adafruit, which, while not a Raspberry Pi project, connects to these same concepts and provides excellent guidance on basic electronics skills. But even if nothing else comes of it, I'm satisfied at completing another project that engaged my children's imagination, empowered them with new knowledge, and encouraged them to explore their interests courageously.

1 comment: