Arduino – Control LEDs with IR Remote Control

In this project you’ll use an infrared (IR) receiver and an Arduino to control 3 LEDs with a remote control. This is useful to re-use old remote controls or give some functionally to some of your remote’s buttons.

This project is divided into two parts:

  1. You’ll decode the IR signals transmitted by your remote control
  2. You’ll use that info to perform a task with your Arduino (control 3 LEDs)

Parts required

To follow this project you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Introducing the Infrared (IR) Receiver

The infrared receiver is the component shown in the figure below. This is the TSOP4838.

IR receiver labels

Infrared receiver pins:

  • First pin: Vout
  • Second pin: GND
  • Third pin: Vcc

When you press your remote control, it sends infrared modulated signals. These signals contain information that your receiver collects.

Each button sends specific information. So, we can assign that information to a specific button.

Decode the IR signals

In this part of the project you need to decode the IR signals associated with each button.

Schematics

Connect the IR receiver accordingly to the schematics below.

Code

To control the IR receiver, you need to install the IRremote Library  in the Arduino IDE.

Installing the IRremote library

  1. Click here to download the IRremote library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get IRremote-master folder
  3. Rename your folder from IRremote-master to IRremote
  4. Move the IRremote folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Copy the following code to your Arduino IDE, and upload it to your Arduino board. Make sure that you have the right board and COM port selected.

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int RECV_PIN = 11;

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
  }
  delay(100);
}

View raw code

Open the serial monitor at a baud rate of 9600.

In this project you want to control 3 LEDs. Choose 6 buttons for the following tasks:

  1. LED1 – ON
  2. LED1 – OFF
  3. LED2 – ON
  4. LED2 – OFF
  5. LED3 – ON
  6. LED3 – OFF

Press, for example, the button number 1 of your remote control. You should see a code on the serial monitor. Press the same button several times to make sure you have the right code for that button. If you see something like FFFFFFFF ignore it, it’s trash.

Do the same for the other buttons.

Write down the code associated with each button, because you’ll need that information later.

Building the final circuit

In this part, you’ll build the circuit with three LEDs that will be controlled using your remote.

Schematics

Assemble all the parts by following the schematics below.

Code

Now, grab the codes you captured in the previous step. You need to convert your codes from hex to decimal.

For that, you can use the following website: www.binaryhexconverter.com/hex-to-decimal-converter

Here’s a conversion example for one of my codes:

Repeat that process to all your hex values and save the decimal values. These are the ones you need to replace in the code below.

Download or copy the following sketch to your Arduino IDE. Write your own decimal values in the sketch provided in the case lines and upload it to your Arduino board. Make sure that you have the right board and COM port selected.

/*
 * Modified by Rui Santos, http://randomnerdtutorialscom
 * based on IRremote Library - Ken Shirriff
*/
 
#include <IRremote.h>
 
int IR_Recv = 11;   //IR Receiver Pin 3
int bluePin = 10;
int greenPin = 9;
int yellowPin = 8;
 
IRrecv irrecv(IR_Recv);
decode_results results;
 
void setup(){
  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Starts the receiver
  pinMode(bluePin, OUTPUT);      // sets the digital pin as output
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(yellowPin, OUTPUT);      // sets the digital pin as output 

}
 
void loop(){
  //decodes the infrared input
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(results.value);
    //switch case to use the selected remote control button
    switch (results.value){
      case 551520375: //when you press the 1 button
        digitalWrite(bluePin, HIGH);
        break;   
      case 551495895: //when you press the 4 button
        digitalWrite(bluePin, LOW);   
        break;
       case 551504055: //when you press the 2 button
        digitalWrite(greenPin, HIGH);
        break;           
       case 551528535: //when you press the 5 button
        digitalWrite(greenPin, LOW);
        break;       
       case 551536695: //when you press the 3 button
        digitalWrite(yellowPin, HIGH);
        break;       
       case 551512215: //when you press the 6 button
        digitalWrite(yellowPin, LOW);
        break;
    }
    irrecv.resume(); // Receives the next value from the button you press
  }
  delay(10);
}

View raw code

Demonstration

In the end you can control each LED individually using the buttons of your remote control.

Watch the video demonstration

Wrapping Up

This is a great project to learn about the IR receiver. There are endless possibilities for what you can do with it.

For example, you can replace those LEDs with a relay to control your house appliances.

This can be particularly useful because some remotes have a bunch of buttons that you never use. So, why not use them to do something useful?

This is an excerpt from our course: Arduino Step-by-step projects. If you like Arduino and you want to build more projects, we recommend enrolling in the Arduino Step-by-step projects course.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

63 thoughts on “Arduino – Control LEDs with IR Remote Control”

  1. Rui

    Na compilaçao da o seguinte erro

    sketch_apr13a.ino: In function ‘void loop()’:
    sketch_apr13a:112: error: expected `;’ before ‘}’ token

    Nao consigo resolver, pode me ajudar

    Reply
    • quer dizer que falta um “;” antes do }. isto quer dizer que ou sem querer apagaste um “;”
      ou nao copiaste o código todo, tenta ver se tem os ponto e virgulas todos e todos os {}

      Reply
    • In theory i think any receiver should work. But some people is having some trouble when they try to make this project with a 2 Legs IR receiver,
      So if you want to make this project and I recommend you to use any 3 legs IR receiver and you should be fine

      Reply
  2. cant get this to work with the arduino motor shield, please help.

    #include

    int IR_Recv = A8; //IR Receiver Pin 3
    int pinI1=8;//define I1 interface
    int speedpinA=9;//enable motor A
    int speedpinB=10;//enable motor B
    int pinI2=11;//define I2 interface
    int pinI3=12;//define I3 interface
    int pinI4=13;//define I4 interface
    int spead =125;//define the spead of motor default is 125

    IRrecv irrecv(IR_Recv);
    decode_results results;

    void setup() { //Set’s up the code to be ran from a fresh start this code only runs once
    // initialize serial communication output
    Serial.begin(9600);// comment this out when not debugging
    irrecv.enableIRIn(); // Starts the receiver
    //set pinmodes for motor controllers first run
    pinMode(pinI1,OUTPUT);
    pinMode(pinI2,OUTPUT);
    pinMode(speedpinA,OUTPUT);
    pinMode(pinI3,OUTPUT);
    pinMode(pinI4,OUTPUT);
    pinMode(speedpinB,OUTPUT);

    }

    void loop(){

    //decodes the infrared input
    if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(decCode);
    //switch case to use the selected remote control button
    switch (results.value){

    case 4064352766: //when you press the Forward button
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
    digitalWrite(pinI1,HIGH);

    Serial.println(“forward”);

    break;
    case 3028878891: //when you press the Reverse button
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
    Serial.println(“backward”);

    break;
    case 3308754100: //when you press the left button
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
    digitalWrite(pinI3,LOW);
    digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
    digitalWrite(pinI1,LOW);
    Serial.println(“Spin Left”);

    break;
    case 197779206: //when you press the right button
    analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
    digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
    digitalWrite(pinI3,HIGH);
    digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
    digitalWrite(pinI1,HIGH);
    Serial.println(“Spin Right”);

    break;
    case 1974032039: //when you press the select button
    digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
    digitalWrite(speedpinB,LOW);
    Serial.println(“Stopping”);

    break;
    default:
    Serial.println(“waiting”);
    }

    irrecv.resume(); // Receives the next value from the button you press
    }

    }

    Reply
  3. when using the serial monitor and watching the leds on the motor shield it appears everything works execpt the analog.write commands as there is no movement of the motors.

    Reply
    • the code seems correct for me… and if it’s working with the arduino serial monitor…
      Does your IR receiver works with a simple project?
      test a simple Control one LED or something… and see if its working.

      I hope this helps.

      Reply
  4. hy

    i wanted to try this a few days ago and found your ir per accident cause of an other article ^^

    hm i wrote the programmer of the library also but not sure WHEN i get an answer so maybe you also have an idea

    when i include it under c/…/arduino/…/libraries and just want to rund an example from the library i get this errors:

    In file included from sketch_dec09a.ino:13:
    C:\Program Files\Arduino\libraries\IRremote/IRremoteInt.h:87: error: ‘uint8_t’ does not name a type
    C:\Program Files\Arduino\libraries\IRremote/IRremoteInt.h:88: error: ‘uint8_t’ does not name a type
    C:\Program Files\Arduino\libraries\IRremote/IRremoteInt.h:89: error: ‘uint8_t’ does not name a type
    C:\Program Files\Arduino\libraries\IRremote/IRremoteInt.h:92: error: ‘uint8_t’ does not name a type
    sketch_dec09a.ino: In member function ‘void IRsendDummy::useDummyBuf()’:
    sketch_dec09a:87: error: ‘volatile struct irparams_t’ has no member named ‘rcvstate’
    sketch_dec09a:88: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:93: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:98: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:104: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:109: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:113: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’
    sketch_dec09a:114: error: ‘volatile struct irparams_t’ has no member named ‘rawlen’

    well im not a too good programmer to get into the library itself and find out whats wrong

    do you ahve any experince or knowledge which can solve my trouble
    i use atm an arduino 1.04 software

    thx

    Reply
    • thanks for trying my project Vance.

      That library works fine with Arduino IDE 1.04.
      What you need to do is to install the library properly… you probably skipped one step or moved the folder to the wrong place.
      that’s totally normal!

      Please download that entire folder:
      https://github.com/shirriff/Arduino-IRremote

      and install it just like the Readme tells you.

      Let me know if this worked for you,
      Rui

      Reply
      • hm under 1.04 i didnt get it running only under 1.5.5 now

        but sadly NOT like in the readme described
        as you might noticed im no native ^^ – so if i understood that correct to only install it under c\..\arduino\libraries

        that didnt work – i also had to install it under userxyz\documents\arduino\libraries\….

        somehow weird but ok it works now

        maybe also for you a worth a test – it gets conflicted with the NewPing library for ultrasonic moduls

        give that an try 😉
        maybe also just an error i receive ^^
        all in all – still an interresting topic to “work” at ^^

        thx for replies

        Reply
        • I noticed that you knew what you were talking about! 🙂
          I wasn’t trying to treat you as a noob, I was just explaining.

          The library works fine for me, with 1.04 and 1.5 IDE versions and it doesn’t conflict either with the NewPing library.
          Sometimes there are errors that we can’t explain, that might be one of them xD

          But Its working now?
          (except the conflict between the NewPing library?)

          Reply
          • yep it works now but still only with 1.5.5 after a few trials (only in doc/lib, only in c/lib, both of them, … ) but after a few tries it works now ^^

            but it doestn work fine
            only if i hit the button estimated 0,3-0,6 sec (just very fast) it reads somehow correct

            if i push the button to long or keep it pressed
            (im talkon about a button on the remote)

            then i only get “FFFFFFFF” values – overflow ?

          • Thats how it works.
            when you press the button it will read one value correct.
            and the other values after that will be “trash”.
            that’s how the library was developed and it’s how it works…

            Your experience may vary if you use a different remote/IR sensor.
            Some sensors will work better then others.

          • ah ok – well – or not well – cause as i said i tried it like in the readme and it didnt work – only when i copied the library in every library folder available :D:D

            now i try it again with a few other methodes out there
            and with the library and 2 other sensor one from an saint smart kit … which doesnt tell what sensor the gave me and a tsop2838 for whom there are nearly good datasheets out there ^^

            ahhhh ok … good to know that the overflow or whatever is normal – thx !

          • Yeah that overflow or random numbers happen to me…
            I don’t know exactly the answer for that.

            Read the Sensor datasheet it will definitely help.

            At Sainsmart you probably find the sensor that came with the kit.
            Usually they have everything detailed on their website.

            If you want more project examples we will find on the web using this library too.
            just search on google or instructables.com.

            Good luck,
            Rui Santos

          • hm good and simple tip.. as i ordered it from an retail via amazon (eu)
            i thought that clumsy documentation is “normal”

            if you want to see a “free” and even more funny english arduino text i can recomend this little projects where i ordered the set

            and here the link to the arduino fiels + “documentation”
            the realy realy tried it and even go a bit in transmission protocoll details and pwm explanation ^^

            enjoy:

            😉

  5. Viva Rui Santos!

    Estou a começar a aprender a utilizar o arduino, e o vosso site parece-me excelente! Gosto muito da maneira como disponibiliza os seus projectos e o site está muito apelativo!

    Estou a pensar em controlar um motor DC com um remote control. No vosso vídeo você diz que posso aplicar tbm a motores, certo?

    Outra pergunta, qualquer comando com IR funciona?

    Reply
    • Olá José,
      Muito obrigado pelo feedback.
      Peço desculpa pela demora a responder, mas tenho tido imenso trabalho.
      Se seguir o meu tutorial, vai começar a perceber melhor como funciona o controlo remoto.
      O primeiro objectivo é ler os valores que o controlo remoto envia, e que o receptor lê.
      Depois de cosneguir codificar esses valores, basta utilizar o meu codigo de arduino e alterar as condiçoes, com os valores que o seu controlo remoto transmite.
      Por fim, alterar as condições if, com o codigo que vai controlar o DC motor.

      Espero ter ajudado,
      Abraço

      Reply
  6. these errors keep happening can you help me

    IRrecvDemo:13: error: expected unqualified-id before ‘=’ token
    IRrecvDemo:15: error: ‘decode_results’ does not name a type
    IRrecvDemo.pde: In function ‘void setup()’:
    IRrecvDemo:20: error: request for member ‘enableIRIn’ in ‘irrecv’, which is of non-class type ‘int’
    IRrecvDemo.pde: In function ‘void loop()’:
    IRrecvDemo:24: error: request for member ‘decode’ in ‘irrecv’, which is of non-class type ‘int’
    IRrecvDemo:24: error: ‘results’ was not declared in this scope
    IRrecvDemo:26: error: request for member ‘resume’ in ‘irrecv’, which is of non-class type ‘int’

    Reply
    • You need to install the IR library…
      That is provided in the top of this post.
      Go to github and follow the read me file.

      Thanks for trying my project lester!

      Reply
  7. helo..i have little problem & confuse 😀
    (i’m sory with my english 🙂 )

    i try your code and add Password.h library to cntrol leds, when password true, green led HIGH & pasword false, red led HIGH. Green ledpin = 13, red ledpin = 12.
    Now the problem is when green led HIGH & red led high too, red led did not light like green, the light is very small.

    Thanks…sory with my english 😀

    Reply
    • Hi Eddy,
      Thanks for trying my project.
      That’s a problem with your LED’s…
      try with another LED … are you using an equal resistor for both LED’s?

      Reply
  8. Hi Rui..
    sory late reply..
    yes, i’m using 220ohm resistor for both LED’s. i try different value of resistor for each led but still could not. I’ve tried another led but still not work,,only pin13 normal. 😀

    oh ya, do you have an example code to input password Validation using keypad with 3 attempts??

    sample: If it is entered password incorrectly serial monitor displaying “password error”
    If it is a wrong password user received another 3attempts to enter the correct one. If he couldn’t enter password correctly by these attempts he have to wait 3 minutes time more to re logged in to the system.

    Thanks Rui 🙂

    Reply
  9. hye i wanna ask you….what if I want to use voice to control the LED instead of remote..what should I do?…really need help here..Thank you

    Reply
    • Hi eddie,
      Thanks for asking there’s a good project for that:
      youtube.com/watch?v=0Jr8dKyXrzA

      Please check that project it will help you do exactly that.

      Reply
      • i can not run a project IR remote control with arduino uno coz when compile it saw like this
        c:/program files/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: IRrecvDemo.cpp.elf section .text will not fit in region text
        c:/program files/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: region text overflowed by 50 bytes
        pls help me

        Reply
        • Hi Kong,
          You didn’t install the Arduino library properly.
          Make sure you unzip the library provided and move it to the Arduino IDE libraries folder.
          It should work fine after you do that!
          Thanks for trying my projects

          Reply
          • i not sure properly or not about unzip the library
            //
            in the Arduino IDE libraries folder
            have ir library ready but still erro
            how i can sent picture to you about install arduino library.

  10. Hi Rui….
    I wanna do this project and I would like to do in PCB….still I’ve not get the exact circuit…Can you please send me…

    Reply
  11. hi. Rui santos.

    I would like to take remote code air conditioner merk LG , but when sent over IR send the air conditioner was not working ..
    and when tried on TV merk SHARP, IR send its success ..

    Reply
  12. Esto es realmente genial, eres un blogger muy profesional. Me he unido a tu RSS y deseo encontrar más cosas en este gran blog. Además, !he compartido tu sitio en mis redes sociales!

    Saludos

    Reply
  13. For the info let me add instead of using a hexadecimal to Decimal converter you can simply change Serial.printIn(results.value,DEC);

    Reply
  14. I followed this tutorial, there were compile errors referred to the file IRremote.cpp. I did the corrections and there were no errors. I checked with NEC, panasonic and another (Sky). How can we know the format of the IR code from this sketch. I added another Serial.print to output the decimal value.

    Reply
  15. I followed exactly what you did and controlled the LED using my arduino board first time. it is so simple. Thank you so much. Keep helping us.

    Reply
  16. Sara,

    All I get when running the sketch in this tutorial is:

    The function decode(&results)) is deprecated and may not work as expected! Just use decode() – without any parameter.
    The function decode(&results)) is deprecated and may not work as expected! Just use decode() – without any parameter.
    The function decode(&results)) is deprecated and may not work as expected! Just use decode() – without any parameter.
    The function decode(&results)) is deprecated and may not work as expected! Just use decode() – without any parameter.

    checked wiring, OK.
    used decode(): monitor completely blank.

    Please help.

    Alan

    Reply
    • Hi Sara,

      I followed your instructions to download the ‘Ir Remote’ library.

      When I got the result shown above I did some research. Went onto Arduino forum & found a solution.

      Then I modified your code as shown below. The decoded values for numbers 1 – 6 are 0 -5.

      This I used in the switch statement.

      The code now works perfectly.

      But why:

      (1) Did the original code not work on my system.

      (2) Why do I now get single digit codes?

      Keep up your great work.

      Stay safe

      Alan

      /*
      * Modified by Rui Santos, http://randomnerdtutorialscom
      * based on IRremote Library – Ken Shirriff
      */

      #include <IRremote.h>

      const byte IR_RECEIVE_PIN = 11;
      int IrReceiverValue;
      int bluePin = 10;
      int greenPin = 9;
      int yellowPin = 8;

      void setup(){
      Serial.begin(9600); //starts serial communication
      IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

      pinMode(bluePin, OUTPUT); // sets the digital pin as output
      pinMode(greenPin, OUTPUT); // sets the digital pin as output
      pinMode(yellowPin, OUTPUT); // sets the digital pin as output
      }

      void loop(){
      //decodes the infrared input
      if (IrReceiver.decode()){
      IrReceiverValue = IrReceiver.decodedIRData.command, HEX;

      //switch case to use the selected remote control button
      switch (IrReceiverValue){
      case 0: //when you press the 1 button
      digitalWrite(bluePin, HIGH);
      break;
      case 1: //when you press the 4 button
      digitalWrite(bluePin, LOW);
      break;
      case 2: //when you press the 2 button
      digitalWrite(greenPin, HIGH);
      break;
      case 3: //when you press the 5 button
      digitalWrite(greenPin, LOW);
      break;
      case 4: //when you press the 3 button
      digitalWrite(yellowPin, HIGH);
      break;
      case 5: //when you press the 6 button
      digitalWrite(yellowPin, LOW);
      break;
      }
      IrReceiver.resume(); // Receives the next value from the button you press

      }
      delay(10);
      }

      Reply
  17. My IR reciever blew off when i gave 5v from the board.Plz change the value to 3.3v if not someone less will also face the trouble too.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.