Project 10.00 Decoding IR
This Arduino sketch is designed to receive and decode infrared signals. When you point a remote at the MCU Trainer, the decoded results are printed to the Serial Monitor.
Project Code:
/////////////////////////////////////////// // 10.00 Infrared Receiver #include <IRremote.h> byte IRRecv = 17; void setup() { Serial.begin(9600); IrReceiver.begin(IRRecv); } void loop() { if (IrReceiver.decode()) { IrReceiver.printIRResultShort(&Serial); IrReceiver.resume(); } } ///////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
Before we do anything, we need to install the IRremote Library. You can do this by going to Tools -> Manage Libraries, and type in “IRremote”. It looks like this:
Click install. If it asks you to install dependencies go ahead and do so.
For reference, you can include it into a project by going to Sketch -> Include Library, and click “IRremote”. This does not need done in this sketch as it is already included.
The library reference where you can find all of the functions avaliable can be found here:
https://github.com/Arduino-IRremote/Arduino-IRremote
In the setup() function we initialize the IR receiver on digital pin 17 using the begin() method from the IRremote library.
IrReceiver.begin(IRRecv);
The loop() function continuously checks for incoming IR signals. It uses the decode() method to check if a signal has been received. If a signal has been received the decode() method then returns a one and the if-statement code executes.
if (IrReceiver.decode())
If a signal is received, it then prints a short summary of the decoded data to the Serial Monitor using the printIRResultShort function. The data printed includes the protocol used, the address, command, and raw data. Much like the “&Wire” don’t worry too much about what the “&Serial” that we pass is really doing. Just know we need to pass that to print out the data.
IrReceiver.printIRResultShort(&Serial);
It then resumes the receiver to keep listening for new signals using the resume() function.
IrReceiver.resume(); } } ///////////////////////////////////////////
Make sure and open the Serial monitor to see what is being printed.
Now that you can see what the signal that your remote is sending, you can use the IR emitter to copy that signal.
The data that I got from my remote was:
“Protocol=NEC Address=0x0 Command=0x9 Raw-Data=0xF609FF00 32 bits LSB first”
Let’s break that data down:
Section | Description |
---|---|
Protocol=NEC | This means the infrared signal uses the NEC protocol, a common infrared protocol used in consumer electronics. |
Address=0x0 | The address is used to specify the target device that should respond to this signal. In this case, the address is 0x0, which usually indicates a generic device. |
Command=0x9 | The command portion of the signal tells the device what to do. In this case, the command is 0x9. |
Raw-Data=0xF609FF00 | This represents the raw data of the signal received by the IR receiver. This is typically a sequence of pulse and space lengths that make up the actual signal. The raw data is useful for debugging or for cases where the protocol, address, and command don't adequately represent the signal. |
32 bits LSB first | This indicates that the data is 32 bits long and that it's being read from the least significant bit (LSB) first. This is important for correctly interpreting the raw data. |
We can use this data to emulate the remote with the IR emitter LED.