HOW to Use IR Remote Control with ESP32
Introduction
In the previous experiments, we learned how to turn on/off the LED and adjust its brightness via PWM and print the button value of the IR remote control in the serial monitor window. Herein, we use an infrared remote control to turn on/off an LED.
Components
|
|
|
|
---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio DIY Purple LED Module*1 |
Keyestudio DIY IR Receiver*1 |
|
|
|
|
Micro USB Cable*1 |
IR Remote Control*1 |
3P Dupont Wire*2 |
|
Connection Diagram
Test Code
//**********************************************************************************
/*
* Filename : IR Control LED
* Description : Remote controls LED on and off
* Auther : http//www.keyestudio.com
*/
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
const uint16_t recvPin = 15; // Infrared receiving pin 15
IRrecv irrecv(recvPin); // Create a class object used to receive class
decode_results results; // Create a decoding results class object
int led = 4;//LED connect to GP4
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(led, OUTPUT);
}
////////////////////
void loop() {
if(irrecv.decode(&results)) { // Waiting for decoding
serialPrintUint64(results.value, HEX);// Print out the decoded results
Serial.print("");
handleControl(results.value); // Handle the commands from remote control
irrecv.resume(); // Receive the next value
}
}
void handleControl(unsigned long value) {
if (value == 0xFF6897) // Receive the number '1'
{
digitalWrite(led, HIGH);//turn on LED
Serial.println(" led on");
}
else if (value == 0xFF9867) // Receive the number '2'
{
digitalWrite(led, LOW);//turn off LED
Serial.println(" led off");
}
}
//**********************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. Open the serial monitor and set the baud rate to 9600. Press the button 1 of the remote, which will be displayed on the monitor, and the LED will be on. Similarly, press the button 2 , the LED will be off.