Universal Remote Control using Android and Arduino (Part 2)

Onward to the Arduino bits…

So the Arduino’s role in this is to act as a “device controller”, that is, it is the bit that interfaces between the Android and the other devices you wish to control. In this post, I’m going to focus on the IR bits, and how to talk to, and control IR devices such as your TV, or a ceiling fan, or whatever. In the next post I’ll add more code, showing you how to talk with the Android Device, and then finally, how to talk to other Controllers on your “Device Network”. Finally, I’ll show you the Android code I used to tie it all together.

Before I get too deep, I want to thank Ken Shirriff for his AMAZING arduino IR Library. Without this, I wouldn’t have pulled this of so well. Check it out at: http://www.pjrc.com/teensy/td_libs_IRremote.html. Thanks Ken!

See the following image to show you how to connect up your IR controller.

 

 

We have an IR detector wired up to pin 2, and an IR Transmitter wired up in pin 13. The actual pins you use aren’t important,  cause you set them in code, but remember to observe the correct polarity for your receiver and transmitter.

Lets look at some code; all it does is toggle the power for my TV and Cable box. I’ve used two different protocols so show you an example of each. There are more too.

//* Monster Remote Processor Code
//* 06/27/2012  Jerry Parker
//* An Arduino IR transmitter that listens for commands via Bluetooth
//* with an LCD screen for echoing information back to Human.
//* Purpose is to listen for commands from an Android or similar device,
//* allowing that android device to control IR devices via bluetooth.
//* Intent is to be used with an android application as a
//* Universal Remote Control.
// load Kens’ awesome library
#include                      //IR Library
#include                //IR Library

//connect IR Reciever and Transmitter
#define irInPin                                11
#define irOutPin                             3

// here is a sample raw data string to turn on/off my cable box
unsigned int TimeWarner[35] = {9000,4550,500,2250,500,4550,500,2250,500,4550,500,2250,500,2250,500,2250,500,2250,500,
2250,500,2250,500,2250,500,2250,500,2250,500,4550,500,4550,500,2250,500};

// and here is a hex value to turn on/off my tv
#define SamsungPowerToggle 0xB4B46C93

void setup() {
Serial.begin(9600);              // Debug port
irrecv.enableIRIn();             // Start the receiver
// now for the magic. Send the samsung tv  the power toggle; we send 3 times to be sure it is recieved
for (int i = 0; i < 3; i++) {
irsend.sendSAMSUNG(SamsungPowerToggle, 32); // Samsung TV
delay(100);
}
// and toggle the Cable box too!
for (int i = 0; i < 3; i++) {
irsend.sendRaw(TimeWarner,35,35); // Raw data to Cable box
delay(100);
}
void loop() {
}

That’s it! Pretty simple to send IR commands out of the Arduino using Ken’s Library. The magic here is that we can save the data in a string, and we can call different protocols. The next code snippit shows you exactly how to do that! We’ll take the above code, and add stuff to it:

void setup() {
Serial.begin(9600); // Debug port
irrecv.enableIRIn(); // Start the receiver

// now for the magic. incorporate a routine to handle devices and activities
SendIRData(1);
SendIRData(2);
SendIRData(3);

}
void loop() {
}
void SendIRData(int val) {
switch (val) {
// call with a 1 to turn on/off cable box
// call with a 2 to turn on off tv
// call with a 3 to toggle both
case 1:
for (int i = 0; i < 3; i++) {
irsend.sendRaw(TimeWarner,35,35); // Raw data to Cable box
delay(100);
}

case 2:
for (int i = 0; i < 3; i++) {
irsend.sendSAMSUNG(SamsungPowerToggle, 32); // Samsung TV
delay(100);
}

case 3:
for (int i = 0; i < 3; i++) {
irsend.sendSAMSUNG(SamsungPowerToggle, 32); // Samsung TV
irsend.sendRaw(TimeWarner,35,35); // Raw data to Cable box
delay(100);
}
}

So, you can see that we can now control individual devices, or an activity, which is controlling multiple devices with one command, by calling SendIRData with an argument. Next week, I’ll flesh out more Arduino code, showing you how to incorporate GetRawData and Get IR so that you can “Learn” from your IR Remote!

C-Ya!

Leave a Reply