Installing LIRC on the Pi was a snap, but took about 30 minutes as there was an update to Raspian that needed to be downloaded.
I removed the IR receiver from the LED light’s circuit board, wired it to the Pi, followed the instructions for setting up a new receiver and remote, and pressed all the buttons asked of me by irrecord.
My /etc/lirc/lircd.conf file for the APA 1616 remote ended up looking like:
begin remote name apa1616 bits 16 flags SPACE_ENC|CONST_LENGTH eps 30 aeps 100 header 8953 4467 one 563 1671 zero 563 555 ptrail 567 repeat 8958 2226 pre_data_bits 16 pre_data 0xF7 gap 107360 begin codes KEY_VOLUMEUP 0x00FF KEY_VOLUMEDOWN 0x807F KEY_POWER 0x40BF KEY_POWER2 0xC03F KEY_A 0x20DF KEY_B 0xA05F KEY_C 0x609F KEY_D 0xE01F KEY_E 0x10EF KEY_F 0x906F KEY_G 0x50AF KEY_H 0xD02F KEY_I 0x30CF KEY_J 0xB04F KEY_K 0x708F KEY_L 0xF00F KEY_M 0x08F7 KEY_N 0x8877 KEY_O 0x48B7 KEY_P 0xC837 KEY_Q 0x28D7 KEY_R 0xA857 KEY_S 0x6897 KEY_T 0xE817 end codes end remote
After irrecord was finished, I hit keys on the remote while at the console, and letters would appear. This is because I named the buttons KEY_A, etc. I didn’t have to run a command to see that I was receiving IR, which was nice.
So, now that I know what the codes are, I should be able to send them to the light using LIRC’s irsend command and have it change color, right? I tried just that.
Okay, time to get out the oscilloscope and see what the IR receiver sends, and what the Pi sends, and make them match. The top is the output of the IR receiver, and what we want our signal to look like. The bottom is what the Pi is doing with its output pin.
As you’ve probably noticed, the signals are very different. The IR receiver starts at 5V, gets pulled down to 0V for some period of time, and then is returned to 5V. The GPIO pin starts at 0V, rises to 3.3V, but rather than staying there, oscillates at some frequency for a period of time. Zooming in on one of the pulses, we see this:
It turns out that these waveforms correspond to a 38KHz carrier frequency that is is a part of the IR communications spec. The IR receiver hardware filters out the carrier. Since that’s the signal the light expects, we need to get Linux to do the same for us.
Changing the signal from active low to active high, and from 3.3V to 5V is fairly straightforward. We just need an NPN transistor. We’ll use the 3.3V GPIO output to drive the base, and put 5V across the collector and emitter. The output to our light gets wired in between the transistor and the 10kOhm resistor.
I discovered an educational program (Yenka) recently that lets you simulate simple circuits, and it’s free for non-commercial home use. I used it to build and test my circuit, and highly recommend it for the novice circuit designer.
This is all great, but what about eliminating the 38KHz carrier?
I got all psyched up to go modify the RPi GPIO driver for LIRC when I noticed that the existing driver had an option that can be changed at load-time.
Bless the author of that module. I don’t have to change any code in order to turn it off. So I made my /etc/modules file look like:
lirc_dev lirc_rpi gpio_in_pin=14 gpio_out_pin=17 softcarrier=0
And I was off to the races. By issuing commands on the Pi, I am now able to simulate remote control button presses, and control the light through software.
irsend SEND_ONCE apa1616 KEY_D
It doesn’t switch colors as fast as I’d like, but I can live with it.
Next: getting the IR filter off the Pi’s camera module without destroying it.