Infrared Encoding and Decoding Module
The infrared encoding and decoding module consists of an MCU, infrared transmitter, and infrared receiver. It allows for serial communication with other infrared-controlled devices and can be used for wireless data communication and data transmission. Supporting NEC infrared encoding, this module is compatible with 99% of NEC-format devices, including most televisions, set-top boxes, DVD players, and fans.
Using basic serial communication knowledge, you can control the module to transmit commands by sending specific instructions through the MCU’s serial interface. For infrared decoding, the module receives data via the serial interface to capture remote control codes. Additionally, two modules can be used together for wireless control.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.10.538d6a4brTW2hu&id=600109896799
Baidu Netdisk Download Link:
https://pan.baidu.com/s/1idRcrVCxQ5zWLh59EFpi9g
Password: n8ud
Specifications
Operating Voltage: 5V
Operating Current: >100mA
Transmission Range: 6–10 meters (varies depending on ambient light and transmission/receiving conditions)
Reception Range: 6–10 meters (dependent on the power of the transmitting device)
Control Method: Serial Interface
Pin Count: 4 Pin (2.54mm pitch header)
Instructions for Use:
During transmission and reception, the indicator light will flash. Otherwise, it will remain steadily lit.
Decoding: No commands are needed for decoding. Simply aim the remote control at the module's receiver and press a button. The module will output the infrared code through the serial port, which can be viewed using a serial debugging tool. The output format consists of three parts: "User Code 1 + User Code 2 + Command Code." When encoding for transmission, only these three values need to be sent.
Viewing Decoding Results
The connections are correct here—no need to second-guess.
Connect the module's TXD to the development board’s TXD->1, and the module's RXD to the development board’s RXD->0.
To view the decoded results, connect a serial debugging tool to your computer and configure it as shown below. Aim an infrared remote, such as an air conditioner remote, at the module to send an IR signal. The infrared encoding and decoding module will decode the signal and display the decoded data. In the example shown, the decoded signal for turning on a Midea air conditioner is displayed as: E0 FD FD.
Communication Method
This infrared transmitter and receiver module uses a specific serial protocol to handle the transmission and reception of infrared signals. Note that all serial commands for this module must be in hexadecimal format (e.g., A1 is represented as 0xA1).
The frame header contains the communication address, with the default address set to A1. This default address can be modified through commands, and there’s also a universal address, 0xFA, which can be used to reset the module’s address if the custom setting is forgotten.
The operation code indicates the function of each command, as defined in the table below.
Operation Code | F1 | F2 | F3 |
---|---|---|---|
Description | IR Signal Transmission Status | Change Communication Address Status | Change Serial Baud Rate Status |
Sending Feedback
After each command is sent, there will be corresponding feedback.
Feedback Data | Description |
---|---|
F1 | Transmission Successful |
F2 | Serial Address Modified Successfully |
F3 | Baud Rate Set Successfully |
No Response | Command Reception Error or Operation Failed |
Examples:
When sending the infrared signal command FA F1 E0 FD FD , receiving F1 as feedback indicates success; any other feedback indicates failure.
When sending the change address command FA F2 A5 00 00, receiving F2 indicates the address was successfully changed; any other feedback indicates failure.
When sending the baud rate change command FA F3 02 00 00, receiving F3 confirms the baud rate change was successful; any other feedback indicates failure.
Code Implementation Notes:
Define an array of type unsigned char with a length of 5: unsigned char send_data[] = {0}; and populate this array with the command instructions. Send this array to the module via the serial interface.
unsigned char send_data[]={0};
send_data[] = 0XF1; //Frame Header
send_data[] = 0XF1; //Operation Code
send_data[] = 0XE0; //Address 1
send_data[] = 0XFD; //Address 2
send_data[] = 0XFD; //Key Value
2
3
4
5
6
Since feedback is expected, ensure previous received data is cleared before each new command, regardless of whether previous data exists.
infrared_receive_clear();//Clear the received data first
infrared_send_hex(send_data, 5);//Send the data
2
After sending, the serial port should wait for the feedback. Set a timeout in case feedback is not received within a certain timeframe to prevent the program from hanging.
time_out = 1000;//Wait for reception time 1000ms
//Wait for response data
//infrared_recv_flag != 1 indicates no data received through serial
while( infrared_recv_flag != 1 && time_out > 0 )
{
time_out--;
delay_1ms(1);
}
if( time_out > 0 )//No timeout
{
infrared_recv_flag = 0;//Clear flag
//If received the success response data
if( infrared_recv_buff[] == 0XF1 ) return 1;
else return 2;//Incorrect received data
}
return 0;//Timeout
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Hardware Connection
The connections are correct here.
Connect the infrared module's 5V to the development board's 5V.
Connect the infrared module's TXD to the development board's RX->0.
Connect the infrared module's RXD to the development board's TX->1.
Connect the infrared module's GND to the development board's GND.
Note: When uploading code, you may need to disconnect the RX and TX pins from the HC-05 to avoid interference during the upload process.
Usage Method
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 11,2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
unsigned char device_addr = 0XA1;//默认器件地址
unsigned char Infrared_emission = 0XF1;//红外发射状态
unsigned char modified_addr = 0XF2;//修改设备地址
unsigned char modified_baud = 0XF3;//修改波特率
/******************************************************************
* 函 数 名 称:Infrared_emission_cmd
* 函 数 说 明:控制模块发射红外命令
* 函 数 形 参:Infrared_buff要发射的红外信号 len红外信号长度
* 函 数 返 回:0: 超时未接收到发射成功数据
* 1: 发射成功
* 2: 接收的数据不是发射成功数据
* 100:发射的数据不是3位
* 备 注:无
******************************************************************/
char Infrared_emission_cmd(unsigned char* Infrared_buff, char len)
{
unsigned char send_data[] = {0};//必须赋初值
unsigned int time_out = 1000; //超时时间,单位MS
String receivedData = "";
// //如果要发送的数据长度不对
if( (len < 3) || (len > 3) )
return 100;
send_data[] = device_addr; //设备地址
send_data[] = Infrared_emission; //操作位
send_data[] = Infrared_buff[]; //数据位1
send_data[] = Infrared_buff[]; //数据位2
send_data[] = Infrared_buff[]; //数据位3
//发送数据
for( int i = 0; i < 5; i++ )Serial.write(send_data[]);
//等待模块的回应数据
while (Serial.available() <= 0 && time_out > 0 )
{
time_out--;
delay(1);
}
if( time_out > 0 )//没有超时
{
while (Serial.available() > 0)
{
unsigned char incomingByte = Serial.read();
receivedData += incomingByte;
delay(2); // 确保数据完全到达
}
//如果接收到通信地址修改成功的回应数据
if( receivedData[] == 0XF1 ) return 1;
else return 2;
}
return 0;
}
void setup() {
Serial.begin(9600);
}
void loop() {
//假设这个是你要发送的红外数据
unsigned char buff[] = {0XE0, 0XFD, 0XFD};
Infrared_emission_cmd(buff,3);
delay(1000);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84