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: 7905

Comment

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

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Igor Golov is now a member of GarageLab (arduino, electronics, robotics, hacking)
Thursday
Marco is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 24
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Thanks for the advice. I will persist with the code till I get it done. thanks once again."
Apr 18
Marcelo Rodrigues commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
""The output waveforms on oscilloscope for Hsync and Vsync and for that matter RGB pins are not…"
Apr 17
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Hi  Marcelo I am trying to make this code work with Attiny 44a operating @ 20 MHz. I have…"
Apr 17
Profile IconReal NOiseux, Muhammad Ali, bob v and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Apr 17
baiqian is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 10
Profile IconDavide and Almas Amandossov joined GarageLab (arduino, electronics, robotics, hacking)
Apr 6
Profile Iconvamoalrenga and John Ferrell joined GarageLab (arduino, electronics, robotics, hacking)
Apr 3
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Yes, please share your progress here. Thanks and good"
Mar 27
FiniousFingers replied to FiniousFingers's discussion New to group
"Thanks, good to know someone is out there. I will be…"
Mar 27
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…"
Mar 27
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."
Mar 23
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
Mar 23
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…"
Mar 23
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
Mar 23
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

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service