Tutorial: How to use the IR remote control with Arduino

On this tutorial, we will show how to use the IR control kit from SparkFun. This kit comes with 2 IR emmiters, 2 IR receivers and a remote control. In this sample, we will turn on a LED when the user push the button. Each button will trigger a different LED.

Here we will implement the code directly, but you can also use the IRRemote library.

Build the following circuit:

Then, upload this code:

int irPin = 2; //Sensor pin 1 wired to Arduino's pin 2
int statLED = 3;
int statLED1 = 4;
int statLED2 = 5;
int statLED3 = 6;
int statLED4 = 7;
int statLED5 = 8;
int statLED6 = 9; //Toggle the status LED every time Power is pressed
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)

void setup() {
pinMode(statLED, OUTPUT);
pinMode(statLED1, OUTPUT);
pinMode(statLED2, OUTPUT);
pinMode(statLED3, OUTPUT);
pinMode(statLED4, OUTPUT);
pinMode(statLED5, OUTPUT);
pinMode(statLED6, OUTPUT);
digitalWrite(statLED, LOW);
digitalWrite(statLED1, LOW);
digitalWrite(statLED2, LOW);
digitalWrite(statLED3, LOW);
digitalWrite(statLED4, LOW);
digitalWrite(statLED5, LOW);
digitalWrite(statLED6, LOW);

pinMode(irPin, INPUT);

Serial.begin(9600);
Serial.println("Waiting: ");
}

void loop() {
int key = getIRKey(); //Fetch the key

if(key != 0) //Ignore keys that are zero
{
Serial.print("Key Recieved: ");
switch(key)
{
case 144: Serial.print("CH Up"); 
if(digitalRead(statLED) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED, HIGH);
else
digitalWrite(statLED, LOW);
break;

case 145: Serial.print("CH Down"); 
if(digitalRead(statLED1) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED1, HIGH);
else
digitalWrite(statLED1, LOW);
break;

case 146: Serial.print("VOL Right");
if(digitalRead(statLED2) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED2, HIGH);
else
digitalWrite(statLED2, LOW);
break;


case 147: Serial.print("VOL Left");
if(digitalRead(statLED3) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED3, HIGH);
else
digitalWrite(statLED3, LOW);
break;


case 148: Serial.print("Mute");
if(digitalRead(statLED4) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED4, HIGH);
else
digitalWrite(statLED4, LOW);
break;


case 165: Serial.print("AV/TV");
if(digitalRead(statLED5) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED5, HIGH);
else
digitalWrite(statLED5, LOW);
break;


case 149: 
Serial.print("Power");
if(digitalRead(statLED6) == LOW) //This toggles the statLED every time power button is hit
digitalWrite(statLED6, HIGH);
else
digitalWrite(statLED6, LOW);
break;


}

Serial.println();
}
}

int getIRKey() {
int data[12];
int i;

while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit

for(i = 0 ; i < 11 ; i++)
data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses

for(i = 0 ; i < 11 ; i++) //Parse them

if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}

int result = 0;
for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
if(data[i] == 1) result |= (1i);

return result; //Return key number
}

The function getIRKey() counts how many LOW pulses the control transmits. Each button sends a different pulse code, so the function decode it and return an integer. Then the main loop function can turn on a LED based on which key was pressed.

That's it!

References:

http://arduino.cc/en/

http://www.arcfn.com/2009_08_01_archive.html

http://www.sparkfun.com/tutorials/291

Views: 7880

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Yes, please share your progress here. Thanks and good"
23 hours ago
FiniousFingers replied to FiniousFingers's discussion New to group
"Thanks, good to know someone is out there. I will be…"
yesterday
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Have you moved forward with any action?  I would be interested in discussing your…"
yesterday
Marcelo Rodrigues replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Ed, No, they didn't deliver yet.  :( Good luck for us all."
Thursday
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
Thursday
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Hey man! A good point to start is reading basic tutorials on Arduino coding and wiring. Then, you…"
Thursday
FiniousFingers posted a discussion

New to group

Couple quick things,I am just starting to learn, or wanting to learn, how to use the Arduino UNO…See More
Thursday
Profile IconDan Urbauer, Bjorn and Jiaming Zhao joined GarageLab (arduino, electronics, robotics, hacking)
Mar 22
FiniousFingers is now a member of GarageLab (arduino, electronics, robotics, hacking)
Mar 20
Erva updated their profile
Mar 16
Erva and Mrs. Priscilla Bonneval are now friends
Mar 16
Profile IconSamantha Dorr, Lee Robinson and Celso Eiju Ito joined GarageLab (arduino, electronics, robotics, hacking)
Mar 16
Edward Evans updated their profile
Mar 15
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Hi Marcelo, I am curious if you have had any resolution on this?  We may be in the same boat…"
Mar 15
Celso Eiju Ito left a comment for Mrs. Priscilla Bonneval
"I just accept your invitation. Thank You for contact me. Sincerelly,   'Eiju"
Mar 15
Mohasin Ahamed Chinnapattan commented on Mauro Assis's blog post How to hack a remote control (and a flying shark) I
"You mentioned collector is set to ground but emitter is set to ground in your diagram "
Mar 14
Profile Iconbadrkhan mijbas khalifa, Bethany Cruz, Mrs. Priscilla Bonneval and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 13
Edward Evans is now a member of GarageLab (arduino, electronics, robotics, hacking)
Mar 9
Profile IconAllan Zilkowsky, Shubham Dewangan, FAUSTO PEREIRA ROMANO and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 1
Dave Lewing is now a member of GarageLab (arduino, electronics, robotics, hacking)
Feb 27

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service