ESP8266IoT Projects

Interfacing PIR Sensor with ESP8266 and EasyIoT

Motion sensors are high-tech security devices that you see nowadays in movies too. Now not only in movies, but we will also build the same kind of device today. So in this tutorial, we are Interfacing PIR Sensor with ESP8266 and monitor the sensor data from the EasyIoT Cloud Platform. At the end of this tutorial, you will learn to make your own powerful motion tracking device.

Interfacing PIR Sensor with ESP8266 and EasyIoT
Interfacing PIR Sensor with ESP8266

Components Required

We are going to interface PIR (Passive Infrared) Motion sensor with the ESP8266 Module. So here is the list of all the components that are required to build a motion-tracking device.

  • ESP8266 ESP-01 WiFi Module
  • Passive Infrared (PIR) Motion Sensor
  • Micro USB Cable
  • Few jumpers wire

Passive Infrared (PIR) Motion Sensor

In this project, we are going to use a Passive Infrared (PIR) Sensor. It allows you to sense motion. Generally used to detect whether a human has moved away or came to the sensors range. PIR sensor is small, inexpensive, and has low power consumption.

PIR Motion Sensor
PIR Motion Sensor

For that reason, the PIR sensor is commonly found in appliances and gadgets that we use in daily life. Further, it is also known by the name PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors.

More IoT based Projects:

How this PIR Sensor Works?

When a motion is triggered the Digital Pulse is High i.e 3Volt (When Motion Detected). Similarly, the Digital Pulse is low when there is no motion detected. Basically, the Pulse lengths are determined by resistors and capacitors on the module. The sensitivity range of PIR Sensor: up to 20 feet (6 meters) which is about 110 degrees and 70 degrees detection range.

Interfacing PIR Sensor with ESP8266

Now lets Interface PIR sensor with ESP8266-01 Module. You can also use the devices like NodeMCU development board for this tutorial. In our case, we use the ESP8266-01 module. Actually, the PIR sensor is powered by 5V, but if we look at the circuit diagram, you will see that 3.3 V is used inside. Since the ESP8266 has 3.3 V, we can bypass the internal PIR regulator.

PIR Sensor Schematic
PIR Sensor Schematic

The connection after the regulator can be found on pin 3 of the JP1 connector. In our case, we connected a 3.3 V power supply to this terminal, not 5V to the external power output. You can see this in the following circuit diagram:

Interfacing PIR Sensor with ESP8266
Circuit Diagram of PIR Sensor With ESP8266

The following are the connections you need to make:

PIR Sensor ESP8266
3.3VCC 3.3V VCC
GNDGND 
OUTGPIO2 on ESP8266
Connecting PIR Sensor With ESP8266

More Arduino based projects that you may like:

Configure EasyIoT Cloud Platform

Sign up for the EasyIoT Cloud service. If you do not receive a confirmation of registration by e-mail, check the spam folder. If it is not there, contact the owners of the resource and they will activate your account.

Then go to Configure-> Modules-> Add Module . Set Digital Input (DI) – digital input as the type of module and the name of its PIR sensor.

Interfacing PIR Sensor with ESP8266

Then click on Sensor.Parameter1. Set the “Description” for the PIR and check the UI messages in order to receive real-time messages via the web interface:

Interfacing PIR Sensor with ESP8266 and EasyIoT

Click the Save parameter button. After saving, you will see InstanceId / ParameterId. Write it down – we’ll need it later in the code:

EasyIOT setup

Return to the configuration module and add two more parameters (via the Add parameter button). These are two options to specify the text in the module. The name of the first parameter Settings.StatusText1, and the second Settings.StatusText2. Set the value for the first as “Motion” and for the second as “Ok“.


Next, set the image for the module. To do this, add two more parameters and name them Settings.Icon1 and Settings.Icon2.

Now, Set the first parameter to “siren_2.png” and similarly, the second parameter to “siren_1.png“. After adding all the parameters, the module configuration should look like below image:

PIR EasyIoT Setup

Program sketch/Code

The program is written in Arduino IDE Software. Refer to the Arduino ESP8266 IDE manual to learn how to connect the ESP8266 module to your computer to download the program. The program is available on GitHub. You will also need the EIoTCloudRestApi library. In the library, enter the Access Point Username and Password.

In the code, overwrite EIOT_CLOUD_INSTANCE_PARAM_ID:

#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxxx"

Also, Set the InstanceId/ParameterId that you have set in your parameter configuration.

Parameter configuration
#include <ESP8266WiFi.h>
#include "EIoTCloudRestApi.h"

// EasyIoT Cloud definitions - change EIOT_CLOUD_INSTANCE_PARAM_ID
#define EIOT_CLOUD_INSTANCE_PARAM_ID    "xxxx"

#define INPUT_PIN        2

EIoTCloudRestApi eiotcloud;
bool oldInputState;

void setup() {
  Serial.begin(115200);
  eiotcloud.begin();

  pinMode(INPUT_PIN, INPUT_PULLUP);
    
  oldInputState = !digitalRead(INPUT_PIN);
}

void loop() {
  int inputState = digitalRead(INPUT_PIN);;  
  
  if (inputState != oldInputState)
  {
    eiotcloud.sendParameter(EIOT_CLOUD_INSTANCE_PARAM_ID, (inputState == 1));
    oldInputState = inputState;
  }
}

If you change the GPIO pin on the ESP8266, then change the GPIO pin in the program as well.

#define INPUT_PIN        2

Now compile your program and upload it to the board. But, make sure you have selected correct board and correct COM port.

Conclusion

Finally, we have completed Interfacing PIR Sensor with ESP8266 and EasyIoT. Now, you have made your own powerful motion tracking device. I hope you found this project useful! Drop a comment below if you have any questions. l will do my best to answer them.

Related Articles

2 Comments

  1. Pingback: Battery Status Monitoring System using ESP8266 & Arduino IoT Cloud

Leave a Reply

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

Back to top button