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 things in 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
  • This sensor is the main part of the 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. void loop() {
  21. if (irrecv.decode(&results)) {
  22. unsigned int value = results.value;
  23. switch(value) {
  24. case code1:
  25. if(itsONled[1] == 1) { // if first led is on then
  26. digitalWrite(led1, LOW); // turn it off when button is pressed
  27. itsONled[1] = 0; // and set its state as off
  28. } else { // else if first led is off
  29. digitalWrite(led1, HIGH); // turn it on when the button is pressed
  30. itsONled[1] = 1; // and set its state as on
  31. }
  32. break;
  33. case code2:
  34. if(itsONled[2] == 1) {
  35. digitalWrite(led2, LOW);
  36. itsONled[2] = 0;
  37. } else {
  38. digitalWrite(led2, HIGH);
  39. itsONled[2] = 1;
  40. }
  41. break;
  42. case code3:
  43. if(itsONled[3] == 1) {
  44. digitalWrite(led3, LOW);
  45. itsONled[3] = 0;
  46. } else {
  47. digitalWrite(led3, HIGH);
  48. itsONled[3] = 1;
  49. }
  50. break;
  51. }
  52. Serial.println(value); // you can comment this line
  53. irrecv.resume(); // Receive the next value
  54. }
  55. }
  • 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: