Table of Contents
Introduction
Hello guys, hope you doing fine.
Did you know what is a PIR sensor and how it is used to make a security system? Well in this article we are going to make an IoT motion detector with NodeMCU and Blynk app.
For making this we are using a NodeMCU esp8266 board and a PIR motion sensor. You can also check some more projects on IoT.
PIR stands for passive infrared radiation and it generates high output when capturing some movement. Check Arduino-based home security system using PIR sensor made by us.
IoT Motion detector Working
- You have to provide your network’s SSID and PASSWORD so that the NodeMCU can send data over the internet. Also, specify your authentication token number.
- When the PIR sensor detects some motion near it the red LED will turn on and the buzzer will start beeping.
- The green LED will glow in normal conditions.
Please check how to use Arduino IDE for uploading the code in NodeMCU.
A preview image of the setup so, you can get an idea that what would you need.
Open the serial monitor to see the activities and the IP address.
Simulation
Activate PIR Sensor
Turn on the PIR sensor by clicking the on button.
If it detects some movement near it or in its range then an alert notification will send by the app on the smartphone.
Set up the Blynk app
You have to install the Blynk IoT app and then open the app. Create a new project by clicking on the box whose image is given below.
Then name the project as you want and select NodeMCU as the device. Choose WiFi as the connection type. Tap on create to create your project.
The Blynk app will send a unique authentication code automatically to your given email address. Then press ok to continue.
Add token to mail
Now tap on the + symbol to open the widget’s box.
Create new button
Select a button and a notification widget as shown in the image below. Adjust the position of both widgets on the screen.
Open the configuration settings by tapping on the highlighted button.
Tap on the button and set the pin to V0. You can name the button as an on/off switch. Make it a switch, not a push-button. For notification, do the setting as given in the image below.
Click the marked button so that the app start working and your NodeMCU can link through it.
Components Required
- NodeMCU esp8266 board
- PIR sensor
- LEDs and 220-ohm resistors
- Buzzer
- Breadboard and Jumper wires
- USB cable for uploading the code
- Smartphone with a good internet connection
IoT motion sensor Circuit diagram:-
Connection is clear and easy to make. make sure to turn of the power supply during the connection. still if you have any doubt you can ask in the comment section.
Connection Table
Nodemcu esp8266 | PIR Motion Sensor | ||
VV, Vin | VCC | ||
G, GND | GND | ||
D5 Pin | OUT Pin | ||
Nodemcu esp8266 | Buzzer | ||
GND | Negative | ||
D2 Pin | Positive | ||
Nodemcu | LED R | LED G | 220 Ohm Resistor |
D1 Pin | Anode Pin | ||
D3 Pin | Anode Pin | ||
Cathode Pin | Cathode Pin | Terminal 1 | |
GND ( Ground ) | Terminal 2 |
Connections:-
- VCC pin of the PIR -> VIN pin of the NodeMCU
- GND pin of the PIR -> GND pin of the NodeMCU
- OUT pin of the PIR -> digital-5 pin of the NodeMCU
- Positive pin of the buzzer -> digital-2 pin of the NodeMCU
- Positive pin of the red LED -> digital-1 pin of the NodeMCU
- Positive pin of the green LED -> digital-3 pin of the NodeMCU
- GND pin of the NodeMCU -> negative pin of the buzzer and LEDs
Code for the Project
Note: Please upload the code which is given below to the NodeMCU. You have to install <ESP8266WiFi.h> and <BlynkSimpleEsp8266.h> libraries first. Check here how to install zip libraries to the Arduino IDE.
//TECHATRONIC.COM
// BLYNK LIBRARY
// https://github.com/blynkkk/blynk-library
// ESP8266 LIBRARY
// https://github.com/ekstrand/ESP8266wifi
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "54wl_i7SD0dMWrXBopu0sBt"; //Enter your Blynk application auth token
char ssid[] = "DESKTOP"; //Enter your WIFI name
char pass[] = "asdfghjkl"; //Enter your WIFI passowrd
BlynkTimer timer;
int pinValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(D1, OUTPUT); // RED LED
pinMode(D2, OUTPUT); // BUZZER
pinMode(D3, OUTPUT); // LED GREEN
pinMode(D5, INPUT); // PIR SENSOR OUTPUT PIN D5
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, notifiaction);
}
BLYNK_WRITE(V0)
{
pinValue = param.asInt();
}
void notifiaction()
{
bool sensor = digitalRead(D5); // PIR SENSOR OUTPUT PIN D5
Serial.println(sensor);
if (pinValue == 1)
{
Serial.println("System is ON");
if (sensor == 1)
{
Blynk.notify("WARNING! Please check your security system");
digitalWrite(D1, HIGH); // LED RED ON
digitalWrite(D2, HIGH); // BUZZER ON
digitalWrite(D3, LOW); // LED GREEN OFF
}
else if (sensor == 0)
{
digitalWrite(D1, LOW); // LED RED OFF
digitalWrite(D2, LOW); // BUZZER OFF
digitalWrite(D3, HIGH); // LED GREEN ON
}
}
else if (pinValue == 0)
{
Serial.println("System is OFF");
digitalWrite(D3, LOW); // LED GREEN OFF
}
}
void loop() {
Blynk.run();
timer.run();
}
If you like this project please tell us in the comments section below. Now try to make it on your own. You can also check tutorials on Arduino and Raspberry PI written by us.
Video Sample
Compilation error: ‘class BlynkWifi’ has no member named ‘notify’
how can i solve this error?