Infrared Sensor Tutorial for Arduino, ESP8266 and ESP32

In this tutorial you learn how to use an infrared sensor in combination with the NEC Infrared Transmission Protocol.

At the end of this tutorial, you build a project with the VS1838B infrared sensor to read and encode infrared signals from a remote control.

Table of Contents

What is Infrared Radiation (IR)?

Infrared radiation (IR) or infrared light is an electromagnetic radiation (EMR) and carries radiant energy like all EMR. Although IR behaves both like a wave and like its quantum particle, the photon.
Infrared light is not visible for humans because of longer wavelength as the human eye is capable to see:

  • Wavelength infrared: 700nm to 1000nm
  • Human visible wavelength: 400nm to 700nm

The following pictures gives you an overview about the electromagnetic spectrum.

electromagnetic spectrum

But there is a possibility to see the infrared light. You can see the light via your mobile phone, because the camera of you mobile phone see the light emitted by the infrared diode. You will see that the light is flickering because the infrared diode will turn on an off very fast. There is a rhythm behind this behavior which is necessary to encode the information sent from the diode.

Therefore we can use the IR by sending information as analog signals and encode these signals with a sensor. The following chapters describe normal infrared sensors and passive infrared sensors. Moreover we will discuss the use of infrared distance modules.

NEC Infrared Transmission Protocol

The NEC IR transmission protocol uses pulse distance encoding of the message bits. Each pulse burst (mark – RC transmitter ON) is 562.5µs in length, at a carrier frequency of 38kHz (26.3µs). Logical bits are transmitted as follows:

  • Logical ‘0’ – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
  • Logical ‘1’ – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms

When a key is pressed on the remote controller, the message transmitted consists of the following, in order:

NEC Message Frame
  1. a 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
  2. a 4.5ms space
  3. the 8-bit address for the receiving device
  4. the 8-bit logical inverse of the address
  5. the 8-bit command
  6. the 8-bit logical inverse of the command
  7. a final 562.5µs pulse burst to signify the end of message transmission

The following table gives you an overview of all components and parts that I used for this tutorial. I get commissions for purchases made through links in this table.

ComponentAmazon LinkAliExpress Link
Arduino Nano AmazonAliExpress
Arduino Pro Mini AmazonAliExpress
Arduino Uno AmazonAliExpress
Arduino Mega AmazonAliExpress
ESP32 ESP-WROOM-32 AmazonAliExpress
ESP8266 NodeMCU AmazonAliExpress
ESP8266 WeMos D1 Mini AmazonAliExpress
VS1838B Infrared Sensor in Sensor Kit Amazon-
VS1838B Infrared Sensor in Sensor Kit -AliExpress

Infrared Sensor

With the help of an infrared sensor the Arduino, ESP8266 and ESP32 microcontroller is able to receive information from an infrared remote and take action based on the button you pushed on the infrared remote. Therefore the sensor scans specific frequency ranges, defined by standards, and convert these signals to the output pins of the sensor. The most basic infrared sensor is the VS1838B.

Different Infrared Libraries for Arduino and ESP8266/ESP32

For operation, the sensor receives and IR signal which have to be encoded. You can try to encode the IR signal for different manufactures for yourself, but I use a library for this task that encode the light pulse and will output a HEX code depending on the information sent by the infrared diode.

You find both libraries in your Arduino IDE. If you do not know how to install a library in your Arduino IDE, I wrote a step by step guide.

Wiring between VS1838B Infrared Sensor and Microcontroller

The following picture shows the wiring between different Arduino, ESP8266 and ESP32 microcontroller boards and the infrared sensor VS1838B.

Wiring VS1838B Infrared Sensor Arduino Nano
Wiring VS1838B Infrared Sensor Arduino Nano

For more information about the Arduino Nano, visit the Arduino Nano Tutorial.

Wiring VS1838B Infrared Sensor Arduino Pro Mini
Wiring VS1838B Infrared Sensor Arduino Pro Mini
Wiring VS1838B Infrared Sensor Arduino Uno
Wiring VS1838B Infrared Sensor Arduino Uno
For more information about the Arduino Uno, visit the Arduino Uno Tutorial.
Wiring VS1838B Infrared Sensor Arduino Mega
Wiring VS1838B Infrared Sensor Arduino Mega
For more information about the Arduino Mega, visit the Arduino Mega Tutorial.
Wiring VS1838B Infrared Sensor ESP32 ESP-WROOM-32
Wiring VS1838B Infrared Sensor ESP32 ESP-WROOM-32
Wiring VS1838B Infrared Sensor ESP8266 NodeMCU
Wiring VS1838B Infrared Sensor ESP8266 NodeMCU
Wiring VS1838B Infrared Sensor ESP8266 WeMos D1 Mini
Wiring VS1838B Infrared Sensor ESP8266 WeMos D1 Mini

Microcontroller Datasheet eBook

The 35 pages Microcontroller Datasheet Playbook contains the most useful information of 14 Arduino, ESP8266 and ESP32 microcontroller boards.

Program Code to Encode Infrared Signals from Remote Control

After we connect our devices we will go into the code. Our objective is to print out the code in HEX and Decimal (DEZ) when we push different buttons on the infrared remote control. In this example I use my TV remote control.

Because the program code differs between the Arduino and the ESP8266 and ESP32 regarding the used library, I created a box for each program code.

In the first line of the program script we include the supported infrared library. Then we have to define the digital pin for the connection between the microcontroller and the IR module. After we defined a new IR object to receive the infrared signals from the remote control and commit the connected pin to this object, we create object called results that receives the decoded infrared results from the library.

In the setup function, we set the baud rate to 9600 for the serial transmission of your USB connection between microcontroller and PC, that has to match the baud rate of the serial monitor in your Arduino IDE. The last step of the setup function is to enable the IR input to be detected.

In the loop function we call the decode function of the IR library with the address of the results variable. Therefore if the IR receiver detects a IR signal that is recoded, the function returns a TRUE to the if function and we print the HEX and decimal value of the result to the serial monitor.

For the ESP8266 and ESP32, the results is too long to print to the serial monitor. Therefore we have to split the results in the first 32 bits and the remaining bits that are printed only as HEX to the serial connection.

After we received the IR signal and printed the signal to the serial connection, we have to reset the receiver and prepare it to receive another code.

The following picture shows the results of the Arduino code, when I push some random buttons on my Samsung remote control.

Infrared Sensor Serial Monitor

With the help of this example you can identify all key codes of the remote control. After the identification you are able to react to different buttons on the remote control. One example could be that you want to turn on a red LED when you press volume up and you turn on a green LED when you press volume down on you remote control. Feel free to think about different use cases, try to program the corresponding sketch and share your project in the comment section at the end of this article.

If you plan to use infrared sensors to detect objects or measure a distance, check out the article about the ultrasonic distance sensor, the TCRT5000 line tracking module and the HC-SR501 PIR motion sensor.

4 thoughts on “Infrared Sensor Tutorial for Arduino, ESP8266 and ESP32”

  1. I have a problem with my IRrecv and my decode_results in my program.
    When I trie to upload I get 3 errors:
    ‘IRrecv’ does not name a type
    ‘decode_results’ does not name a type
    ‘class IRrecv’ has no member named ‘blink13’
    can you help?

    Reply
    • Hi Daan, IRrecv and IRrecv are classes of the IRremote library. I guess either you did not install the IRremote library from Ken Shirriff or you did not include the library in your Arduino code with #include “IRremote.h”

      Reply
  2. I am using an Arduino UNO and successfully uploaded the sketch to show the HEX and DEC codes for buttons pressed on the remote. I have tried four different remotes. The Serial Monitor (9600) shows 0 over 0 each time I press a different button on the remote. The VS1838B board receives the button press (it’s LED flashes) but the signal is not decoded. What could be the cause of this?

    Reply

Leave a Comment