ESP8266 NodeMCU – Simple Wifi Alarm using PIR Motion Sensor

In this tutorial we will learn how to use a PIR Motion Sensor (Passive infrared sensor) to detect if someone is inside the room. The ESP8266, will read the state of the sensor constantly. If you need help on how to start with ESP8266 check the tutorial “ESP8266 NodeMCU – Blinking a LED”.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, i will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

PIR Motion Sensor

Figure 1

The PIR Motion Sensor is a passive infrared sensor, which applies for projects that need to detect human or particle movement in a certain range. Since it is powerful and low-cost, it is widely accepted for open-source projects related to Arduino, raspberry pi, etc. This can teach beginners about PIR sensor.

The sensor, measures infrared light radiating from objects in its field of view. The HC-SR501, is often equipped with a fresnel lens to increase the feild of view, and the result being approximately like figure 2.

Figure 2

Technically, PIR sensor, is able to detect different levels of infrared radiation. Actually, the motion detector is separated by two parts. The output will swing high or low if one half see different IR radiation than the other.

In the Table below, we can see the specifications of the module:

Product Type HC–SR501 Body Sensor Module
Operating Voltage Range 5-20VDC
Quiescent Current <65mA
Level output High 3.3 V /Low 0V
Trigger L not repeated trigger/H repeated trigger
Delay time 5-300S( adjustable) Range (approximately .3Sec -5Min)
Board Dimensions 32mm*24mm
Angle Sensor <110 ° cone angle
Operation Temp. -15-+70 degrees
Lens size sensor Diameter:23mm(Default)

In figure 3, we can see the pinout of the sensor. It is very important to verify the pinout before you connect because if your sensor is from a chinese manufactor the pin order could be swapped in relatiton to one from amazon, for example. Normaly, the information is in printed in the PCB.

Figure 3

The screw potentiometers are for time and distance ajustements, respectively:

  • Adjust the distance potentiometer clockwise rotation, increased sensing distance (about 7 meters), on the contrary, the sensing distance decreases (about 3 meters).
  • Adjust the delay potentiometer clockwise rotation sensor the delay lengthened (300S), on the contrary, shorten the induction delay (5S).

NOTE: Induction module needs a minute or so to initialize. During initializing time, it will output 0-3 times. One minute later it comes into standby. Keep the surface of the lens from close lighting source and wind, which will introduce interference.

Blynk Android app

There are a lot of android aplications for IOT. We will use blynk APP. Other application for desktop can be used, but we will keep it android for now.

Blynk is a hardware-agnostic IoT platform with white-label mobile apps, private clouds, device management, data analytics, and machine learning. You should check their Website to know more about Blynk.

Figure 5

So to install and configure the app, you must follow the tutorial in their website. However if you follow the blynk tutorial, you will end up using the blynk.io server. The blynk.io server is a good option because, is safe, continuous and fast. Although blynk.io server is currently providing 2.000 of energy, which limit your Dashboard. To solve this problem, you can use public free blynk servers, from this GitHub. Remember that this servers are unreliable and can have intermittent shutdown. I will use the second server, but you can use any of them.

To change the blynk.io server for the blynk.iot-cm.com, in the login in page of the app, when choosing the server, you need to click on the “3 dots” botton and copy the server name and App port, as shown in figure 2.

Figure 6

Affter making the instructions above, you will receive an email with a token. We will use that token in the code for access to the server. To beggin, creat a new project and name it as you desire. In this tutorial, it will be “Wifi Switch”.

Figure 7

Below the project name, you need to especify the hardware. Choose the ESP8266 option.

Figura 8

Go to main screen again. Swipe the main screen to the left to open the widgets menu, and insert a Button, tow leds and a notification. Press the button to open the button settigns, and choose the virtual pin V0 and turn on the switch configuration. Press the LED to open the LED settings and choose the virtual pin V1 for the invasion LED and the vitual pin V2 for the other LED. As shown in figure 9.

Figure 9

Last but not least, press the notify to open the notify settigns, and configure just like the figure 10. You also can customize the behavior, such as, sound, vibration, ect… This widget, allows you to receive a notification every time you order in the code or when the hardware turn on or off. Be aware, that some servers do not permite to send notifications or permit a small number of notifications per day. If it is the case, you have to change to the original Blynk server or other.

Figure 10

For now on, we just have to program our devices and connect them with the blynk app, and we are ready to go.

Circuit

Figure 10

Before connect everything, remenber to verify the pinout. Connect the output of the PIR to GPIO 2 Pin D4, like figure 10. The pin VU, stands for “Voltage USB”, so you can use the five volts of your usb cable. If not possible, use an external power supply and joind the GNDs.

Coding

Copy the main sketch bellow to your Arduino IDE project and save it. The Blynk library and EPS8266 library must be included by you, I give you the links. Heve fun!!

Please follow and like us:
Ricardo Carreira

Author: Ricardo Carreira

Portuguese Electrical Engineer interested in Automation, Energy, Power generation, Renewables and 3D printers . With a degree in Electrical and Computer Engineering in the field of Energy and Automation. Currently starting in Master’s degree in Electrical Engineering.

Leave a Reply

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