Menu Close

PIR Motion Sensor with Blynk-NodeMCU

This tutorial of Robo India explain an IoT Project of Motion Detection with NodeMCU and Blynk. 

1. Introduction 

The tutorial PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use. They are commonly found in appliances and gadgets used in homes or businesses.

The PIR motion sensor is ideal to detect movement. By this tutorial, anytime when the movement is detected by the sensor, a message is provided by Blynk App.

2 Hardware required

S.No.ItemQuantity
1NodeMCU 1
2PIR motion sensor 1
3Male to Female Jumper wire 3

3. Connection

S.No.NodeMCU
PIR Sensor
1GND GND
2Vin VCC
3D1 Out Pin

4. Programming :

Before uploading, make sure to paste your authorization token into the auth [] variable. Also make sure to load your Wifi network settings into the ssid[] and pass[] variables.

You may download this code (Arduino Sketch) from here.

//Robo India Tutorial on Motion Detection With Blynk App
//Hardware Required: NodeMCU & PIR Sensor
//Software: Blynk App
//http://roboindia.com/tutorials/

#include <ESP8266WiFi.h>
#define BLYNK_PRINT Serial       
#include <BlynkSimpleEsp8266.h>
char auth[] = "Your Auth Key";

/* WiFi credentials */
char ssid[] = "Your Network";
char pass[] = "Your Password";

/* HC-SR501 Motion Detector */
#define pirPin 5                // Input for HC-S501
int pirValue;                   // Place to store read PIR Value
int pinValue;                   //Variable to read virtual pin

BLYNK_WRITE(V0)
{
 pinValue = param.asInt();    
} 

void setup()
  {
    Serial.begin(115200);
    delay(10);
    Blynk.begin(auth, ssid, pass);
    pinMode(pirPin, INPUT);
  }

void loop()
  {
     if (pinValue == HIGH)    
      {
        getPirValue();
      }
    Blynk.run();
  }

void getPirValue(void)        //Get PIR Data
  {
   pirValue = digitalRead(pirPin);
    if (pirValue) 
     { 
       Serial.println("Motion detected");
       Blynk.notify("Motion detected");  
     }
  }


5. On Blynk App 

After the code has been uploaded to the NodeMCU. Following steps are to be performed on Blynk App.

5.1 Create a new project and give it name Motion Detector and then Select device “NodeMCU”.

5.2 Add widget “Notification” to get notification of motion detection.

5.3 As the Motion Detector sends data continously, we can receive Blynk notification as many time as well. So, to avoid this repeatedly received notification we have added an alert button. When we set this button HIGH, a message is provided by Blynk.

5.4 After uploading the Arduino IDE code, press the play button on Blynk app for output.

6. Output

When you press Button on Blynk app, your smartphone start getting notification of Motion Detection. When you want to control these notification, again press the alert button on Blynk app.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com


Leave a Reply