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.
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:
- a 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
- a 4.5ms space
- the 8-bit address for the receiving device
- the 8-bit logical inverse of the address
- the 8-bit command
- the 8-bit logical inverse of the command
- 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.
|Arduino Pro Mini
|ESP8266 WeMos D1 Mini
|VS1838B Infrared Sensor in Sensor Kit
|VS1838B Infrared Sensor in Sensor Kit
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.
- For Arduino microcontroller use the library “IRremote” from Ken Shirriff
- For ESP8266 and ESP32 microcontroller use the library “IRremoteESP8266” from David Conran
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.
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.
const int RECV_PIN = 7;
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.
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.