TV Remote and IR Sensor

Control anything remotely with Infrared signals.

When I first started building robots, I wanted to make them remote control. The problem was that I didn’t know a whole lot about how to do that. What I found, though, was a cheap and reliable solution that has been around for some time: Infrared Receiving Diodes. These handy little, $1.95 diodes are simply a matter of plug and play with easy to use software. Just plug the pins into 5v, Gnd, and an Analog or PWM pin and you are good to go. Just be sure to grab a TV remote as you head off to you laboratory…

The software library can be found at Ken Shirriff’s blog. What you do is upload this sketch to your Arduino (I am not sure how well this works with Arduino copies, but I think it is safe to assume that it does) and then open up the serial monitor, aim the remote at it, press some buttons, and read the resulting values.

The only con to this is that you have to have a clear line of sight and as you might imagine the range isn’t all that great.


#include <IRremote.h>

int RECV_PIN = A0; // Analog Pin 0
 IRrecv irrecv(RECV_PIN);
 decode_results results;

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

void loop() {
 if (irrecv.decode(&results)) {
 Serial.println(results.value, HEX);
 irrecv.resume(); // Receive the next value
 }
 }

Now that we have the value that each button corresponds to we can program our robot. As an example we will say that one of our values was XXXXXXXX. At the top of the sketch, but under the library write:


unsigned long value1 = 0xXXXXXXXX; // where XXXXXXXX is on our your remote's values. You need the “0x” in front of it.

Then in the sketch we would have an “if” statement such as


if(results.value == value1) {
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);               // wait for a second
 }

The completed sketch would look like this:


#include <IRremote.h>

unsigned long value1 = 0xXXXXXXXX; // where XXXXXXXX is on our your remote's values.

int RECV_PIN = 11;
 IRrecv irrecv(RECV_PIN);
 decode_results results;
 int led = 13;

// the setup routine runs once when you press reset:
 void setup() {

Serial.begin(9600);
 irrecv.enableIRIn(); // Start the receiver

// initialize the digital pin as an output.
 pinMode(led, OUTPUT);
 }

// the loop routine runs over and over again forever:
 void loop() {

if (irrecv.decode(&results)) {
 Serial.println(results.value, HEX);
 irrecv.resume(); // Receive the next value
 }

if(results.value == value1) {
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);               // wait for a second
 }

}

Or we can use this to control a Servo:


#include <Servo.h>
 #include <IRremote.h>

unsigned long Value2 = 0xXXXXXXXX; // where XXXXXXXX is on our your remote's values. We will call this Value 1
 unsigned long Value1 = 0xXXXXXXXX; // where XXXXXXXX is another button on your remote

int RECV_PIN = 11;
 IRrecv irrecv(RECV_PIN);
 decode_results results;

Servo servo1;

// the setup routine runs once when you press reset:
 void setup() {

Serial.begin(9600);
 irrecv.enableIRIn(); // Start the receiver

// initialize the digital pin as an output.

servo1.attach(10); // attack servo to digital pin 10
 }
 }
 // the loop routine runs over and over again forever:
 void loop() {

if (irrecv.decode(&results)) {
 Serial.println(results.value, HEX);
 irrecv.resume(); // Receive the next value
 }

if(results.value == Value1) {
 servo1.write(179);
 }

if(results.value == Value1) {
 servo1.write(1);
 }

}

The low cost and versatility of these are what make them so great, as well as their ability to take a real beating over time. Available from practically every electronics vendor they will soon make their way into your projects if they have not already.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s