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.
Component | Amazon Link | AliExpress Link |
---|---|---|
Arduino Nano | Amazon | AliExpress |
Arduino Pro Mini | Amazon | AliExpress |
Arduino Uno | Amazon | AliExpress |
Arduino Mega | Amazon | AliExpress |
ESP32 ESP-WROOM-32 | Amazon | AliExpress |
ESP8266 NodeMCU | Amazon | AliExpress |
ESP8266 WeMos D1 Mini | Amazon | AliExpress |
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.
- 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.
For more information about the Arduino Nano, visit the Arduino Nano Tutorial.
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.
#include "IRremote.h"
const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
Serial.println(results.value, DEC);
irrecv.resume();
Serial.println();
}
}
#include "IRrecv.h"
uint16_t RECV_PIN = D4; // for ESP8266 micrcontroller
//uint16_t RECV_PIN = 4; // for ESP32 micrcontroller
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
if (results.value >> 32) // print() & println() can't handle printing long longs. (uint64_t)
Serial.print((uint32_t) (results.value >> 32), HEX); // print the first part of the message
Serial.println((uint32_t) (results.value & 0xFFFFFFFF), HEX); // print the second part of the message
irrecv.resume(); // Receive the next value
}
delay(100);
}
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.
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?
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”
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?
Hi AI,
do your use to 100% the code from the example? Otherwise you can send me your code to christopher.david@diyi0t.com and I have a look.