Controlling LED Using IR Remote In Arduino Mega

Introduction
 
In this article I will explain about the controlling LED using IR Remote in Arduino Mega. The LED can be turned on/off using the remote control.
 
Parts of list
  • Arduino Mega 2560
  • IR Remote
  • IR sensor 
  • Bread Board
  • Hook Up Wires
  • LED-3.
 
IR Remote and IR Sensor Function:
 
IR Remote:


      Figure 1: IR Remote 
  • Remote is controlling the things at particular places.
  • Remote can work in the form of a signal from the sender to the receiver parts. 
Uses:
  • Television
  • DVD Players
  • Audio Players
 
IR Sensor:

  
       Figure 2: IR Sensor
  • In this sensor is the main part of IR Remote.
  • It will produce the signal to the remote.
  • It can have the three pins Gnd, Vcc, Vin.  
Connections:
 
Connection From the LED to the Arduino Mega 2560:
 
Take the THREE LED pin and fix in the breadboard.
 
First LED Connection:
  • Positive pin = 02
  • Negative Pin = Gnd
Second LED Connection:
  • Positive pin =04.
  • Negative Pin = Gnd 
Third LED Connection:
  • Positive pin =07
  • Negative Pin = Gnd 
Connection From the IR Sensor to the Arduino Mega 2560:
  • The first pin is the Gnd pin connected to the Gnd of the Arduino Mega 2560.
  • The second pin is the Vcc pin connected to the +5v of the Arduino Mega 2560.
  • The third pin is the Vin pin connected to the digital pin 3 to the Arduino Mega 2560. 
Programming:
  1. #include <IRremote.h>  
  2. int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838  
  3. int led1 = 2;  
  4. int led2 = 4;  
  5. int led3 = 7;  
  6. int itsONled[] = {0,0,0,0};  
  7. #define code1  63495 // code received from button A  
  8. #define code2  30855 // code received from button B  
  9. #define code3  22695 // code received from button C  
  10. IRrecv irrecv(RECV_PIN);   
  11. decode_results results;  
  12. void setup()  
  13. {  
  14. Serial.begin(9600);   // you can comment this line  
  15. irrecv.enableIRIn();  // Start the receiver  
  16. pinMode(led1, OUTPUT);  
  17. pinMode(led2, OUTPUT);  
  18. pinMode(led3, OUTPUT);  
  19. }  
  20.   
  21. void loop() {  
  22. if (irrecv.decode(&results)) {  
  23. unsigned int value = results.value;  
  24. switch(value) {  
  25. case code1:  
  26.          if(itsONled[1] == 1) {        // if first led is on then  
  27.             digitalWrite(led1, LOW);   // turn it off when button is pressed  
  28.             itsONled[1] = 0;           // and set its state as off  
  29.          } else {                      // else if first led is off  
  30.             digitalWrite(led1, HIGH); // turn it on when the button is pressed  
  31.           itsONled[1] = 1;          // and set its state as on  
  32.         }  
  33.           break;   
  34.        case code2:  
  35.          if(itsONled[2] == 1) {  
  36.             digitalWrite(led2, LOW);  
  37.             itsONled[2] = 0;  
  38.          } else {  
  39.             digitalWrite(led2, HIGH);  
  40.             itsONled[2] = 1;  
  41.          }  
  42.           break;  
  43.        case code3:  
  44.          if(itsONled[3] == 1) {  
  45.            digitalWrite(led3, LOW);  
  46.             itsONled[3] = 0;  
  47.          } else {  
  48.              digitalWrite(led3, HIGH);  
  49.              itsONled[3] = 1;  
  50.          }  
  51.           break;            
  52.     }  
  53.     Serial.println(value); // you can comment this line  
  54.     irrecv.resume(); // Receive the next value  
  55.   }  
  56. }  
  • Before uploading the program add the library in the Arduino library.
  • Click to add the library - IR Arduino library. 
Explanation:
  • In the section it can work under the remote control.
  • To see the remote value press the button and see in the serial monitor. 
  • When we press button1 the led can be turn ON.
  • When we press button2 the led can be turn ON.
  • When we press button3 the led can be turn ON.
  • Same process can be done for turning OFF the LED. 
Output: 

   
                                        Figure 3: Output
 
Read more articles on Arduino:

Up Next
    Ebook Download
    View all
    Learn
    View all