Control TV via IR with Arduino & AppleScript Part 1

[Part 1 Learn your IR codes] [Part 2 Control Arduino using AppleScript] [Part 3 Examples and Downloads]

Part 1

What with my exams finished, I thought I’d take some down time and build an IR Remote that I can control with my computer.

I use a 22” LG TV as a display for my MacBook, but because it’s a TV and not a monitor, it doesn’t switch off when I put my computer to sleep. Pressing the on/off button on the front of the TV doesn’t always switch it off (it’s a rubbish button), so often I’ll walk away from my desk, and the TV will still be on. Also, when I use the TV as the display, I no longer have control over the computer volume, OS X sets the volume to max and then prevents me from changing it. This means I have to use the iTunes volume slider, or the TV’s volume buttons.

I wanted to be able to control the TV from my keyboard; volume up and down, access the menus and switch the TV on and off.

Here is an example of what I achieved. In this video, the script (which can also be activated by pressing CMD+F15) asks the user for a time to set the TV’s built in Power-on time.

You Will Need:

  • A microcontroller of some sort (see below for details) and associated USB cable
  • A breadboard
  • Jumper wires
  • 220Ω resistor (because that’s the one I used, whether it’s right or not, who knows!?)
  • Momentary push button switch
  • An IR Receiver
  • An IR LED
  • An LED (for status light (optional))

To get started, you’ll need to get yourself an Arduino, I am currently using an Arduino Uno, but I plan to transfer the sketch and IR LED to an Arduino Mini which should be small enough to fit underneath my keyboard.

The library that we will need to use has some hardware limitations, so choose from one of the following:

  • Teensy 1.0 / 2.0
  • Teensy++ 1.0 / 2.0
  • Sanguino
  • Atmega8
  • Arduino
    • Mega
    • Duemilanove
    • Diecimila
    • Uno
    • LilyPad
    • Mini
    • Fio

The Arduino Micro, for example, does not work. I tried that one, it hated it.

Get Started

The library in question is the IRremote library by Ken Shirriff, and can be found here:

https://github.com/shirriff/Arduino-IRremote

Follow the readme instructions re installation.

Once the library is installed, we’ll need a method of learning the IR commands. To do that, open up the Arduino IDE and select File > Examples > IRremote (or IRremotemaster) > IRrecord:

IDEIRrecordExample

That will open up the IRrecord sketch. The next step is to make sure you have the right board and com port selected, in my case Tools  > Board > Arduino Uno, and Tools > Serial Port > /dev/tty.usbmodem621

It’s always a good idea to check that everything is working as it should be by loading up the Blink example sketch and running that first. If that works, then we’re away! (File > Examples > Basics > Blink).

The next step is to setup our Arduino as per the code. In my case, I made a slight adjustment, I changed:

int RECV_PIN = 11;

to

int RECV_PIN = 2;

as I had already connected it previously.

In the picture below, the Red LED on the right is connected to Pin 13, this is STATUS_PIN.

The odd shaped black component in the middle of the breadboard is the IR receiver pin, this is connected to Pin 2 (or Pin 11 if you haven’t changed it), and is the RECV_PIN.

The clear LED is a superbright 950nm IR LED and is connected to Pin 3 for PWM.

The button on the far left (which I think I actually connected backwards (but it’s alright)) is connected to Pin 12, and is BUTTON_PIN

 

IRrecordArduinoUno

Important!

If like me you are using the TV as a monitor for the computer you are programming the Arduino with, stick some black insulating tape over the IR receiver on your TV. We’ll need to press all the buttons on the remote,  so it would be helpful if the TV didn’t react to any of those button presses.

Also, in my case, I was using a superbright 950nm IR LED, and I found that the red insulating tape I had to hand wasn’t opaque enough to stop the IR signals from getting through, so go for black, and maybe even two layers!

Now, if you’re using an LG TV, you might be able to get away with using my codes, (perhaps not if the TV is older, or even newer than mine). If you don’t have an LG TV, then you’ll have to do this next section manually.

LG IR Codes (at least for my TV) are as follows:

MUTE = 0x20df906f
vUP = 0x20df40bf
vDn = 0x20dfc03f
aUP = 0x20df02fd
aDWN = 0x20df827d
aLFT = 0x20dfe01f
aRGT = 0x20df609f
SELECT = 0x20df22dd
1 = 0x20df8877
2 = 0x20df48b7
3 = 0x20dfc837
4 = 0x20df28d7
5 = 0x20dfa857
6 = 0x20df6897
7 = 0x20dfe817
8 = 0x20df18e7
9 = 0x20df9867
0 = 0x20df08f7
MENU = 0x20dfc23d
RETURN = 0x20df14eb
EXIT = 0x20dfda25
ON/OFF = 0x20df10ef

If you’re TV isn’t an LG, or if those codes don’t work for you, then we’ll have to do it manually.

Compile and upload the IRrecord sketch to your Arduino, if you’ve wired your button like mine then when the Arduino runs the code, you’ll notice that the red LED will blink constantly unless you press and hold the button. I couldn’t be bothered to rewire it, so I left it as it was.

If that is the case, then (with your piece of insulating tape over the TV’s IR sensor), click the serial button on the top right of the Arduino IDE, press and hold the button on the bread board, aim your TV remote at the IR receiver on the bread board and press a button. You should see something like this appear in the serial monitor on the screen:

20DF906F

. . . and that’s it. That is the IR code for the button you pressed. Now open up a spreadsheet or a text editor and paste that next to the name of the button, and carry on. Once you have all the buttons you need, that’s all you have to do with that sketch.

 

That’s the end of part 1. In the next part, we’ll look at sending that code to the TV via the Arduino, and how to control the Arduino using AppleScript.

[Part 1 Learn your IR codes] [Part 2 Control Arduino using AppleScript] [Part 3 Examples and Downloads]

Author: Dan

Share This Post On