Introduction: IoT Motion Detector With NodeMCU and BLYNK
This is a very simple tutorial to show how ease is to implement an IoT project with NodeMCU and Blynk.
Any time that we a movement is detected by the sensor, a message provide by Blynk is send to a smartphone.
Step 1: Bill of Material
- NodeMCU ESP12-E $8.89
- Motion Sensor HC-SR501 $2.57
- Resistor (1K, 2.2K and 330ohm)
- LED
- Breadboard
- Cables
Step 2: The HC-SR501 Passive Infrared (PIR) Motion Sensor
This motion sensor module uses the LHI778 Passive Infrared Sensor and the BISS0001 IC to control how motion is detected. The module features adjustable sensitivity that allows for a motion detection range from 3 meters to 7 meters. The module also includes time delay adjustments and trigger selection that allow for fine tuning within your application.
The bellow link is a great article that explain in details how the sensor works:
Step 3: The HW
The HW is very simple. The sensor has 3 pins (+5V, GND and Output).
It is important to point that the output of the HC-SR501 generates a logic signal of + 3.3V (HIGH) or 0V (LOW), which is COMPATIBLE with the input levels of the NodeMCU, which also works with the 3.3V level. I kept the circuit with a level converter to ensure if any other sensor with 5V output is used. If you are sure that you sensor generates 3.3V and not 5V as output you can eliminate the level converter, connecting NodeMCU Pin D1 (GPIO 5) directly to HC-SR501 Output.
We have also included a LED at pin D7 (GPIO13), for a local visualization.
Step 4: Testing the Sensor
First let's us do a simple test for testing and calibrating the sensor.
Upload the bellow code at Arduino IDE:
/* HC-SR501 Motion Detector */ #define ledPin D7 // Red LED #define pirPin D1 // Input for HC-SR501 int pirValue; // variable to store read PIR Value void setup() { pinMode(ledPin, OUTPUT); pinMode(pirPin, INPUT); digitalWrite(ledPin, LOW); } void loop() { pirValue = digitalRead(pirPin); digitalWrite(ledPin, pirValue); }
When doing a movement in front of the sensor, you will see the Red LED turn ON. You can use the above code for adjustments in the sensor if needed.
NOTE: We are supposing here that you have already prepared your Arduino IDE to handle the NodeMCU. If not, please take a look at my tutorial:
From blink to Blynk, an IoT jorney on the wings of NodeMCU ESP-12E
Step 5: Including BLYNK
Follow the bellow steps:
- Create a New Project.
- Give it a name (in my case "Motion Detector")
- Select NodeMCU as HW Model
- Copy the AUTH TOKEN to be used in the code (you can send it to your email).
- Includes a "Push Notification" Widget.
- Press "Play" (The triangle at right up corner)
Of course, the Blynk App will tel you that the NodeMCU is off line.
It's time to upload the full code at your Arduino IDE:
/************************************************************** * IoT Motion Detector with Blynk * Blynk library is licensed under MIT license * This example code is in public domain. * * Developed by Marcelo Rovai - 30 November 2016 **************************************************************/ #include <ESP8266WiFi.h> #define BLYNK_PRINT Serial // Comment this out to disable prints and save space #include <BlynkSimpleEsp8266.h> char auth[] = "YOUR AUTH CODE HERE"; /* WiFi credentials */ char ssid[] = "YOUR SSID"; char pass[] = "YOUR PASSWORD"; /* HC-SR501 Motion Detector */ #define ledPin D7 #define pirPin D1 // Input for HC-S501 int pirValue; // Place to store read PIR Value void setup() { Serial.begin(115200); delay(10); Blynk.begin(auth, ssid, pass); pinMode(ledPin, OUTPUT); pinMode(pirPin, INPUT); digitalWrite(ledPin, LOW); } void loop() { getPirValue(); Blynk.run(); } /*************************************************** * Get PIR data **************************************************/ void getPirValue(void) { pirValue = digitalRead(pirPin); if (pirValue) { Serial.println("==> Motion detected"); Blynk.notify("T==> Motion detected"); } digitalWrite(ledPin, pirValue); }
Once the code is uploaded and running, check the BLYNK app. It should be now also running.
Make a movement in front of the sensor. Now you must receive a message in your mobile as show above.
Bellow the full Arduino code for your project:
Attachments
Step 6: Conclusion
As always, I hope this project can help others find their way in the exciting world of electronics and IoT!
For more projects, please visit my blog: MJRoBot.org
Saludos from the south of the world!
See you at my next instructable!
Thank you
Marcelo

Participated in the
Arduino Contest 2016

Participated in the
Make it Glow Contest 2016

Participated in the
IoT Builders Contest
20 Comments
Question 2 years ago on Step 6
Hi , I have built the motion detection project and it works 100%. But I have a problem when I remove the usb cable and power the PIR with a 6v battery and also the board ground and VIN pin. then it does not connect to Blynk.
I checked the input from the PIR and it switches correctly when it detects movement , but the led doed not come on and Blynk says not connected.
Can you assist me please .
Thank you for a great tutorial,
Regards,
Winston
4 years ago
I know this is an older instructable, but wanted to mention there is a known issue with the biss0001 pir sensor false triggering due to rfi from the esp8266 chip. I have been developing a compact battery powered esp/pir sensor and the biss0001 has only worked well in 1 out of 5 builds. Instead, consider using the AM312 pir sensor. It is smaller, can be powered with 2.7-12VDC, and has digital signal processing with rfi filters. It is much more suitable for use near an esp8266.
4 years ago on Step 6
Can you please explain the coding after void pirvalue(void) or there is any another way to write it
5 years ago
In the loop of getpirValue the if condition no need add if pirValue=1? becasue without equal to 1 i cant get notification on blynk app
5 years ago
how to add buzzer n wiring schema?
6 years ago
Hi, Thank you for this easy instructables.
Is there a way instead of a loop to have a callback when the PIR is active?
6 years ago
can this project be used with wemos D1 mini?
Reply 6 years ago
Yes, you can. Only the PIN number must be verified.
Thanks
6 years ago
nice I didn't know blink could text you
6 years ago
I only tested the project on my lab and it worked fine. For a simple home alarm system, the project would work as it, but using SimpleTimer is a much better solution, because if constant motion is occurring the code could no work properly. A better solution would be to move the getPirValue(); into a >SimpleTimer1< loop, say every 500-1000ms, instead of the main void loop(). Thanks to Gunner (https://www.youtube.com/user/GunnersGarrison) for the comment
6 years ago
I followed the instructions, but I couldn't get a reading from the PIR until I removed the 2.2k resistor. After that it seemed to work. I then tried removing the 1k resistor as well and plugged directly into the D1 and it still worked. I thought I would need the voltage divider?
Reply 6 years ago
Hi. Connecting the PIR directly to NodeMCU MAY work. But it is not advisable. The input pins has protection to over voltage, but you must remember that you will also should limit current (at least you must add a 100 or 200 ohms resistor in series with PIR. Your circuit worked now, but could be "fried" in a near future. Electronic circuit varies a lot and sometimes one chip can work and another not. So, better work within with nominal parameters range and not near the maximum.
So, better use the voltage dividir. It should work. Disconect the PIR output pin from D1 and use a voltimeter to check the voltage level when a movement is detected. You should see the variation. Review all voltage divider connections, double checking if all GNDs are really connected. Connect the PIR now at the input of voltage divider. Connect the mid point of the dividir to the voltmeter, you should see the same variation but lower voltage level. If worked, now connect the voltage divider output to D1.
Thanks
Reply 6 years ago
It must have been a loose connection or something. It's the next day and I added the 2.2k resistor back and now I'm able to get a reading from the PIR, I'm not sure why I was unable to last night. I'll be able to click on the "I Made It!" button! Great Instructable, I was finally able to wrap my head around Blynk. I even added an LED widget in Blynk to mimic the LED on the breadboard!
6 years ago
Great project with NodeMCU!!!
Reply 6 years ago
Thanks a lot!
;-)
6 years ago
Compliments !!!
Very interesting project
Reply 6 years ago
Thanks a lot! ;-)
6 years ago
Somebody send me this project with the nodemcu module please, thanks. My e-mail: andresgiraldoroman@gmail.com
Reply 6 years ago
Hi Andrés. I do not understand what you need. Please clarify.
Thanks
Marcelo
6 years ago
I want get it!