Arduino - Control LEDs With a Remote Control

165,587

105

47

About: Author, Blogger, Electronics Enthusiast and Entrepreneur. For complete Electronics Projects and Tutorials go to: https://RandomNerdTutorials.com

Intro: Arduino - Control LEDs With a Remote Control

Hi guys,
I'm sharing with you a project I've recently done. I'ts about how you can control some LED's with a remote control and your Arduino. But you can apply this to any electronic device such as motors , lights for example.


you can visit my website for more electronic projects, interesting news and tips:
http://randomnerdtutorials.com/


Step 1: IR Library

First you need to go to this page and download the IR library. You just need to follow the read me file to install. I think that any IR remote control can do this, I’ll be using a Philips Universal one.

Step 2: Parts Required

Parts list:
1x Arduino
1x Breadboard
1x Remote control
1x IR receiver ( I’ll be using TSOP4838)
4x LED’s
4x 220ohm resistors
Jumper cables

The infrared receiver has 3 pins:
First pin: Vout, outputs HIGH when no signal is present and LOW when a mark is received.
Second pin: GND.
Third pin: Vcc.

Step 3: Circuit Diagram

You need to wire your circuit something like this.
And then I went to the arduino IDE > file>examples>IRremote> IRrecvDemo. You need to upload the sketch to your arduino, open the serial monitor and start using your remote control and see which values the arduino is receiving.

After a while I’ve wrote down which values appear in the serial monitor when you press the volume up key or any other key, and write it down for all the keys you want to use. And they were:

Power:  E240
Forward: E250
Reverse: E248
Volume+: E244
Volume-: E254
Mute: E24C7
You will need to convert these hexadecimal numbers to decimal, you can use this tool for that.

Step 4: Upload the Arduino Code

Upload this code

/*
* IRremote Library - Copyright 2009 Ken Shirriff
* created by Rui Santos, http://randomnerdtutorials.wordpress.com
* Control LED's with a remote control
* 2013
*/

#include <IRremote.h>

int IR_Recv = 3;   //IR Receiver Pin 3
int g_ledPin = 5;  //green LED pin 5
int y_ledPin = 6;  //yellow LED pin 6
int r_ledPin = 9;  //red LED pin 9
int b_ledPin = 10; //blue LED pin 10
int ledPins[] = {5, 6, 9, 10};  //array with all the LED's pins
int ledStates[] ={0, 0, 0, 0};  //this means the LED's states at first is 0 = LOW
int i=0;  //LED index for the arrays

IRrecv irrecv(IR_Recv);
decode_results results;

//variables to make the LED blink when selected
int ledState = LOW;             // ledState to turn the LED on or off
long previousMillis = 0;        // stores last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup(){
  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Starts the receiver
  pinMode(g_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(y_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(r_ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(b_ledPin, OUTPUT);      // sets the digital pin as 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 57936: //when you press the Forward button
       //this if/else statement makes sure that LED is ON or OFF before move to the next LED
       if(ledStates[i]==0)
          digitalWrite(ledPins[i], LOW);
       else
          digitalWrite(ledPins[i], HIGH);
       Serial.println("Next LED");
       //makes sure that when we reach the last LED it goes to the first LED again
       if(i>=3)
          i=-1; 
       i+=1;             
       break;
       
      case 57928: //when you press the Reverse button
        //this if/else statement makes sure that LED is ON or OFF before move to the previous LED
        if(ledStates[i]==0)
          digitalWrite(ledPins[i], LOW);
        else
          digitalWrite(ledPins[i], HIGH);
        Serial.println("Previous LED");
        //makes sure that when we reach the first LED it goes to the last LED
        if(i<=0)
          i=4;
        i-=1;
        break;
       
      case 57932: //when you press the Mute button
        if(ledStates[i]==0){ //if the LED is off, It will turn on
          Serial.println("Turns ON the LED Selected");
          digitalWrite(ledPins[i], HIGH);  //sets the LED on
          ledStates[i]=1;                  //updates the LED state
        }
        else{
          Serial.println("Turns OFF the LED Selected"); //else: the LED is on, It will turn off
          digitalWrite(ledPins[i], LOW);   //sets the LED off
          ledStates[i]=0;                  //updates the LED state
        }       
        break; 
       
      case 57920: //when you press the Power button
        Serial.println("Turns OFF all the LED's");
        digitalWrite(g_ledPin, LOW);   // sets the green LED off
        ledStates[0] =0;               // updates the LED state
        digitalWrite(y_ledPin, LOW);   // sets the yellow LED off
        ledStates[1] =0;               // updates the LED state
        digitalWrite(r_ledPin, LOW);   // sets the red LED off
        ledStates[2] =0;               // updates the LED state
        digitalWrite(b_ledPin, LOW);   // sets the blue LED off
        ledStates[3] =0;               // updates the LED state
        break; 
       
      default:
        Serial.println("Waiting");
    }
    irrecv.resume(); // Receives the next value from the button you press
  }
  //this if statment makes the LED blink if it's selected and off
  if(ledStates[i]==0){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;  
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPins[i], ledState)
    }
  }
}

Step 5: And It's Done

Thanks for reading, let me know what you think about my projects, leave a comment and don't forget to visit my website for more tutorials.

http://randomnerdtutorials.com/

<iframe width="560" height="315" src="http://www.youtube.com/embed/lQ7K8Jp3Jns" frameborder="0" allowfullscreen></iframe>

Share

Recommendations

  • Audio Contest 2018

    Audio Contest 2018
  • Electronics Tips & Tricks Challenge

    Electronics Tips & Tricks Challenge
  • Plastics Contest

    Plastics Contest

47 Discussions

0
None
markm285

1 year ago

So the error i get is that only one LED lights up out of the 4. I've tried changing the other leds out and the pins they're connected to but no difference. the arduino does receive the IR signal but it doesn't seem to be doing anything to the led that lights up. in the serial moniter, the IR signals look something like :

"21448776745 waiting"

1 reply
0
None
Christo6911

3 years ago on Step 4

LEDREMOTE.ino: In function 'void loop()':

LEDREMOTE.ino:112:5: error: expected ';' before '}' token

Multiple libraries were found for "IRremote.h"

Used: C:\Program Files (x86)\Arduino\libraries\RobotIRremote

Not used: C:\Users\SERVER\Documents\Arduino\libraries\Arduino-IRremote-master

1 reply
0
None
agmhereChristo6911

Reply 2 years ago

I also got the same error.

Add ';' at the end of line 111 - 'digitalWrite(ledPins[i], ledState);'.

It's too late for you but hope it helps someone else with same error.

0
None
DesmondM

2 years ago

Hi der, I've found a exciting website for dis.

http://vaaiibhav.me/how-to-use-arduino-with-infrared-remote-control/

0
None
Alok Shaw

2 years ago

am using TV remote to control my motor but i am getting an error can anyone fix it.

Also, after long pressing the button getting , i am getting a no. repeated for long press ,which is different than the first one.

#include <IRremote.h>

//ONKYO Remote

int RECV_PIN = 11; //Pin 11 is for the IR remote

IRrecv irrecv(RECV_PIN);

decode_results results; // The name of the decode_results function

int led = 13; // Pin 13 is for the builtin

LED on the arduino

const int Motor1Pin1 = 7; // Pins for the motors so 7, 6, 5, 4

const int Motor1Pin2 = 6;

const int Motor2Pin1 = 5;

const int Motor2Pin2 = 4;

void setup() {

irrecv.enableIRIn();

pinMode(led, OUTPUT); // OUTPUT the LED

pinMode(Motor1Pin1, OUTPUT); // OUTPUT the motor pins

pinMode(Motor1Pin2, OUTPUT);

pinMode(Motor2Pin1, OUTPUT);

pinMode(Motor2Pin2, OUTPUT);

}

void loop() {

digitalWrite(led, HIGH); // When the arduino is ready to get commands the LED will turn on

if(irrecv.decode(&results)) { // if the arduino receives a signal from the remote...

irrecv.resume();

if(results.value == 0x4BB641BE) { // Here's where you need to change the value from

4BB641BE to the one in your serial monitor but always put 0x before the value (the number zero

and the letter x)

moveForward(); // move forward

}

else if(results.value == 0x4BB6C13E) { // Change the value for the Backward

moveBackward();

}

else if(results.value == 0x4BB621DE) { // Change the value for the Left

moveLeft();

}

else if(results.value == 0x4BB6A15E) { // Change the value for the Right

moveRight();

}

else if(results.value == 0x4BB6E916) { // Change the value for the Stop

Stop();

}

}

}

// voids for all the mouvement. If it doesnt work for you please feel free to change it

void moveForward() {

digitalWrite(Motor1Pin2, HIGH);

digitalWrite(Motor1Pin1, LOW);

digitalWrite(Motor2Pin2, LOW);

digitalWrite(Motor2Pin1, HIGH);

}

void moveBackward() {

digitalWrite(Motor1Pin1, HIGH);

digitalWrite(Motor1Pin2, LOW);

digitalWrite(Motor2Pin1, LOW);

digitalWrite(Motor2Pin2, HIGH);

}

void moveLeft() {

digitalWrite(Motor1Pin1, LOW);

digitalWrite(Motor1Pin2, HIGH);

digitalWrite(Motor2Pin2, HIGH);

digitalWrite(Motor2Pin1, LOW);

}

void moveRight() {

digitalWrite(Motor1Pin2, LOW);

digitalWrite(Motor1Pin1, HIGH);

digitalWrite(Motor2Pin1, HIGH);

digitalWrite(Motor2Pin2, LOW);

}

void Stop(){

digitalWrite(Motor1Pin2, LOW);

digitalWrite(Motor1Pin1, LOW);

digitalWrite(Motor2Pin1, LOW);

digitalWrite(Motor2Pin2, LOW);

}

0
None
electric1981

2 years ago

If anyone is having trouble with getting this to compile go to your IRlibrary folder and delete the robot files then it will work thats what i did and works great

0
None
JunadA

3 years ago

I'm confused cos I'm a newb. Is the numbers after 'case' you know the 'case 59732'. are those the decodings of the remote ir signal?

0
None
maitrteya

3 years ago on Step 5

Arduino: 1.6.6 Hourly Build 2015/07/21 05:42 (Windows 7), Board: "Arduino Uno"

IRrecvDemo.ino: In function 'void setup()':

IRrecvDemo:17: error: redefinition of 'void setup()'

sketch_jul26d:1: error: 'void setup()' previously defined here

IRrecvDemo.ino: In function 'void loop()':

IRrecvDemo:23: error: redefinition of 'void loop()'

sketch_jul26d:6: error: 'void loop()' previously defined here

Multiple libraries were found for "IRremote.h"

Used: D:\maitreya\Arduino\arduino-nightly\libraries\RobotIRremote

Not used: C:\Users\Maitru_2\Documents\Arduino\libraries\Arduino-IRremote-master

redefinition of 'void setup()'

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

waht to do with this

0
None
maitrteya

3 years ago on Introduction

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"

In file included from sketch_jul26b.ino:23:

C:\Program Files (x86)\Arduino\libraries\RobotIRremote/IRremoteInt.h:187: error: 'RAWBUF' was not declared in this scope

what is the meaning of this

there comes this error

0
None
ahmedH22

3 years ago on Introduction

hey, I tried your project and it worked, but I have a little issues related to the distance at which I use my remote. The LEDs can only be switched at really short distances for me. Is it the same with you or my receiver isn't powerful enough ?

0
None
kavish laxkar

3 years ago on Step 5

in this we have to just change the received value from ir after the word "case...like case 1535313823"...or to do sometng else..? plz help .

collect2: error: ld returned 1 exit status

Error compiling.

please help:( i hv no idea what that means

Hi,when I want use serial monitor to see which values the arduino is receiving.

But I cannot see any value returned. Is that because I don't use the philips remote?

0
None
Ramunekaz

3 years ago on Step 4

hi when i copy your code i get:

ir-led.ino: In function 'void loop()':

ir-led.ino:105:5: error: expected ';' before '}' token

Error compiling.

2 replies
0
None
Christo6911Ramunekaz

Reply 3 years ago

That mean you have a syntax error on line 105...

In the IDE, click on settings and activate "line number" option... Go to the line 105 and fix ot

0
None
nazir97

3 years ago on Introduction

C:\Program Files (x86)\Arduino\libraries\RobotIRremote\IRremoteTools.cpp:5: error: 'TKD2' was not declared in this scope

what is it asking for and how can I fix it