Infrared Receiver¶
Overview¶
An infrared-receiver is a component that receives infrared signals and can independently receive infrared ray and output signals compatible with TTL level. It’s similar with a normal plastic-packaged transistor in size and it is suitable for all kinds of infrared remote control and infrared transmission.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring¶
Schematic Diagram¶
Code¶
Note
You can open the file
22-ir_receiver.ino
under the path ofelite-explorer-kit-main\basic_project\22-ir_receiver
directly.Or copy this code into Arduino IDE.
The
IRremote
library is used here, you can install it from the Library Manager.
Note
There is a transparent plastic piece at the back of the remote control to cut off the power and pull it out before you use the remote control.
Code Analysis¶
This code is designed to work with an infrared (IR) remote control using the IRremote
library. Here’s the breakdown:
Including the library and defining constants. First, the IRremote library is included, and the pin number for the IR receiver is defined as 2.
#include <IRremote.h> const int IR_RECEIVE_PIN = 2;
Initializes serial communication at a baud rate of 9600. Initializes the IR receiver on the specified pin (
IR_RECEIVE_PIN
) and enables LED feedback (if applicable).void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the IR receiver }
The loop runs continuously to process incoming IR remote signals.
void loop() { // Check if there is any incoming IR signal if (IrReceiver.decode()) { // IrReceiver.printIRResultShort(&Serial); // Print the received data in one line // Serial.println(IrReceiver.decodedIRData.command, HEX); // Print the command in hexadecimal format Serial.println(decodeKeyValue(IrReceiver.decodedIRData.command)); // Map and print the decoded IR signal to corresponding key value IrReceiver.resume(); // Enable receiving of the next value } }
Checks if an IR signal is received and successfully decoded.
Decodes the IR command and stores it in
decodedValue
using a customdecodeKeyValue()
function.Prints the decoded IR value to the serial monitor.
Resumes IR signal reception for the next signal.
Helper function to map received IR signals to corresponding keys
// Function to map received IR signals to corresponding keys String decodeKeyValue(long result) { // Each case corresponds to a specific IR command switch (result) { case 0x16: return "0"; case 0xC: return "1"; case 0x18: return "2"; case 0x5E: return "3"; case 0x8: return "4"; case 0x1C: return "5"; case 0x5A: return "6"; case 0x42: return "7"; case 0x52: return "8"; case 0x4A: return "9"; case 0x9: return "+"; case 0x15: return "-"; case 0x7: return "EQ"; case 0xD: return "U/SD"; case 0x19: return "CYCLE"; case 0x44: return "PLAY/PAUSE"; case 0x43: return "FORWARD"; case 0x40: return "BACKWARD"; case 0x45: return "POWER"; case 0x47: return "MUTE"; case 0x46: return "MODE"; case 0x0: return "ERROR"; default: return "ERROR"; } }