Advertisement
arduino led ir remote schematic

Control LEDs ON/OFF with IR Remote and Arduino

I made this Arduino project that allows to turn ON and OFF LEDs using a cheap IR remote control as a preparation for the next big step: disassemble my girlfriend’s chinese LED lamp and replace its board with and Atmega Tiny microcontroller, probably ATTiny24A and change the stupid remote control with another one.

The project is pretty simple, you don’t need a lot of components. In my case I have used only 3 x 330Ω resistors, 3 leds and a TSOP4838 IR receiver module. You must check the datasheet before use the IR module to see which pins connect to the power supply and which one delivers the output voltage.

I used an available IR Arduino library so it was pretty easy to decode the signals transmitted by the infrared remote. You can download the version that I’ve used using the link below but I advice you to go to the official page and download any updated version. To install, unzip the archive, move the downloaded IRremote directory to: arduino-1.x/libraries/IRremote where arduino-1.x is your Arduino installation directory.

Video presentation of the working project

Arduino IR Remote Control Schematic

arduino led ir remote schematic

Arduino Sketch

/* 
source: www.electroschematics.com
You'll need to change the led pins and the codes 
accordingly to your configuration and IR remote
*/

#include <IRremote.h>

int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero) 
the first zero must remain zero but you can 
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1  63495 // code received from button A
#define code2  30855 // code received from button B
#define code3  22695 // code received from button C

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);   // you can comment this line
  irrecv.enableIRIn();  // Start the receiver
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch(value) {
       case code1:
         if(itsONled[1] == 1) {        // if first led is on then
            digitalWrite(led1, LOW);   // turn it off when button is pressed
            itsONled[1] = 0;           // and set its state as off
         } else {                      // else if first led is off
             digitalWrite(led1, HIGH); // turn it on when the button is pressed
             itsONled[1] = 1;          // and set its state as on
         }
          break; 
       case code2:
         if(itsONled[2] == 1) {
            digitalWrite(led2, LOW);
            itsONled[2] = 0;
         } else {
             digitalWrite(led2, HIGH);
             itsONled[2] = 1;
         }
          break;
       case code3:
         if(itsONled[3] == 1) {
            digitalWrite(led3, LOW);
            itsONled[3] = 0;
         } else {
             digitalWrite(led3, HIGH);
             itsONled[3] = 1;
         }
          break;          
    }
    Serial.println(value); // you can comment this line
    irrecv.resume(); // Receive the next value
  }
}

arduino ir remote control leds photo

You will have to open the serial monitor window and press the remote buttons to find out what code does each button transmitts and then update the sketch to use those codes. You can add more leds but you’ll have to change the code inside the switch() function and add more cases. For example if you want to add one more led add this code after the last break; inside the switch();

       case code4:
         if(itsONled[4] == 1) {
            digitalWrite(led4, LOW);
            itsONled[4] = 0;
         } else {
             digitalWrite(led4, HIGH);
             itsONled[4] = 1;
         }
          break; 

The array itsONled[] = {0,0,0,0} is used to set the initial state of LEDs (in our case OFF) and has number of leds + 1 more zeros, so if you add 2 more leds, then add 2 more zeros (5 leds for a total of 6 zeros).

31 Comments

Join the conversation!

Error! Please fill all fields.
  • your1click

    C:\Users\pradip\Downloads\arduino-nightly-windows\arduino-nightly\libraries\RobotIRremote\src\IRremoteTools.cpp:5:16: error: ‘TKD2’ was not declared in this scope

    int RECV_PIN = TKD2; // the pin the IR receiver is connected to

    ^

    exit status 1
    Error compiling for board Arduino/Genuino Uno.

    • your1click

      Its done already thanks for useful code. and i did one mistake before i forgot to update my library

    • volthauslab

      Not sure what you’re doing there with the TKD2. The IR is connected to your Arduino so the sketch is expecting the pin number on the Arduino to which you’re connecting the IR receiver.

  • carlosfiguerero1gmail-com

    Good morning, thanks a lot!!!! I was searching this proyect all over the net and I find your post and all the comments that help whit the troubleshooting, from Argentina thank you all!!

  • Darcey

    Hello Rohit, is this the code that code that comes with the IRremote. If you use the IRrecvDemo code from the IRremote library the code should work fine. I hope this helps.

  • Rohit Paudel

    I want to see the signals code in serial monitor so i apply this code.

    #include
    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
    }
    }

    BUT,
    it was unable to compile and show some unusual errors. I don’t know as i am not quit familier with this kinds of project.Following error was occour.

    checking_ir_receiver:3: error: ‘IRrecv’ does not name a type
    checking_ir_receiver:4: error: ‘decode_results’ does not name a type
    checking_ir_receiver.ino: In function ‘void setup()’:
    checking_ir_receiver:9: error: ‘irrecv’ was not declared in this scope
    checking_ir_receiver.ino: In function ‘void loop()’:
    checking_ir_receiver:14: error: ‘irrecv’ was not declared in this scope
    checking_ir_receiver:14: error: ‘results’ was not declared in this scope

    What can i do to solve this. I have no idea at all. Please anyone help me !!

  • Perre

    Your code is very very helpful, thank you very much

  • UAG

    I tried it and all works well. Thanx.
    But when i press remote button it does not work very smoothly. Sometimes when i press a button it on and off very nicely but sometimes i have to press a button twice.
    Sometimes when i press button once it on and then off again. Can u suggest what to do.
    Is it the code or tsop ? Problem?

  • Michelle

    Hi! I only have an IR receiver that has 2 pins. How can I connect this to my arduino?

  • Dogan

    What is the maximum distance

  • David Connolly

    For some reason I am finding that many of these IR LED control sketches are not HEX friendly. They compile perfectly when the codes transmitted from the remotes contain only digits but when a alphabetical character is used the ‘not declared in scope’ error arise. Look at it this way, just another opportunity to learn. Thank you for ta,king the time to put this informative article together. David

    • Greg

      Convert the Hex code to Decimal and place the value after the #define code(1,2,3,…..)The serial monitor will display in hex but the microprocessor only understands decimal.
      This project works out great and instructions were easy to follow.

  • Frank

    i took care of the deleting of that suggested by Azem “TKD2 should delete the file IRremoteTools.cpp from Library ( C:\Program Files ” and now im getting a new error

    i even when back and deleted that one depicted on my new screenshot and is not even budging that compile error any suggestions?

Looking for the latest from TI?