How to use Infrared Remote With arduino
We can find infrared remote everywhere. Your TV, your Air Conditioner, and so many devices that use infrared remote. How if we build our device that can be controlled by infrared remote too? it’s simple using arduino. We just have to buy an infrared remote module. We will get infrared receiver module and the transmitter.
First things, you have to wire the infrared receiver and arduino like picture below
You should download the library first for this infrared receiver. You can download the library here.
And the you can use the sketch below to test the infrared receiver and remote. If everything is good you should see the code for every button on the serial monitor.
/* * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv * An IR detector/demodulator must be connected to the input RECV_PIN. * Version 0.1 July, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { Serial.begin(9600); // In case the interrupt driver crashes on setup, give a clue // to the user what's going on. Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX); irrecv.resume(); // Receive the next value } delay(100); }
After upload the code, open Serial Monitor.
Every time you press the button you should see the serial monitor print the unique code for every button. Every button should has different code. We can identify which button is pressed by this code.
I will try again to print every button I press on the remote, but now I will print just in decimal value not hexadecimal, to do this you need to upload this sketch
#include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; unsigned long data; void setup() { Serial.begin(9600); // In case the interrupt driver crashes on setup, give a clue // to the user what's going on. Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); } void loop() { if (irrecv.decode(&results)) { data = results.value; Serial.println(data); irrecv.resume(); // Receive the next value } delay(100); }
And then every time you press the button it should print decimal value like this :
So I found my code for button “1” is 16753245. Now copy this number first.
Turn On and Off a Lamp using this remote
Next experiment I want every time i press the button “1” it will turn on or turn off a lamp. To do this we will need a relay module and a lamp. Wire the relay and lamp like this :
Now we will edit the sketch and will turn on and off the lamp every time we press the button “1”
#include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; unsigned long data; void setup() { Serial.begin(9600); // In case the interrupt driver crashes on setup, give a clue // to the user what's going on. Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); } void loop() { if (irrecv.decode(&results)) { data = results.value; Serial.println(data); irrecv.resume(); // Receive the next value } if (data == 16753245) { digitalWrite(relay, !digitalRead(relay)); } data = 0; delay(100); }
After upload the sketch, now you should be able to turn on and off the lamp using button “1” on remote.
Switch the lamp using TV remote
I will use a TV remote to switch the lamp. This is just prove that the IR receiver can receive from any remote transmitter.
I want every time I press the power of my remote TV the lamp will switch.
So first step, I need to find the code or number for button power of my TV remote. To do this just open serial monitor while press the power button of my TV remote just like when we look code for “1” button from remote module.
I found that code or number for power button of my TV remote is 1834818995. So I edit the sketch for switch button to this code.
#include <IRremote.h> int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results; unsigned long data; void setup() { Serial.begin(9600); // In case the interrupt driver crashes on setup, give a clue // to the user what's going on. Serial.println("Enabling IRin"); irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); } void loop() { if (irrecv.decode(&results)) { data = results.value; Serial.println(data); irrecv.resume(); // Receive the next value } if (data == 16753245) { digitalWrite(relay, !digitalRead(relay)); } data = 0; delay(100); }
You can see full video tutorial below in my youtube channel