Chapter 23 Infrared Remote
In this chapter, we’ll learn how to use an infrared remote control, and control a LED.
Project 23.1 Infrared Remote Control
First, we need to understand how infrared remote control works, then get the command sent from infrared remote control.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Jumper M/M
|
Infrared Remote x1 |
Resistor 10kΩ x1 |
Infrared Remote x1 (May need CR2025 battery x1, please check the holder)
|
||
Component knowledge
Infrared Remote
An infrared(IR) remote control is a device with a certain number of buttons. Pressing down different buttons will make the infrared emission tube, which is located in the front of the remote control, send infrared ray with different command. Infrared remote control technology is widely used in electronic products such as TV, air conditioning, etc. Thus making it possible for you to switch TV programs and adjust the temperature of the air conditioning when away from them. The remote control we use is shown below:
Infrared receiver
An infrared(IR) receiver is a component which can receive the infrared light, so we can use it to detect the signal emitted by the infrared remote control. DATA pin here outputs the received infrared signal.
When you use the infrared remote control, the infrared remote control sends a key value to the receiving circuit according to the pressed keys. We can program the ESP32-WROVER to do things like lighting, when a key value is received.
The following is the key value that the receiving circuit will receive when each key of the infrared remote control is pressed.
Circuit
Schematic diagram |
|
Hardware connection |
|
|
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “23.1_Infrared_Remote”. Select “irrecvdata.py”, right click your mouse to select “Upload to /”, wait for “irrecvdata.py” to be uploaded to ESP32-WROVER and then double click “Infrared_Remote.py”.
23.1_Infrared_Remote
Click “Run current script”, press the key of the infrared remote and the key value will be printed in “Shell”. As shown in the illustration below:
The following is the program code:
1from irrecvdata import irGetCMD
2
3recvPin = irGetCMD(15)
4try:
5 while True:
6 irValue = recvPin.ir_read()
7 if irValue:
8 print(irValue)
9except:
10 pass
Import the infrared decoder.
1from irrecvdata import irGetCMD
Associate the infrared decoder with Pin(15).
1recvPin = irGetCMD(15)
Call ir_read() to read the value of the pressed key and assign it to IRValue.
1irValue = recvPin.ir_read()
When infrared key value is obtained, print it out in” Shell”.
1while True:
2 irValue = recvPin.ir_read()
3 if irValue:
4 print(irValue)
Reference
- Class irrecvdata
Before each use of the object irrecvdata , please add the statement “ from irrecvdata import irGetCMD “ to the top of the python file.
irGetCMD(): Object of infrared encoder, which is associated with Pin(15) by default.
ir_read(): The function that reads the key value of infrared remote. When the value is read, it will be returned; when no value is obtained, character None will be returned.
Project 23.2 Control LED through Infrared Remote
In this project, we will control the brightness of LED lights through an infrared remote control.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Jumper M/M
|
Infrared Remote x1 |
Resistor 10kΩ x1 |
Infrared Remote x1 (May need CR2025 battery x1, please check the holder)
|
Active buzzer x1
|
|
LED x1
|
Resistor 1kΩ x2 |
NPN transistorx1 (S8050) |
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
The Code controls the brightness of the LED by determining the key value of the infrared received.
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “23.2_Control_LED_through_Infrared_Remote”. Select “irrecvdata.py”, right click your mouse to select “Upload to /”, wait for “irrecvdata.py” to be uploaded to ESP32-WROVER and then double click “Control_LED_through_Infrared_Remote.py”.
23.2_Control_LED_through_Infrared_Remote
Click “Run current script”. When pressing “0”, “1”, “2”, “3” of the infrared remote control, the buzzer will sound once, and the brightness of the LED light will change correspondingly.
Rendering
The following is the program code:
1from machine import Pin,PWM
2import time
3from irrecvdata import irGetCMD
4
5ledPin=PWM(Pin(14,Pin.OUT),10000)
6buzzerPin=Pin(13,Pin.OUT)
7recvPin = irGetCMD(15)
8
9def handleControl(value):
10 buzzerPin.value(1)
11 time.sleep_ms(100)
12 buzzerPin.value(0)
13
14 if value == '0xff6897': #0
15 print('0')
16 ledPin.duty(1)
17 elif value == '0xff30cf': #1
18 print('1')
19 ledPin.duty(100)
20 elif value == '0xff18e7': #2
21 print('2')
22 ledPin.duty(300)
23 elif value == '0xff7a85': #3
24 print('3')
25 ledPin.duty(1000)
26 else:
27 return
28try:
29 while True:
30 irValue = recvPin.ir_read()
31 if irValue:
32 print(irValue)
33 handleControl(irValue)
34except:
35 ledPin.deinit()
The handleControl() function is used to execute events corresponding to infrared code values. Every time when the function is called, the buzzer sounds once and determines the brightness of the LED based on the infrared key value. If the key value is not “0”, “1”, “2”, “3”, the buzzer sounds once, but the brightness of LED will not change.
1def handleControl(value):
2 buzzerPin.value(1)
3 time.sleep_ms(100)
4 buzzerPin.value(0)
5
6 if value == '0xff6897': #0
7 print('0')
8 ledPin.duty(1)
9 elif value == '0xff30cf': #1
10 print('1')
11 ledPin.duty(100)
12 elif value == '0xff18e7': #2
13 print('2')
14 ledPin.duty(300)
15 elif value == '0xff7a85': #3
16 print('3')
17 ledPin.duty(1000)
18 else:
19 return
Each time the key value of IR remote is received, function handleControl() will be called to process it.
1try:
2 while True:
3 irValue = recvPin.ir_read()
4 if irValue:
5 print(irValue)
6 handleControl(irValue)
7except:
8 ledPin.deinit()














