ESP8266 - IR Remote Control

You've probably come across the infrared remote control, also called the IR remote control, when using devices like TVs and air conditioners at home... This tutorial instructs you how to use the infrared (IR) remote control and an infrared receiver to control the ESP8266. In detail, we will learn:

IR controller - IR receiver - ESP8266 NodeMCU

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×IR Remote Controller and Receiver Kit
1×CR2025 Battery
1×Jumper Wires
1×Breadboard
1×(Optional) 5V Power Adapter
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of IR Remote Control

An IR control system has two components: an IR remote controller and an IR receiver.

  • The IR remote controller is used to send commands using infrared signals.
  • The IR receiver captures and interprets these signals to control the device.

An IR kit typically consists of the two components mentioned above: an IR remote controller and an IR receiver.

IR remote controller

The IR remote controller is a portable device that transmits infrared signals. It is equipped with a keypad featuring multiple buttons:

  • Each button on the remote controller serves a distinct function or command.
  • When a button is pressed, the remote controller emits an invisible infrared signal. This signal contains a special code or pattern linked to the pressed button.
  • Since these infrared signals belong to the infrared spectrum, they cannot be seen by the human eye.
IR controller
image source: diyables.io

IR Receiver

The IR receiver module is a sensor that detects and receives the infrared signals emitted by the remote controller.

The infrared receiver detects the incoming infrared signals and converts them into the code (command) representing the button pressed on the remote controller.

The IR Receiver can be a sensor or a module. You can use the following choices:

  • IR Receiver Module only
  • IR Receiver Sensor only
  • IR Receiver Sensor + Adapter
IR Receiver sensor module
image source: diyables.io
IR Receiver sensor adapter
image source: diyables.io

IR Receiver Pinout

IR receiver module or sensor has three pins:

  • VCC pin: Connect this pin to the 3.3V or 5V pin of the ESP8266 or external power source.
  • GND pin: Connect this pin to GND pin of the ESP8266 or external power source..
  • OUT (Output) pin: This pin is the output pin of the IR receiver module. Connected to a digital input pin on the ESP8266.

How It Works

When a user presses a button on the IR remote controller, the following actions take place:

  • The IR remote controller converts the button's command into an infrared signal using a specific protocol.
  • The IR remote controller emits the encoded infrared signal.
  • The IR receiver picks up the emitted infrared signal.
  • The IR receiver decodes the infrared signal, translating it back into the original command.
  • The ESP8266 reads and receives the command from the IR receiver.
  • The ESP8266 maps the received command to the corresponding button pressed on the IR remote controller.

In summary, these steps illustrate how the IR remote controller's button press is converted into an understandable command that can be utilized by the ESP8266.

Don't worry if it seems complicated. With the assistance of the DIYables_IRcontroller library, it becomes incredibly easy.

Wiring Diagram

Wiring diagram between ESP8266 and IR Receiver Module

The wiring diagram between ESP8266 NodeMCU and IR Remote Control

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

Wiring diagram between ESP8266 and IR Receiver Sensor

The wiring diagram between ESP8266 NodeMCU and IR Remote Control

This image is created using Fritzing. Click to enlarge image

Wiring diagram between ESP8266 and IR Receiver Sensor and Adapter

Before connecting the IR receiver sensor to the ESP8266, you have the option to connect it to the adapter. This allows for easier setup and ensures proper connection between the IR receiver sensor and the ESP8266.

ESP8266 NodeMCU IR remote control receiver adapter
image source: diyables.io

How To Program For IR Remote Controller

  • Include the library:
#include <DIYables_IRcontroller.h> // Library for IR Receiver
  • Declare a DIYables_IRcontroller_17 or DIYables_IRcontroller_21 object corresponds with 17-key or 21-key IR remote controllers:
DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms // OR DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
  • Initialize the IR Controller.
irController.begin();
  • In the loop, check if a key is pressed or not. If yes, get the key
Key17 key = irController.getKey(); // if using 17-key IR controller // OR Key21 key = irController.getKey(); // if using 21-key IR controller
  • After detecting a key press, you can perform specific actions based on each key.

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ir-remote-control */ #include <DIYables_IRcontroller.h> // DIYables_IRcontroller library #define IR_RECEIVER_PIN D5 // The ESP8266 pin D5 connected to IR controller DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms void setup() { Serial.begin(9600); irController.begin(); } void loop() { Key17 key = irController.getKey(); if (key != Key17::NONE) { switch (key) { case Key17::KEY_1: Serial.println("1"); // TODO: YOUR CONTROL break; case Key17::KEY_2: Serial.println("2"); // TODO: YOUR CONTROL break; case Key17::KEY_3: Serial.println("3"); // TODO: YOUR CONTROL break; case Key17::KEY_4: Serial.println("4"); // TODO: YOUR CONTROL break; case Key17::KEY_5: Serial.println("5"); // TODO: YOUR CONTROL break; case Key17::KEY_6: Serial.println("6"); // TODO: YOUR CONTROL break; case Key17::KEY_7: Serial.println("7"); // TODO: YOUR CONTROL break; case Key17::KEY_8: Serial.println("8"); // TODO: YOUR CONTROL break; case Key17::KEY_9: Serial.println("9"); // TODO: YOUR CONTROL break; case Key17::KEY_STAR: Serial.println("*"); // TODO: YOUR CONTROL break; case Key17::KEY_0: Serial.println("0"); // TODO: YOUR CONTROL break; case Key17::KEY_SHARP: Serial.println("#"); // TODO: YOUR CONTROL break; case Key17::KEY_UP: Serial.println("UP"); // TODO: YOUR CONTROL break; case Key17::KEY_DOWN: Serial.println("DOWN"); // TODO: YOUR CONTROL break; case Key17::KEY_LEFT: Serial.println("LEFT"); // TODO: YOUR CONTROL break; case Key17::KEY_RIGHT: Serial.println("RIGHT"); // TODO: YOUR CONTROL break; case Key17::KEY_OK : Serial.println("OK"); // TODO: YOUR CONTROL break; default: Serial.println("WARNING: undefined key:"); break; } } }
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ir-remote-control */ #include <DIYables_IRcontroller.h> // DIYables_IRcontroller library #define IR_RECEIVER_PIN D5 // The ESP8266 pin D5 connected to IR controller DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms void setup() { Serial.begin(9600); irController.begin(); } void loop() { Key21 key = irController.getKey(); if (key != Key21::NONE) { switch (key) { case Key21::KEY_CH_MINUS: Serial.println("CH-"); // TODO: YOUR CONTROL break; case Key21::KEY_CH: Serial.println("CH"); // TODO: YOUR CONTROL break; case Key21::KEY_CH_PLUS: Serial.println("CH+"); // TODO: YOUR CONTROL break; case Key21::KEY_PREV: Serial.println("<<"); // TODO: YOUR CONTROL break; case Key21::KEY_NEXT: Serial.println(">>"); // TODO: YOUR CONTROL break; case Key21::KEY_PLAY_PAUSE: Serial.println(">||"); // TODO: YOUR CONTROL break; case Key21::KEY_VOL_MINUS: Serial.println("–"); // TODO: YOUR CONTROL break; case Key21::KEY_VOL_PLUS: Serial.println("+"); // TODO: YOUR CONTROL break; case Key21::KEY_EQ: Serial.println("EQ"); // TODO: YOUR CONTROL break; case Key21::KEY_100_PLUS: Serial.println("100+"); // TODO: YOUR CONTROL break; case Key21::KEY_200_PLUS: Serial.println("200+"); // TODO: YOUR CONTROL break; case Key21::KEY_0: Serial.println("0"); // TODO: YOUR CONTROL break; case Key21::KEY_1: Serial.println("1"); // TODO: YOUR CONTROL break; case Key21::KEY_2: Serial.println("2"); // TODO: YOUR CONTROL break; case Key21::KEY_3: Serial.println("3"); // TODO: YOUR CONTROL break; case Key21::KEY_4: Serial.println("4"); // TODO: YOUR CONTROL break; case Key21::KEY_5: Serial.println("5"); // TODO: YOUR CONTROL break; case Key21::KEY_6: Serial.println("6"); // TODO: YOUR CONTROL break; case Key21::KEY_7: Serial.println("7"); // TODO: YOUR CONTROL break; case Key21::KEY_8: Serial.println("8"); // TODO: YOUR CONTROL break; case Key21::KEY_9: Serial.println("9"); // TODO: YOUR CONTROL break; default: Serial.println("WARNING: undefined key:"); break; } } }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search "DIYables_IRcontroller", then find the DIYables_IRcontroller library by DIYables
  • Click Install button to install DIYables_IRcontroller library.
ESP8266 NodeMCU DIYables_IRcontroller library
  • You will be asked for installing the library dependency as below image:
ESP8266 NodeMCU IRremote library
  • Click Install all button to install the dependency
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Press keys on the remote controller one by one
  • Check out the result on the Serial Monitor.
  • When you press the keys on a 17-key IR controller one by one, the following is the result:
COM6
Send
UP DOWN LEFT RIGHT OK # 0 * 9 8 7 6 5 4 3 2 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You now have the ability to customize the code and control different devices like LEDs, fans, pumps, actuators, and more using IR remote controllers. This means you can make changes to the code to make these devices respond to your commands sent through the remote control.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!