Skip to Content

ESP32 And IR Remote Interface – A Complete Tutorial

ESP32 And IR Remote Interface – A Complete Tutorial

In this tutorial I will show you how to use an Infrared (IR) Remote with the ESP32 microcontroller.

I’ll cover the essential info you need to know about IR Remotes and show you how to connect an IR receiver to an ESP32.

Adding a remote to your project helps you to control the system easily. A remote is a low-cost addition that you can bring to any project. 

I will provide IR codes for a standard IR remote, working code for ESP32, along with a pinout and connection diagram. 

By the end of this article, you will have an understanding of how the IR-remote works and the skills and knowledge to begin experimenting and building your projects.

Whether you’re a beginner or an experienced maker, this article will provide a solid foundation to bring to your projects.

Let’s get started!

Components Needed To Build ESP32 And IR-Remote Project

Hardware Components

Software

Guide

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.


Basics of The ESP32 And IR-Remote

IR-Remote Fundamentals

A typical IR remote and a receiver looks like the image below.

typical IR remote and a receiver

You will find them in many appliances as well, such as ceiling fans like this:

ceiling fans

And, of course, any remote control you may use for lighting or audio devices:

remote control you may use for lighting or audio devices

It is straightforward to bring an IR remote into your next ESP32 project. 

I can share a few more ESP32 project examples where you will benefit by adding the low-cost IR option.

  1. The ESP32 controls a relay in this project. You can also bring an IR interface to control the load connected to the relay using IR! Controlling a light, opening the garage door, or even playing a radio now becomes easy with IR!
  1. TM1637 LED segment controller and ESP32 project can benefit by adding IR. Now, you can control the device, and the project can also display the selected device. So you can quickly know whether the command was accepted or not. 
  1. You can also choose the load to be controlled first and then use other buttons on the IR remote to control the load. This combines the above two projects into one and adds the IR feature to build a more sophisticated project! 

An IR remote comes with an IR transmitter and an IR receiver like this:

IR remote comes with an IR transmitter and an IR receiver

IR-Transmitter

An IR transmitter usually consists of a device with multiple buttons. Depending on the application, it can be a number or other signs. 

Below you can see one model of such IR-Transmitters. The IR-transmitter are also called remotes in general.

IR transmitter

Each remote will have 

  • Power supply (usually a coin cell battery)
  • a Keypad
  • Small internal circuitry which will generate codes on every button press
  • Transmit circuitry, which will modulate the IR LED according to the generated code
IR LED according to the generated code

As you can see, the remote will have no power when idle. Hence a single battery cell in the remote can come for years. 

I will cover the IR codes and standards in the later section. Let’s have a look at the IR receiver in the next section.

IR-Receiver

The IR receiver continuously looks for the signal from the remote. A typical IR receiver looks like the one below:

IR receiver

An IR receiver will be powered all the time. When it receives an IR signal, it will process it and convert it into digital levels (1001010010).

If it is a valid code, it can take corresponding action, such as dimming a light or turning on a particular appliance. 

A typical IR receiver can be functionally divided into three blocks:

  • IR Sensor
  • Signal Processing
  • Code Recovery
IR receiver can be functionally divided into three blocks

In the below image, you can see the IR receiver module you usually find with hobbyists.

IR receiver module you usually find with hobbyists

Let’s understand the pin definition of header pins on the IR receiver.

header pins on the IR receiver

There are three pins on the IR receiver module. The pins are 

  1. Power Supply Pin
  2. Ground Pin
  3. Data Pin

The power supply pin usually accepts voltage between 3 V and 5.5 V. Please refer to the datasheet of the module you bought for the exact voltage. 

The ground pin is the current return pin.

The data pin gives the IR codes (logic 1s and 0s). You can connect the data pin to any general-purpose input pin of the ESP32 module.

Let’s now look at the IR remote codes we have been referring to.

IR Remote Codes

Every button press on the remote corresponds to some sequence of logic 1s and 0s. 

For example, look at the image below. When you press the power button, the code sent will be 0xFFA25D. If you had pressed the MENU button, the remote would transmit 0xFFE21D.

transmit 0xFFE21D

Who defines those IR codes? Are there any standards?

Yes. 

IR remote and receiver follow standards. There are several standards for IR communication, such as NEC, SIRC, JVC, and more. I will discuss the NEC standard further. 

Every button is mapped to 32-bit data (encoded) in NEC format, as you can see in this table:

NEC IR Protocol Frame Format
AddressComplement of AddressCommandComplement of Command
0-7 bits8-15 bits16-23 bits24-31 bits

A logic ZERO → 562.5 µs high time + 562.5 µs low time

A logic ONE → 562.5 µs high time + 1.6875 ms low time

You can see how an IR signal would look when you observe it on an oscilloscope.

how an IR signal

In the below table, you can see all the codes associated with the IR remote. 

Remote KeyCommandNEC encoded data
Power1620xFFA25D
Menu2260xFFE21D
Test340xFF22DD
Plus20xFF02FD
Back1940xFFC23D
Previous2240xFFE01F
Play1680xFFA857
Next1440xFF906F
01040xFF6897
Minus1520xFF9867
C1760xFFB04F
1480xFF30CF
2240xFF18E7
31220xFF7A85
4160xFF10EF
5560xFF38C7
6900xFF5AA5
7660xFF42BD
8740xFF4AB5
9820xFF52AD

Now you know the data to be sent and the data’s frame format. 

This means even if you don’t have a remote, you can use any IR LED to frame the message and control other devices without its original remote!

You use the same table above to decode the received data from the IR receiver.

Let’s say you don’t know the IR codes of an existing device. What you can do is this:

  1. Press the IR remote and monitor the data on the IR receiver. 
  2. You can save the IR code received for that particular key. 
  3. Repeat the same for all the keys. 
  4. You can replace the original remote with your own IR transmit LED circuit. 

The device should respond to your IR commands now.

-> Read our article about How Easy Is It To Learn Arduino?

Connecting The IR Module with ESP32

In this section, we will build a project using ESP32, the IR remote, and the receiver. Let’s get started with the hardware connections.

Below in this wiring diagram, you can see the simple connections to interface the IR receiver to the ESP32 module:

connections to interface the IR receiver to the ESP32 module

I use GPIO26 to receive the IR digital output data, but you can use any pin. 

Make sure to connect the ground pins first before connecting other pins. Also, connect power only after completing all the connections.

Next, connect 4 LEDs to pins GPIO13, GPIO14, GPIO27, and GPIO12.

connect 4 LEDs to pins GPIO13, GPIO14, GPIO27, and GPIO12

Congratulations on completing the hardware connections.

-> Read also A Beginner’s Guide To ESP32: Everything You Need To Know

Code Example for IR-Remote With ESP32

You can use the code below to test the ESP32 module and the IR-Remote project. Please follow my guide to install the ESP32 core on the Arduino IDE.

#include "IRremote.h"


#define receiver 26 // IR receiver, Connect the Y pin of the Keyes IR sensor with the GPIO 26 of ESP32


int device1 = 13; // gpio 13
int device2 = 12; // gpio 12
int device3 = 14; // gpio 14
int device4 = 27; // gpio 27


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create an instance of 'irrecv'
decode_results results;            // create an instance of 'decode_results'
/*-----( Declare Variables )-----*/






void setup()
{
  Serial.begin(115200);
  Serial.println("IR Receiver Raw Data + Button Decode Test");
  irrecv.enableIRIn(); // Start the receiver


  pinMode(device1, OUTPUT);
  pinMode(device2, OUTPUT);
  pinMode(device3, OUTPUT);
  pinMode(device4, OUTPUT);
  digitalWrite(device1, LOW);
  digitalWrite(device2, LOW);
  digitalWrite(device3, LOW);
  digitalWrite(device4, LOW);




}




void loop()
{
  if (irrecv.decode(&results)) // have we received an IR signal?


  {
    Serial.println(results.value, HEX);


    //translateIR();
    if (results.value == 0x1FE50AF)
    {
      digitalWrite(device1, !digitalRead(device1));
      delay(100);
    }
    else if (results.value == 0x1FED827)
    {
      digitalWrite(device2, !digitalRead(device2));
      delay(100);
    }
    else if (results.value == 0x1FEF807)
    {
      digitalWrite(device3, !digitalRead(device3));
      delay(100);
    }
    else  if (results.value == 0x1FE30CF)
    {
      digitalWrite(device4, !digitalRead(device4));
      delay(100);
    }
    else
    {


      Serial.println("Wrong button pressed");
      delay(100);
    }


    irrecv.resume(); // receive the next value
  }
}/* --(end main loop )-- */

Let’s walk through the code. You can use the code below to easily test the ESP32 and a classic IR remote. 

This code allows ESP32 to receive signals from an IR remote.

#include "IRremote.h"

The first line includes the IRremote.h library, which provides the necessary functions for receiving and decoding IR signals.

#define receiver 26

The next line defines the GPIO PIN (26) to which the IR receiver connects. 

The following four lines define the GPIO pins to which the four devices will connect.

int device1 = 13; // gpio 13
int device2 = 12; // gpio 12
int device3 = 14; // gpio 14
int device4 = 27; // gpio 27

The next two lines create two objects: one to receive IR signals and one to hold the decoded results.

IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

The setup() function is called once when the board starts up. It sets up the serial communication at 115200 baud, initializes the IR receiver object, sets up the four device pins as outputs and sets their initial state to LOW.

The loop() function is called repeatedly while the board is running. It waits for an IR signal to be received by calling the decode() method of the IR receiver object. 

If an IR signal has been received, the code checks the decoded value against a list of predefined values. Please refer to the basics sections to know more.

If the decoded value matches one of the predefined values, the code toggles the corresponding device pin by calling digitalWrite() with the inverse of the current state of the pin (this allows the code to turn the device on or off with each button press). 

In this case, the four devices are 4 LEDs.

If the decoded value doesn’t match any predefined values, the code prints a message to the serial monitor indicating that the wrong button was pressed. 

Finally, the resume() method of the IR receiver object is called to receive the next IR signal.

    irrecv.resume(); // receive the next value

I hope this explanation helps! Let me know if you have any other questions.

FAQs About The ESP32 And IR-Remote LED Driver

I have included a list of the most frequently asked questions about projects built using the ESP32 and the IR remote. If you have more questions, please post them in the comments section. I will be glad to answer them.

What is an IR remote control?

An IR remote control transmits infrared signals to a receiver. You will connect the receiver to ESP32 in this case. The receiver converts the IR signal into voltage levels which ESP32 can understand. 

Every button on the remote will have unique codes based on which you can take the desired action, such as increasing the fan speed or dimming the light. 

How can I use an ESP32 to receive IR remote signals?

You can use an ESP32 with an IR receiver module to receive IR remote signals. The receiver module detects the infrared signals and converts them into electrical signals that the ESP32 can read. 

In this article, I have provided you with a connecting guide and working code examples to get started faster. 

How can I use an ESP32 to transmit IR remote signals?

You can use an ESP32 with an IR LED to transmit IR remote signals. The ESP32 can generate the appropriate IR codes. If you want to simulate an existing remote, you should first observe the IR codes of the remote.

What are some common IR remote protocols?

Some common IR remote protocols include NEC, Sony, RC-5, RC-6, and Philips.

How can I decode an IR remote signal using an ESP32?

You can decode the signal using an IR receiver module connected to the ESP32. The ESP32 can then analyze the signal and determine the appropriate action. In this article, you can find an example. 

How can I send an IR remote signal using an ESP32?

You can use an IR LED connected to the ESP32 to send the IR signal. The ESP32 can generate the necessary electrical signals to control the IR LED and transmit the appropriate IR codes.

Can I use an ESP32 to control multiple electronic devices with IR remote control?

Yes, you can use an ESP32 to control multiple electronic devices with IR remote control. Program the ESP32 to generate the appropriate IR codes for each device and then transmit the codes using an IR LED.

Can I use an ESP32 to receive and transmit IR remote signals at the same time?

Yes, you can use an ESP32 to receive and transmit IR remote signals at the same time. You must use separate IR LED and IR receiver modules for each function. That’s the only catch.

Are there any libraries available for IR remote control with ESP32?

Yes, there are several libraries available for IR remote control with ESP32, including the IRremoteESP8266 library, the IRremote library, and the ESP32-IRremote library.

-> Read also A Beginner’s Guide To ESP32 Programming

Conclusion

In this article, I have covered the essential info to use an IR remote with the ESP32.

I have provided IR codes, how to decode IR codes, and easy ways to find out the IR codes of any remote. 

I have provided the connecting guide and a working ESP32 IR remote example code to get started faster.

An IR remote improves how a user interacts with the devices and can be applied in a wide range of projects.

Would you like to suggest any improvement to the current article? What would you like to read about next? Please share your thoughts in the comment section.