How to Interface With IR remote sensor and Arduino
The full form of IR is infrared . it is Light having greater wavelength than visible light . we can't see infrared light However bats, snakes, frogs can see it . we can also see it but with the help of technology.
Now whenever we press any button from remote a simple set of binary code (eg. 101011110) get sent through IR light. for every button there is individual binary code and that code get detected by IR receiver. Now that receiver sends that data to Arduino. and with the help of that data we can create great remote projects.
Circuit Diagram :
Decoding Remote Button Values :
To decode remote button values we will need a code that will read value and print it on Serial monitor . For this we use library "IRremote.h" . Download library from Library manager and then you can use it.
Code :
#include "IRremote.h"
int recvpin = A0;
IRrecv sensor(recvpin);
decode_results val;
void setup() {
Serial.begin(9600);
sensor.enableIRIn();
}
void loop() {
while (sensor.decode(&val) == 0) {}
Serial.println(val.value, HEX);
delay(100);
sensor.resume();
}
Now As here i get a value for Remote on/off button, you will get different value for different kind of button. and you can do this for rest of the buttons and note them somewhere.Now it's time for final code, take the value of a button that you want and with the help of
if-else or Switch-case statement we can do some real remote work with it.
Here is simple example of How we can turn on and off LED with remote
Code:
#include "IRremote.h"
int recvpin = A0;
int LED = 13;
IRrecv sensor(recvpin);
decode_results val;
int i = 0 ;
void setup() {
Serial.begin(9600);
sensor.enableIRIn();
pinMode(LED, OUTPUT);
digitalWrite(LED,LOW);
}
void loop() {
while (sensor.decode(&val) == 0) {}
Serial.println(val.value, HEX);
delay(100);
sensor.resume();
if (val.value == 0x86C6807F) {
i++;
}
if (i == 1) {
digitalWrite(LED, HIGH);
}
if (i == 2) {
digitalWrite(LED, LOW);
i = 0;
}
}
In my Case the On/Off button Hexadecimal value is "86C6807F" , In your case it maybe different.So with the help of this code we can control LED with remote. You can control Lights and fans and other electrical devices also with the help of relay modules. SO That's it , Hope you like the tutorial .
Post a Comment