The most effective method to Control LEDs With an Arduino, IR Sensor, and Remote

In this undertaking, we will control LEDs utilizing an IR sensor and a remote. The IR sensor is a 1838B IR collector. At whatever point a catch on the remote is squeezed, it will send an infrared flag to the IR sensor in the coded shape. The IR sensor will then get this flag and will offer it to the Arduino.

How Can It Work?

At whatever point a catch is pushed on the remote, it sends an infrared flag in encoded shape. This flag is then gotten by the IR beneficiary and given to the Arduino. 

We will spare the code for the catches that we need to control the LEDs in the Arduino code. At whatever point a catch on the remote is squeezed, the Arduino gets a code. The Arduino will contrast this code and the codes officially spared, and if any of them coordinate, the Arduino will turn on the LED associated with that catch. 

Circuit Diagram

To start with, interface the four LEDs to the Arduino. Interface the positives of the four LEDs to the pins 7, 6, 5, and 4. Associate the negative of the four LEDs to GND on the Arduino through the 220 ohm resistors. The more drawn out wires on the LEDs are sure and the shorter wires are negative. 

At that point interface the IR sensor to the Arduino. The associations for the IR sensor with the Arduino are as per the following: 
  • Associate the negative wire on the IR sensor to GND on the Arduino. 
  • Associate the center of the IR sensor which is the VCC to 5V on the Arduino. 
  • Interface the flag stick on the IR sensor to stick 8 on the Arduino. 



#include

#define first_key 48703
#define second_key 58359
#define third_key 539
#define fourth_key 25979
int receiver_pin = 8;

int first_led_pin = 7;
int second_led_pin = 6;
int third_led_pin = 5;
int fourth_led_pin = 4;
int led[] = {0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;

void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);
pinMode(fourth_led_pin, OUTPUT);
}

void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1] == 1) {
digitalWrite(first_led_pin, LOW);
led[1] = 0;
} else {
digitalWrite(first_led_pin, HIGH);
led[1] = 1;
}
break;
case second_key:

if(led[2] == 1) {
digitalWrite(second_led_pin, LOW);
led[2] = 0;
} else {
digitalWrite(second_led_pin, HIGH);
led[2] = 1;
}
break;
case third_key:

if(led[3] == 1) {
digitalWrite(third_led_pin, LOW);
led[3] = 0;
} else {
digitalWrite(third_led_pin, HIGH);
led[3] = 1;
}
break;
case fourth_key:

if(led[4] == 1) {
digitalWrite(fourth_led_pin, LOW);
led[4] = 0;
} else {
digitalWrite(fourth_led_pin, HIGH);
led[4] = 1;
}
break;
}
Serial.println(value);
receiver.resume();
}
}

Code Explanation

As a matter of first importance, we included the library for the IR sensor and remote, at that point we characterized the codes for the keys that we will use in our undertaking. On the off chance that you are utilizing the code out of the blue and don't have a clue about the code for the keys, at that point transfer the Arduino code as it is and press the remote keys—the code for the keys you squeezed will be appeared in the serial screen. Presently change this code with the past code for the key that you need to control the LED with. 


#include 
#define first_key  48703
#define second_key  58359
#define third_key  539
#define fourth_key  25979
int receiver_pin = 8;

Next, we characterized the pins where we have associated the LEDs. We have associated the LEDs at pins 7, 6, 5, and 4. In this way, we characterized these pins as the LED pins. 


int first_led_pin = 7;
int second_led_pin = 6;
int third_led_pin = 5;
int fourth_led_pin = 4;

In the setup work, we characterized the LED sticks as the yield pins, since we are giving the yield to the LEDs through those pins. 

pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);
pinMode(fourth_led_pin, OUTPUT);

Insider savvy work, first, we check if any key has been squeezed. On the off chance that any key has been squeezed, at that point we contrast that key and the keys that we have characterized in our code. On the off chance that the key matches, at that point the LED associated with that stick will illuminate. On the off chance that the LED associated with that stick is as of now lit up, at that point it will go down.

if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case first_key:    
if(led[1] == 1) {       
digitalWrite(first_led_pin, LOW);
led[1] = 0;           
} else {                      
digitalWrite(first_led_pin, HIGH); 
led[1] = 1;          
}
break; 
case second_key:

if(led[2] == 1) {
digitalWrite(second_led_pin, LOW);
led[2] = 0;
} else {
digitalWrite(second_led_pin, HIGH);
led[2] = 1;
}
break;
case third_key:

if(led[3] == 1) {
digitalWrite(third_led_pin, LOW);
led[3] = 0;
} else {
digitalWrite(third_led_pin, HIGH);
led[3] = 1;
}
break;

Comments

Popular posts from this blog

How to Program an Arduino with the Scratch Programming Language Using mBlock

Arduino Servo Motor Control / Usage

5 Code Editors for Developers and Download Links