ESP32

ESP32 Home Automation using IR Remote and Keyes IR Sensor

ESP32 Home Automation Using IR Remote:

 

ESP32 Home Automation using IR Remote and Keyes IR Sensor– Previously, I used an IR Sensor and IR Remote controller with Arduino for controlling an Electronic Door lock and for controlling the speed of a DC Fan. I just love controlling things wirelessly over long distances using the Internet as the communication medium and over a short distance using Bluetooth or other RF transceiver modules. You might have read my articles on ESP32 and ESP8266. ESP32 and ESP8266 WiFi modules are 3.3V compatible controller boards and I have been using these controller boards in IoT based projects for controlling electrical devices from anywhere in the world using different IoT platforms including the Blynk application, Thingspeak, Ubidots, IFTTT, Google Assistant, Firebase, and Telegram, etc. If you are using any of these IoT platforms or any other IoT platform, then you can only control the devices when the controller board is connected with the WiFi.

Why use WiFi when we are inside a room, we can use a Bluetooth-supported android application to control different electrical loads or we can use an IR remote controller. I have already explained how to control different electrical loads wirelessly using the ESP32 built-in Bluetooth module and an android application. So, if you want to control your home appliances using Bluetooth and android application then go ahead and read my article.


 

ESP32 Home Automation using IR Remote

Today, I am going to use the same ESP32 Development board with this small-size IR remote controller and IR Sensor for controlling these 4 different colored 220Vac light bulbs. For demonstration purposes, I am using only 4 lights, if you want you can increase or decrease the number of lights, or you can use some other electrical loads.

ESP32 Home Automation using IR Remote

Anyways, before connecting all the four lights with the relays, first I checked all the 4 lights and by the way, be very careful while working with 220Vac because it can be really dangerous. After checking all the four lights I disconnected the 220Vac supply and I started connecting all the 4 lights with the relays. Don’t worry I will explain all the connections with the help of a circuit diagram.

Anyways, I am using a 4S lithium-Ion battery pack for powering up my ESP32 Development board, if you want to make the same 3S or 4S lithium Ion Battery pack then you can read my article on “how to make 3S and 4S lithium Ion Battery Packs”. You use any DC power supply between 9 volts and 25 volts as my designed ESP32 development board has a 5V regulator. So, once everything is powered up, then we can use the IR remote controller to control all the four lights.



 

Altium Designer:

altium designer sponsor

Altium Designer is the world’s most trusted PCB design system. Altium Designer enables engineers to effortlessly connect with every facet of the electronics design process. Over 35 years of innovation and development focused on a truly unified design environment makes it the most widely used PCB design solution. With Altium Designer you can create PCB designs with an intuitive and powerful interface that connects you to every aspect of the electronics design process. Route it your way through any angle, tune for the delay, Push, Slide, and Walkaround faster than ever. Interact and collaborate with mechanical designers like never before in a photo-realistic, 3D design environment. If you want to get started with the Altium designer, you can click on the get started.

ESP32 Home Automation using IR Remote

Since I am controlling 4 lights so that’s why I am using only 4 buttons; button1, button2, button3, and button4 on the IR remote controller. You can use the same buttons to turn ON and turn OFF all the four lights. This IR remote controller has more than enough buttons which can be used to control all the electrical loads inside a room. I have been testing this project for hours, there is no false triggering, and the ESP32 controller board is not hanging at all. Now, it’s time to explain the circuit diagram and programming. Without any further delay, let’s get started!!! 


 

Amazon Links:

12v Adaptor:

ESP32 WiFi + Bluetooth Module(Recommended)

LM7805 Voltage Regulator:

470uf capacitor:

DC Female Power Jack:

Female Headers:

1n4007 diode:

10k Resistor:

2n2222 NPN transistor

12V SPDT Relay:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


 

ESP32 WiFi Module:

ESP32 is already integrated antenna and RF balun, power amplifier, low-noise amplifiers, filters, and power management module. The entire solution takes up the least amount of printed circuit board area. This board is used with 2.4 GHz dual-mode Wi-Fi and Bluetooth chips by TSMC 40nm low power technology, power and RF properties best, which is safe, reliable, and scalable to a variety of applications. Features: High performance-price ratio Small volume, easily embeded to other products Strong function with support LWIP protocol, Freertos Supporting three modes: AP, STA, and AP STA Supporting Lua program, easily to develop.

ESP32 Home Automation using IR Remote

If this is your first time using ESP32 WiFi Module then you will need to install the ESP32 board.


 

KY-022 IR remote control sensor:

The KY-022 module has integrated with the onboard an infrared receiver, one resistor and one LED. 1 Kilo ohm resistor is used in this module to limit current circulating inside the module so that the large current not damages the module. The led in the module will be light up when it receive infrared signal which will show us that the module is working.

ESP32 Home Automation using IR Remote

Now I will show how these components are connected together. In the circuit diagram we can see that the Resistor is connected with the led to limit the current.

ESP32 Home Automation using IR Remote

Now we can see that there is three pin of the sensor which are:

  • Ground
  • Voltage
  • Signal

We have in red the flow of the voltage and we have in black the flow of the ground and we have in green the flow of the signal. This sensor works when it receive infrared signal from the transmitter. This sensor can receive infrared signals from a source that is away at 8 meters max after receiving the infrared signal the sensor will encode and amplify the signal received to be suitable for transmitting via a low voltage wiring. This module has a frequency of 38 kilohertz. This module gives a digital signal so we can connect the pins of the signal with any digital parts of different microcontroller boards like Arduino, ESP32 or Raspberry Pi.



 

Circuit diagram:

Interfacing the KY-022 with ESP32:

To interface Keyes IR sensor “KY-022” with the ESP32 we will connect the signal pin of the KY-022 with the GPIO pin 26. Connect the ground pin of the KY-022 with the ground of the ESPP32. Connect the VCC of the KY-022 with the 3V of the ESP32.

ESP32 Home Automation using IR Remote

This is the most basic circuit diagram for getting started if in case you don’t to use relays.


 

ESP32 and IR Remote based Home Automation Circuit Diagram:

ESP32 Home Automation using IR Remote

J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. The output of the voltage regulator is connected with the 5v pin of the ESP32 module and is also connected with the R pin of the IR sensor.  The ground of the power supply is connected with the ground of the ESP32 module and also with the G pin of the IR Sensor which is the ground pin. So, make sure all the grounds are connected together. The Y pin of the IR sensor is connected with the GPIO 26 of the ESP32 module.

These are 12v SPDT type relays and can’t be directly controlled using the ESP32 Module, So, that’s why we need a driver to control these relays. You can use a relay driver IC or you can use 2n2222 NPN transistor and a 10k resistor. One pin of the relay coil is connected with the collector of the 2n2222 NPN transistor while the other pin of the relay coil is connected with the 12 volts. The emitter of the transistor is connected with the ground while the base is connected with the 10k ohm resistor.

Now to control these relays you simply need to connect these 10k resistors with the ESP32 I/O pins. In this project, I am using the GPIO pins 13, 12, 14, and 27. I will be using the same pins in the programming.

The neutral wire from the 110/220Vac supply is connected with the neutral of all the lights. While the Live wire from the AC supply is connected with the lights through these relays.

ESP32 Home Automation using IR Remote

Here is my ESP32 development board, if you want to make the same development board then you can read my article.


 

ESP32 and IR Remote based Home Automation Programming:

#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 instance of 'irrecv'
decode_results results;            // create 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 )-- */


 

Code explanation:

Before, you start the programming, first of all, make sure you install the IRremote library. For this click on the Sketch menu, go to Include Library, and then click on the Manage Libraries.

ESP32 Home Automation using IR Remote

Search for the IRremote library and install the latest version.

ESP32 Home Automation using IR Remote

As you can see I have already installed the IRremote library.

#define receiver 26

The IR remote Y pin is connected with the GPIO 26 of the ESP32 Module. Next, I defined four pins to which all 4 relays are connected.

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



 

Inside the setup function, I set the baud rate and set all the devices as the output.

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);

Inside the loop function we check if any data is received from the IR Sensor.

if (irrecv.decode(&results))

Let’s say, if a button is pressed on the IR remote then this line of code

Serial.println(results.value, HEX); will print its hex value. If you want to use any other IR remote then simply upload this program, open the serial monitor and start pressing the buttons, the buttons hex codes will be displayed on the serial monitor.

ESP32 Home Automation using IR Remote

Then you can copy those codes and use them in the programming. This is the code “0x1FE50AF” of button1, this is the code “0x1FED827” of button2, this is the code “0x1FEF807” of button3, and this is the code” 0x1FE30CF” of button4. If you want to use button5 then simply open the serial monitor, press button5 on the IR remote, do it at least 4 to 5 times to confirm the code. Then copy that code and write another if condition. It’s just that simple.

The rest of the program is pretty straightforward, we check the code and turn ON or turn OFF the desired load. So, that’s all for now.


 

Finally, I uploaded the code and then using the IR Remote I was able to control all the 4 lights. For the practical demonstration watch video tutorial given below.

ESP32 Home Automation using IR Remote

 

Watch Video Tutorial on YouTube

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button