Infrared Decoding and Coding Module
The infrared decoding and coding module uses an MCU + infrared transmitter + infrared receiver. The MCU's UART is routed out to connect to other devices that need infrared control, enabling functions such as infrared wireless data communication and data transmission. It has NEC format infrared code transmission capability and can control 99% of NEC infrared format devices, such as most TVs, set-top boxes, DVDs, electric fans, and other appliances. You only need to use the microcontroller's UART communication knowledge to send specified commands through the UART to control the module's transmission; you can also perform infrared decoding through the UART receive method to obtain the remote control code information. You can also use two modules to achieve wireless control.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.10.538d6a4brTW2hu&id=600109896799 Materials download link: https://pan.baidu.com/s/1idRcrVCxQ5zWLh59EFpi9g Materials extraction code: n8ud
Specifications
Operating voltage: 5V Supply current: >100mA Transmission distance: 6-10 meters (varies with ambient light and transceiver conditions) Reception distance: 6-10 meters (related to the transmitting device's power) Control method: UART Number of pins: 4 Pin (2.54mm pitch pin header)
View Materials
The indicator LED flashes during transmission and reception, otherwise it stays solid.
Decoding: No command needs to be sent during decoding. Just point the remote at the module's receiver and press a button. The UART of the module will then output the infrared code. You can view the decoding result through the serial debug assistant. The result is output as three digits: "User Code 1 + User Code 2 + Command Code". When sending a code, you only need to send these three digits.
How to view the decoding result
Use a USB-to-TTL serial debug module to connect to the infrared transceiver module, then open the serial assistant on the computer for debugging.
Connect the serial debug module to the infrared transceiver module.
Connect the serial debug assistant to the computer, and open the serial debug software with the following configuration. Use an infrared remote or air conditioner remote to send an infrared signal to the module. You will see the decoded data returned by the infrared transceiver module after parsing the remote signal. The figure shows the decoded data of the Midea air conditioner remote's turn-on signal: E0 FD FD.
Communication method This infrared transceiver module implements the reception and transmission of infrared signals through a specific UART protocol. Note that the format of the UART commands sent and received by this module is hexadecimal, i.e., A1 is 0xA1.
The frame header is the communication address, A1 is the default address, and the default address can be modified through commands, so there is also a universal address 0xFA. When you forget the address you set, you can modify it through the universal address 0xFA. The operation bit is used to indicate what function the current command implements. The definition description is shown in the table below.
Send feedback After each command is sent, there is corresponding feedback information.
Example: After sending the infrared signal data FA F1 E0 FD FD, returning F1 means the transmission was successful; returning something else means the transmission failed; After sending the communication address modification FA F2 A5 00 00, returning F2 means the modification was successful; returning something else means the modification failed; After sending the baud rate modification FA F3 02 00 00, returning F3 means the modification was successful; returning something else means the modification failed;
Implementation code description: Define an unsigned char array with a length of 5: unsigned char send_data[5]={0}; and fill the commands into the array.
unsigned char send_data[5]={0};
send_data[0] = 0XF1; //Frame header
send_data[1] = 0XF1; //Operation bit
send_data[2] = 0XE0; //Address 1
send_data[3] = 0XFD; //Address 2
send_data[4] = 0XFD; //Key value2
3
4
5
6
Send this array to the module via UART. However, note that because there is feedback information, in order to determine whether the returned data is correct, you need to clear the previously received data first, regardless of whether any data was received before.
infrared_receive_clear();//Clear the received data first
infrared_send_hex(send_data, 5);//Send data2
After sending the data, wait for the UART to receive the feedback data, and set a timeout to end the reception if no data is received for a long time, preventing the program from hanging while waiting.
time_out = 1000;//Wait reception time 1000ms
//Wait for response data
//infrared_recv_flag != 1 means UART has not received data
while( infrared_recv_flag != 1 && time_out > 0 )
{
time_out--;
delay_1ms(1);
}
if( time_out > 0 )//Not timed out
{
infrared_recv_flag = 0;//Clear the flag
//If the response data for successful transmission is received
if( infrared_recv_buff[0] == 0XF1 ) return 1;
else return 2;//Received data is incorrect
}
return 0;//Reception timed out2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting.
For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here.
However, here we replace the file names bsp_dht11.c and bsp_dht11.h with bsp_infrared.c and bsp_infrared.h, and change the folder name to Infrared.
Write Code
In the file bsp_infrared.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#include "bsp_infrared.h"
#include "string.h"
#include "stdlib.h"
unsigned char infrared_recv_buff[USART_RECEIVE_LENGTH];//UART receive buffer
uint16_t infrared_recv_length = 0;//UART receive length
unsigned char infrared_recv_flag = 0;//UART reception complete flag 1=reception complete 0=not complete
unsigned char device_addr = 0XA1;//Default device address
unsigned char Infrared_emission = 0XF1;//Infrared transmission state
unsigned char modified_addr = 0XF2;//Modify device address
unsigned char modified_baud = 0XF3;//Modify baud rate
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: Infrared_GPIO_Init
* Function Description: Initialize the universal infrared pin
* Function Parameters: Set baud rate
* Function Return: None
* Author: LC
* Notes: The default baud rate of the universal infrared is 9600
******************************************************************/
void Infrared_GPIO_Init(uint32_t band_rate)
{
//Define UART configuration structure, must be initialized, otherwise it cannot work
uart_config_t uart_config={0};
uart_config.baud_rate = band_rate; //Configure baud rate
uart_config.data_bits = UART_DATA_8_BITS; //Configure data bits to 8
uart_config.parity = UART_PARITY_DISABLE; //Configure parity to none
uart_config.stop_bits = UART_STOP_BITS_1; //Configure stop bits to 1
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; //Disable hardware flow control
//Load the above parameters into the UART1 registers
uart_param_config(PORT_UART, &uart_config);
//Bind pins TX=GPIO_TX_PIN RX=GPIO_RX_PIN RTS=Not used CTS=Not used
uart_set_pin(PORT_UART, GPIO_TX_PIN, GPIO_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Install UART driver
uart_driver_install(PORT_UART, USART_RECEIVE_LENGTH, USART_RECEIVE_LENGTH, 0, NULL, 0);
//Create UART receive task
xTaskCreate(BSP_INFRARED_TASK, "BSP_INFRARED_TASK", USART_RECEIVE_LENGTH*2, NULL, configMAX_PRIORITIES, NULL);
}
/************************************************
Function Name: infrared_send_byte
Function: Send a single byte via UART
Parameters: ucch: byte to send
Return Value:
Author: LC
*************************************************/
void infrared_send_byte(uint8_t ucch)
{
uart_write_bytes(PORT_UART, (const char*)&ucch, 1);
}
void infrared_send_hex(uint8_t *ch, int len)
{
while(len--)
{
uint8_t value = *ch++;
uart_write_bytes(PORT_UART, (const char*)&value, 1);
}
}
/************************************************
Function Name: infrared_receive_clear
Function: Clear all data received by UART
Parameters: None
Return Value: None
Author: LC
*************************************************/
void infrared_receive_clear(void)
{
unsigned int i = 0;
for( i = 0; i < USART_RECEIVE_LENGTH; i++ )
{
infrared_recv_buff[ i ] = 0;
}
infrared_recv_length = 0;
}
/******************************************************************
* Function Name: Infrared_emission_cmd
* Function Description: Control the module to transmit infrared command
* Function Parameters: Infrared_buff is the infrared signal to transmit len is the infrared signal length
* Function Return: 0: Timed out without receiving transmission success data
* 1: Transmission successful
* 2: Received data is not the transmission success data
* 100: Transmitted data is not 3 digits
* Author: LC
* Notes: None
******************************************************************/
char Infrared_emission_cmd(unsigned char* Infrared_buff, char len)
{
unsigned char send_data[5] = {0};//Must be initialized
unsigned int time_out = 1000; //Timeout, in ms
// //If the data length to send is incorrect
if( (len < 3) || (len > 3) )
return 100;
send_data[0] = device_addr; //Device address
send_data[1] = Infrared_emission; //Operation bit
send_data[2] = Infrared_buff[0]; //Data bit 1
send_data[3] = Infrared_buff[1]; //Data bit 2
send_data[4] = Infrared_buff[2]; //Data bit 3
infrared_receive_clear();//Clear received data first
infrared_send_hex(send_data, 5);//Send data
//Wait for response data
while( infrared_recv_flag != 1 && time_out > 0 )
{
time_out--;
delay_1ms(1);
}
if( time_out > 0 )//Not timed out
{
infrared_recv_flag = 0;
//If the response data for successful communication address modification is received
if( infrared_recv_buff[0] == 0XF1 ) return 1;
else return 2;
}
return 0;
}
/******************************************************************
* Function Name: modified_addr_cmd
* Function Description: Modify the UART address command
* Function Parameters: addr_value is the UART address to modify
* Function Return: 0: Timed out without receiving modification success data
* 1: Modification successful
* 2: Received data is not the modification success data
* Author: LC
* Notes: None
******************************************************************/
char modified_addr_cmd(unsigned int addr_value)
{
unsigned char send_data[5] = {0};//Must be initialized
unsigned int time_out = 1000; //Timeout, in ms
send_data[0] = device_addr; //Device address
send_data[1] = modified_addr; //Operation bit
send_data[2] = addr_value; //Data bit
infrared_receive_clear();//Clear received data first
infrared_send_hex(send_data, 5);//Send data
//Wait for response data
while( infrared_recv_flag != 1 && time_out > 0 )
{
time_out--;
delay_1ms(1);
}
if( time_out > 0 )//Not timed out
{
infrared_recv_flag = 0;
//If the response data for successful communication address modification is received
if( infrared_recv_buff[0] == 0XF2 ) return 1;
else return 2;
}
return 0;
}
/******************************************************************
* Function Name: modified_baud_cmd
* Function Description: Modify the baud rate command
* Function Parameters: baud_value is the baud rate to modify, valid values are:
* 4800, 9600, 19200, 57600
* Function Return: 0: Timed out without receiving modification success data
* 1: Modification successful
* 2: Received data is not the modification success data
* Author: LC
* Notes:
******************************************************************/
char modified_baud_cmd(unsigned int baud_value)
{
unsigned char send_data[5] = {0};//Must be initialized
unsigned int time_out = 1000; //Timeout, in ms
send_data[0] = device_addr; //Device address
send_data[1] = modified_baud; //Operation bit
switch(baud_value)//Baud rate value to modify
{
case 4800: send_data[2] = 0X01; break;
case 9600: send_data[2] = 0X02; break;
case 19200: send_data[2] = 0X03; break;
case 57600: send_data[2] = 0X04; break;
}
infrared_receive_clear();//Clear received data first
infrared_send_hex(send_data, 5);//Send data
//Wait for response data
while( infrared_recv_flag != 1 && time_out > 0 )
{
time_out--;
delay_1ms(1);
}
if( time_out > 0 )//Not timed out
{
infrared_recv_flag = 0;
//If the response data for successful baud rate setting is received
if( infrared_recv_buff[0] == 0XF3 ) return 1;
else return 2;
}
return 0;
}
/************************************************
Function Name: BSP_INFRARED_TASK
Function:
Parameters: None
Return Value: None
Author: LC
*************************************************/
void BSP_INFRARED_TASK(void)
{
while(1)
{
if(infrared_recv_flag == 0)
{
//Receive data length received by UART
infrared_recv_length = uart_read_bytes(PORT_UART, infrared_recv_buff, USART_RECEIVE_LENGTH, 10 / portTICK_PERIOD_MS);
if( infrared_recv_length > 0 )//Data length greater than 0 means data was received
{
infrared_recv_buff[infrared_recv_length] = 0;
infrared_recv_flag = 1;
}
}
delay_ms(50);
}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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
In the file bsp_infrared.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#ifndef _BSP_infrared_H_
#define _BSP_infrared_H_
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "rom/ets_sys.h"
#include "driver/gptimer.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#define GPIO_RX_PIN 2
#define GPIO_TX_PIN 1
#define PORT_UART UART_NUM_2
#define USART_RECEIVE_LENGTH 1024 //Maximum UART receive length
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void delay_1us(unsigned int us);
void delay_1ms(unsigned int ms);
void Infrared_GPIO_Init(uint32_t band_rate);//Initialize universal infrared pin
void infrared_receive_clear(void);//Clear
char Infrared_emission_cmd(unsigned char* Infrared_buff, char len);//Infrared transmission command
char modified_addr_cmd(unsigned int addr_value);//Modify UART address command
char modified_baud_cmd(unsigned int baud_value);//Modify baud rate command
void BSP_INFRARED_TASK(void);
#endif2
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
Porting Verification
Use two infrared transceiver modules: one connected to the ESP32-S3 dev board responsible for transmission, and one connected to a USB-to-TTL module to verify whether the transmitted data is correct. In the main function of your own project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-09 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_infrared.h"
//Self-test infrared signal to turn on Midea air conditioner
unsigned char Midea_Open[3] = {0XE0,0XFD,0XFD};
void app_main(void)
{
printf("demo start\r\n");
Infrared_GPIO_Init(9600);
while(1)
{
printf("\r\ndat = %d\r\n",Infrared_emission_cmd(Midea_Open,3) );
delay_ms(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
Power-on effect:
Use two infrared transceiver modules: one connected to the ESP32-S3 dev board responsible for transmission, and one connected to a USB-to-TTL module to verify whether the transmitted data is correct.
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.