How To Set Up An IR Remote and Receiver On An Arduino - Circuit Basics
How To Set Up An IR Remote and Receiver On An Arduino - Circuit Basics
SEARCH …
ON AN ARDUINO
Get new tutorials sent to
Posted by Krishna Pattabiraman | Arduino | 60
your inbox!
EMAIL ADDRESS
SUBSCRIBE
There are plenty of interesting Arduino projects that use IR communication too. With a
simple IR transmitter and receiver, you can make remote controlled robots, distance
sensors, heart rate monitors, DSLR camera remote controls, TV remote controls, and lots
more.
In this tutorial I’ll first explain what infrared is and how it works. Then I’ll show you how to
set up an IR receiver and remote on an Arduino. I’ll also show you how to use virtually any
IR remote (like the one for your TV) to control things connected to the Arduino.
Angeloni Gleba Palhano - Supermercado
Sons de Verão | A\CASA
Conheça a coleção Sons de Verão
The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need
to learn how to master the Arduino. It includes all of the parts, wiring diagrams, code,
and step-by-step instructions for 58 different robotics and internet of things projects
that are super fun to build!
WHAT IS INFRARED?
Infrared radiation is a form of light similar to the light we see all around us. The only
difference between IR light and visible light is the frequency and wavelength. Infrared
radiation lies outside the range of visible light, so humans can’t see it:
Because IR is a type of light, IR communication requires a direct line of sight from the
receiver to the transmitter. It can’t transmit through walls or other materials like WiFi or
Bluetooth.
HOW IR REMOTES AND RECEIVERS WORK
A typical infrared communication system requires an IR transmitter and an IR receiver.
The transmitter looks just like a standard LED, except it produces light in the IR spectrum
instead of the visible spectrum. If you have a look at the front of a TV remote, you’ll see
the IR transmitter LED:
The same type of LED is used in IR transmitter breakout boards for the Arduino. You can
see it at the front of this Keyes IR transmitter:
The IR receiver is a photodiode and pre-amplifier that converts the IR light into an
electrical signal. IR receiver diodes typically look like this:
Some may come on a breakout board like this:
IR SIGNAL MODULATION
IR light is emitted by the sun, light bulbs, and anything else that produces heat. That
means there is a lot of IR light noise all around us. To prevent this noise from interfering
with the IR signal, a signal modulation technique is used.
The receiver diode detects all frequencies of IR light, but it has a band-pass filter and only
lets through IR at 38 kHz. It then amplifies the modulated signal with a pre-amplifier and
converts it to a binary signal before sending it to a microcontroller.
IR TRANSMISSION PROTOCOLS
The pattern in which the modulated IR signal is converted to binary is defined by a
transmission protocol. There are many IR transmission protocols. Sony, Matsushita, NEC,
and RC5 are some of the more common protocols.
The NEC protocol is also the most common type in Arduino projects, so I’ll use it as an
example to show you how the receiver converts the modulated IR signal to a binary one.
Concertos
mundialmente famosos
Logical ‘1’ starts with a 562.5 µs long HIGH pulse of 38 kHz IR followed by a 1,687.5 µs long
LOW pulse. Logical ‘0’ is transmitted with a 562.5 µs long HIGH pulse followed by a 562.5
µs long LOW pulse:
This is how the NEC protocol encodes and decodes the binary data into a modulated
signal. Other protocols differ only in the duration of the individual HIGH and LOW pulses.
IR CODES
Each time you press a button on the remote control, a unique hexadecimal code is
generated. This is the information that is modulated and sent over IR to the receiver. In
order to decipher which key is pressed, the receiving microcontroller needs to
know which code corresponds to each key on the remote.
Different remotes send different codes for the keypresses, so you’ll need to determine the
code generated for each key on your particular remote. If you can find the datasheet, the
IR key codes should be listed. If not though, there is a simple Arduino sketch that will read
most of the popular remote controls and print the hexadecimal codes to the serial
monitor when you press a key. I’ll show you how to set that up in a minute, but first we
need to connect the receiver to the Arduino…
Lets get started with the hardware connections. The pin layout on most breakout
boards looks like this:
To connect a breakout board mounted IR receiver, hook it up to the Arduino like this:
To install the library from the ZIP file, open up the Arduino IDE, then go to Sketch >
Include Library > Add .ZIP Library, then select the IRremote ZIP file that you downloaded
from the link above.
#include <IRremote.h>
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
irrecv.resume();
}
}
Now press each key on your remote and record the hexadecimal code printed for each
key press.
Using the program above, I derived a table of keys and their corresponding codes from
the remote that came with my HX1838 IR receiver and remote set. Note that you will
receive a 0XFFFFFFFF code when you press a key continuously.
Key Code
CH- 0xFFA25D
CH 0xFF629D
CH+ 0xFFE21D
<< 0xFF22DD
>> 0xFF02FD
>|| 0xFFC23D
– 0xFFE01F
+ 0xFFA857
EQ 0xFF906F
100+ 0xFF9867
200+ 0xFFB04F
0 0XFF6897
1 0xFF30CF
2 0xFF18E7
3 0xFF7A85
4 0xFF10EF
5 0xFF38C7
6 0xFF5AA5
7 0xFF42BD
8 0xFF4AB5
9 0xFF52AD
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
switch (results.decode_type){
case NEC: Serial.println("NEC"); break ;
case SONY: Serial.println("SONY"); break ;
case RC5: Serial.println("RC5"); break ;
case RC6: Serial.println("RC6"); break ;
case DISH: Serial.println("DISH"); break ;
case SHARP: Serial.println("SHARP"); break ;
case JVC: Serial.println("JVC"); break ;
case SANYO: Serial.println("SANYO"); break ;
case MITSUBISHI: Serial.println("MITSUBISHI"); break ;
case SAMSUNG: Serial.println("SAMSUNG"); break ;
case LG: Serial.println("LG"); break ;
case WHYNTER: Serial.println("WHYNTER"); break ;
case AIWA_RC_T501: Serial.println("AIWA_RC_T501"); break ;
case PANASONIC: Serial.println("PANASONIC"); break ;
case DENON: Serial.println("DENON"); break ;
default:
case UNKNOWN: Serial.println("UNKNOWN"); break ;
}
irrecv.resume();
}
}
#include <IRremote.h>
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
if (results.value == 0XFFFFFFFF)
results.value = key_value;
switch(results.value){
case 0xFFA25D:
Serial.println("CH-");
break;
case 0xFF629D:
Serial.println("CH");
break;
case 0xFFE21D:
Serial.println("CH+");
break;
case 0xFF22DD:
Serial.println("|<<");
break;
case 0xFF02FD:
Serial.println(">>|");
break ;
case 0xFFC23D:
Serial.println(">|");
break ;
case 0xFFE01F:
Serial.println("-");
break ;
case 0xFFA857:
Serial.println("+");
break ;
case 0xFF906F:
Serial.println("EQ");
break ;
case 0xFF6897:
Serial.println("0");
break ;
case 0xFF9867:
Serial.println("100+");
break ;
case 0xFFB04F:
Serial.println("200+");
break ;
case 0xFF30CF:
Serial.println("1");
break ;
case 0xFF18E7:
Serial.println("2");
break ;
case 0xFF7A85:
Serial.println("3");
break ;
case 0xFF10EF:
Serial.println("4");
break ;
case 0xFF38C7:
Serial.println("5");
break ;
case 0xFF5AA5:
Serial.println("6");
break ;
case 0xFF42BD:
Serial.println("7");
break ;
case 0xFF4AB5:
Serial.println("8");
break ;
case 0xFF52AD:
Serial.println("9");
break ;
}
key_value = results.value;
irrecv.resume();
}
}
If your remote sends different codes than the ones in the table above, just replace the hex
code in each line where it says:
case 0xFFA25D:
Serial.println(“CH-“);
In these lines, when the hex code 0xFFA25D is received, the Arduino prints “CH-“.
The next step is to create an object called results , from the decode_results class, which
will be used by the irrecv object to share the decoded information with our application
(line 5).
Concertos
mundialmente famosos
The irrecv.blink13(true) function on line 11 will blink the Arduino’s on board LED every
time the receiver gets a signal from the remote control, which is useful for debugging.
Concertos
mundialmente famosos
In the void loop() block, the function irrecv.decode will return true if a code is received
and the program will execute the code in the if statement. The received code is stored
in results.value . Then I used a switch to handle each IR code and print the
corresponding key value.
if (results.value == 0XFFFFFFFF)
results.value = key_value;
If we receive 0XFFFFFFFF from the remote, it means a repetition of the previous key. So in
order to handle the repeat key pattern, I am storing the hex code in a global variable
key_value every time a code is received:
key_value = results.value;
When you receive a repeat pattern, then the previously stored value is used as the current
key press.
At the end of the void loop() section, we call irrecv.resume() to reset the receiver and
prepare it to receive the next code.
#include <IRremote.h>
#include <LiquidCrystal.h>
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
lcd.begin(16, 2);
}
void loop(){
if (irrecv.decode(&results)){
if (results.value == 0XFFFFFFFF)
results.value = key_value;
lcd.setCursor(0, 0);
lcd.clear();
switch(results.value){
case 0xFFA25D:
lcd.print("CH-");
break;
case 0xFF629D:
lcd.print("CH");
break;
case 0xFFE21D:
lcd.print("CH+");
break;
case 0xFF22DD:
lcd.print("|<<");
break;
case 0xFF02FD:
lcd.print(">>|");
break ;
case 0xFFC23D:
lcd.print(">|");
break ;
case 0xFFE01F:
lcd.print("-");
break ;
case 0xFFA857:
lcd.print("+");
break ;
case 0xFF906F:
lcd.print("EQ");
break ;
case 0xFF6897:
lcd.print("0");
break ;
case 0xFF9867:
lcd.print("100+");
break ;
case 0xFFB04F:
lcd.print("200+");
break ;
case 0xFF30CF:
lcd.print("1");
break ;
case 0xFF18E7:
lcd.print("2");
break ;
case 0xFF7A85:
lcd.print("3");
break ;
case 0xFF10EF:
lcd.print("4");
break ;
case 0xFF38C7:
lcd.print("5");
break ;
case 0xFF5AA5:
lcd.print("6");
break ;
case 0xFF42BD:
lcd.print("7");
break ;
case 0xFF4AB5:
lcd.print("8");
break ;
case 0xFF52AD:
lcd.print("9");
break ;
}
key_value = results.value;
irrecv.resume();
}
}
Again, if the hex codes don’t match the codes output by your remote, just replace them
for each character where it says case 0xXXXXXXXX; .
The example circuit has the IR receiver connected to the Arduino, with a red LED
connected to pin 10 and a green LED connected to pin 11:
The code below will write digital pin 10 HIGH for 2 seconds when the “5” button is pressed,
and write digital pin 11 HIGH for 2 seconds when the “2” button is pressed:
#include <IRremote.h>
void setup(){
irrecv.enableIRIn();
irrecv.blink13(true);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop(){
if (irrecv.decode(&results)){
switch(results.value){
case 0xFF38C7: //Keypad button "5"
digitalWrite(redPin, HIGH);
delay(2000);
digitalWrite(redPin, LOW);
}
switch(results.value){
case 0xFF18E7: //Keypad button "2"
digitalWrite(greenPin, HIGH);
delay(2000);
digitalWrite(greenPin, LOW);
}
irrecv.resume();
}
}
So far we have covered the properties of infrared radiation and how communication
happens between the transmitter and receiver. We saw how to identify the IR key codes
for a given remote control. We learned how to display key presses on serial monitor and
on an LCD screen. Finally I showed you how to control the Arduino’s output with the
remote. Have fun playing with this and be sure to let us know in the comments if you
have any questions or trouble setting this up!
SHARE:
RELATED POSTS
60 COMMENTS
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
exit status 1
Error compiling for board Arduino/Genuino Uno.
)
REPLY
Tom on October 25, 2018 at 4:28 pm
Hi, late but hopefully still helpful, if not for you maybe for somebody
else.
This error-message occurs when you’re using the “Robot IR Remote”
library instead of the “IRremote” library, which you would first have to
import, either by using the buildt-in feature of the Arduino-IDE, or by
downloading a ZIP-archive.
REPLY
REPLY
REPLY
REPLY
The other cycle contained 4 relayes, each one have 1 daiods and 574
transistor. this cycle was supported using 5v (1 amp) adapter.
those cycles were conected from (a)- cathode (b)- the ic’s output pins to the
transistors.
the target was to open/close each relay by lg-tv remote control. the cycle
work very will through 1 hour from starting point, but after that it hang and
not receive the signals.
REPLY
for some reason the program never finishes uploading onto my uno. The
program verifies properly and I see some on the memory usage figures but it
just never finishes. Any ideas?
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
REPLY
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
if (results.value == 0XFFFFFFFF)
results.value = key_value;
switch(results.value){
case 1FE48B7:
Serial.println(“Switch ON/OFF”);
break;
case 1FE58A7:
Serial.println(“Mode”);
break;
case 1FE7887:
Serial.println(“MUTE”);
break;
case 1FE807F:
Serial.println(“>||”);
break;
case 1FE40BF:
Serial.println(“|<>|”);
break ;
case 1FE20DF:
Serial.println(“EQ”);
break ;
case 1FEA05F:
Serial.println(“VOL-“);
break ;
case 1FE609F:
Serial.println(“VOL+”);
break ;
case 1FEE01F:
Serial.println(“0”);
break ;
case 1FE10EF:
Serial.println(“RPT”);
break ;
case 1FE906F:
Serial.println(“U/SD”);
break ;
case 1FE50AF:
Serial.println(“1”);
break ;
case 1FED827:
Serial.println(“2”);
break ;
case 1FEF807:
Serial.println(“3”);
break ;
case 1FE30CF:
Serial.println(“4”);
break ;
case 1FEB04F:
Serial.println(“5”);
break ;
case 1FE708F:
Serial.println(“6”);
break ;
case 1FE00FF:
Serial.println(“7”);
break ;
case 1FEF00F:
Serial.println(“8”);
break ;
case 1FE9867:
Serial.println(“9”);
break ;
}
key_value = results.value;
irrecv.resume();
}
}
exit status 1
‘IFEFFFFFFFF’ was not declared in this scope
REPLY
REPLY
REPLY
REPLY
REPLY
Thank you
REPLY
REPLY
REPLY
REPLY
https://www.arduinolibraries.info/libraries/i-rremote
REPLY
REPLY
Thank you for helping me see the light. These 2 lines are what significantly
helped me move forward with my project.
if (results.value == 0XFFFFFFFF)
results.value = key_value;
Thank you, thank you, thank you
REPLY
By the way I use the same Library as you;, results.value codes and the number
of bits are perfect
Can you help me?
Thanks
REPLY
REPLY
In the scheme ‘Using the IR Remote to Control Things’ the two resistors are
connected to the anode. They should be connected to the kathode-
side(ground), as I found out.
Kind regards,
Jan Speyer, the Netherlands
REPLY
REPLY
REPLY
Harold on September 25, 2020 at 5:05 pm
I am getting this error Message ? This report would have more information
with
“Show verbose output during compilation”
enabled in File > Preferences.
Arduino: 1.0.6 (Windows 2000), Board: “Arduino Uno”
In file included from sketch_sep25j.ino:1:
C:\Documents and Settings\HAC\My Documents\Arduino\libraries\Arduino-
IRremote-2.6.1\src/IRremote.h:486: error: ISO C++ forbids initialization of
member ‘sendPin’
C:\Documents and Settings\HAC\My Documents\Arduino\libraries\Arduino-
IRremote-2.6.1\src/IRremote.h:486: error: making ‘sendPin’ static
REPLY
REPLY
REPLY
I’m trying to copy a remote controller of which I have the schematics, but not
the parts.
It seems that it’s using the Toshiba protocol, but I can’t find information
about it… The part I’m trying to simulate is the PT2248
REPLY
the first code, instead of giving me IR codes, it would just print a 0 to the
serial port every time i pressed a button on the controller.
I tried a number of things, including testing all the different examples in the
IRremote library….
after i failed to get it to work seemlessly, i discovered that the IRremote library
has been updated in the last few months.
However, you said that swap 3.0.1 to earlier version of IRremote library
has solved the issue.
Thanks.
REPLY
In Arduino IDE:
File>Preferences
copy the sketchbook location
REPLY
In Arduino IDE:
File>Preferences
copy the sketchbook location
REPLY
#include
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume();
}
delay(100);
}
REPLY
REPLY
REPLY
REPLY
REPLY
Library : IRremote
Lib. version : 3.3.0
Add library : Tools – Manage Libraries
Library URL : https://github.com/Arduino-IRremote/Arduino-IRremote
*/
#include
#include
IRrecv irrecv(VS1838B_PIN);
void setup() {
void loop() {
if (irrecv.decode()) {
IRdecodedRawData = irrecv.decodedIRData.decodedRawData;
Serial.print(IRdecodedRawData, HEX);
Serial.print(” @ “);
switch (irrecv.decodedIRData.protocol){
// Decode
/*
if(IRdecodedRawData == 0xFFA25D) Serial.println(“Pressed 1”);
if(IRdecodedRawData == 0xFF629D) Serial.println(“Pressed 2”);
if(IRdecodedRawData == 0xFFE21D) Serial.println(“Pressed 3”);
if(IRdecodedRawData == 0xFF22DD) Serial.println(“Pressed 4”);
if(IRdecodedRawData == 0xFF02FD) Serial.println(“Pressed 5”);
if(IRdecodedRawData == 0xFFC23D) Serial.println(“Pressed 6”);
if(IRdecodedRawData == 0xFFE01F) Serial.println(“Pressed 7”);
if(IRdecodedRawData == 0xFFA857) Serial.println(“Pressed 8”);
if(IRdecodedRawData == 0xFF906F) Serial.println(“Pressed 9”);
if(IRdecodedRawData == 0xFF9867) Serial.println(“Pressed 0”);
if(IRdecodedRawData == 0xFF6897) Serial.println(“Pressed *”);
if(IRdecodedRawData == 0xFFB04F) Serial.println(“Pressed #”);
if(IRdecodedRawData == 0xFF18E7) Serial.println(“Pressed Arrow Up”);
if(IRdecodedRawData == 0xFF4AB5) Serial.println(“Pressed Arrow
Down”);
if(IRdecodedRawData == 0xFF10EF) Serial.println(“Pressed Arrow Left”);
if(IRdecodedRawData == 0xFF5AA5) Serial.println(“Pressed Arrow
Right”);
if(IRdecodedRawData == 0xFF38C7) Serial.println(“Pressed Ok”);
*/
REPLY
C:\Users\thiva\AppData\Local\Temp\ .arduinoIDE-unsaved2023326-25928-
8331dh.pit1g\sketch_apr26a\sketch_apr26a.ino:1:10: fatal error: IRremote: No
such file or directory
#include
^~~~~~~~~~
compilation terminated.
exit status 1
REPLY
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
COMMENT
Save my name, email, and website in this browser for the next time I comment.
For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy
Policy and Terms of Use.
POST COMMENT
Copyright Circuit Basics
Raspberry Pi Arduino DIY Electronics Programming Videos Resources About Contact Us Privacy Policy