Quick answer: to get a phone notification from Blynk when a PIR sensor detects motion, open your template in Blynk.Console → Events & Notifications and create an event with the code motion_detected. Turn on Notifications for it (push to the device owner) and Send to Timeline. In the ESP32 sketch, call Blynk.logEvent("motion_detected", "Motion at the front door") only on a LOW→HIGH change of the PIR output, with a cooldown. Blynk counts events against a daily limit (commonly reported as 100 per device per rolling 24 hours), so a sketch that calls logEvent on every loop pass uses up the limit within minutes and notifications stop.
This guide wires an HC-SR501 PIR to an ESP32, sets up the event, and gives you a complete sketch with warm-up handling, rising-edge detection, a cooldown, an arm/disarm switch in the app, and a motion indicator on the dashboard.
| What you need | Notes |
|---|---|
| ESP32 dev board | ESP8266 works with BlynkSimpleEsp8266.h |
| HC-SR501 PIR module | 5 V supply, 3.3 V logic output, safe for ESP32 GPIO |
| Blynk account + Blynk IoT phone app | Notifications arrive through the app |
| Jumper wires, micro-USB cable |
How a PIR sensor detects motion
A PIR (passive infrared) sensor doesn't send anything out; it watches infrared (heat) radiation. Inside the dome, a pyroelectric element has two halves, and the faceted Fresnel lens splits the room into many zones. When a warm body moves from one zone to the next, one half sees more heat than the other, and the module's BISS0001 chip turns that difference into a HIGH output.
| What this means | In practice |
|---|---|
| It detects changes in heat, not presence | Someone sitting perfectly still stops triggering it |
| Movement across zones works best | Walking past is detected further away than walking straight at it |
| Any moving heat source counts | Pets, heaters cycling, sun patches and warm air from vents can trigger it |
| Output is a simple digital HIGH/LOW | No library needed: digitalRead() is enough |
That last point is why the logic in your sketch matters more than the sensor: the module only reports "something warm moved", and your code decides when that becomes a notification.
1. Wire the PIR sensor
| HC-SR501 pin | ESP32 pin |
|---|---|
| VCC | 5V / VIN |
| OUT | GPIO27 |
| GND | GND |
Set up the module before you write any code:
| Control | Set to | Why |
|---|---|---|
| Sensitivity pot | Middle, then adjust | Range is roughly 3–7 m |
| Time-delay pot | Fully anticlockwise (shortest, a few seconds) | Let the sketch's cooldown handle repeats |
| Trigger jumper | H (repeat trigger) | Output stays HIGH while motion continues, giving one clean edge per visit |
After power-up the HC-SR501 needs up to about a minute to stabilise, and it may trigger randomly during that time. The sketch ignores the first 60 seconds.
2. Create the event and datastreams in Blynk.Console
- Templates → your template → Events & Notifications → + Add New Event. Event name: Motion detected. Event code:
motion_detected(the code, not the name, goes in your sketch). Type: Warning. - In the event's Notifications tab, turn on Enable notifications and add Device Owner as a push recipient. Optionally limit how often it may notify.
- Turn on Send event to Timeline so you can see every event in the app, even ones that didn't notify.
- Datastreams: add
V0Motion (integer 0–1),V1Last motion (string) andV2Armed (integer 0–1, default 1). - Dashboard: an LED widget on V0, a Label on V1 and a Switch on V2. Add the same widgets in the phone app.
- On your phone, allow notifications for the Blynk IoT app in the system settings.
3. Upload the sketch
Fill in the three BLYNK_ lines from your device's Device Info tab (they must stay above the #include lines), plus your Wi-Fi details. The sketch samples the PIR every 100 ms with BlynkTimer, so there is no delay() to break the cloud connection.
#define BLYNK_TEMPLATE_ID "TMPLxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "PIR Alerts"
#define BLYNK_AUTH_TOKEN "REPLACE_WITH_DEVICE_AUTH_TOKEN"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "REPLACE_WITH_WIFI";
char pass[] = "REPLACE_WITH_WIFI_PASSWORD";
const int PIR_PIN = 27;
const unsigned long WARMUP_MS = 60000; // ignore the PIR while it stabilises
const unsigned long COOLDOWN_MS = 60000; // at most one notification per minute
BlynkTimer timer;
bool armed = true;
bool previousMotion = false;
unsigned long lastAlertAt = 0;
bool alertedOnce = false;
BLYNK_WRITE(V2) { armed = param.asInt(); } // arm/disarm switch in the app
BLYNK_CONNECTED() { Blynk.syncVirtual(V2); } // restore the switch after a reboot
void checkMotion() {
bool motion = digitalRead(PIR_PIN) == HIGH;
unsigned long now = millis();
if (now < WARMUP_MS) { previousMotion = motion; return; }
if (motion != previousMotion) Blynk.virtualWrite(V0, motion ? 1 : 0); // dashboard LED
bool risingEdge = motion && !previousMotion;
previousMotion = motion;
if (!risingEdge) return;
Serial.println("Motion detected");
if (!armed) return;
if (alertedOnce && now - lastAlertAt < COOLDOWN_MS) {
Serial.println("Suppressed (cooldown)");
return;
}
lastAlertAt = now;
alertedOnce = true;
if (Blynk.connected()) {
Blynk.logEvent("motion_detected", "Motion detected at the front door");
Blynk.virtualWrite(V1, String(now / 1000) + " s after boot");
}
}
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(100L, checkMotion);
}
void loop() {
Blynk.run();
timer.run();
}What each part does
| Part | Purpose |
|---|---|
WARMUP_MS | Skips the HC-SR501's unstable first minute after power-up |
risingEdge | Fires once when motion starts, not on every 100 ms sample while it continues |
COOLDOWN_MS | Caps notifications at one per minute, protecting your daily event limit |
armed + BLYNK_WRITE(V2) | Turn alerts off from the app while you're home, without unplugging anything |
Blynk.virtualWrite(V0, …) | Live motion LED on the dashboard. Datastream updates don't count as events |
Blynk.logEvent(code, description) | Creates the event; Blynk sends the push notification according to the event's settings |
If Wi-Fi is down when motion happens, the sketch prints to Serial but does not queue the event. That's deliberate: a burst of stale alerts after reconnecting is usually worse than a missed one. To show the Last motion label in real clock time instead of seconds since boot, sync the time with NTP (configTime() on the ESP32).
4. Tune placement and the alert policy
- Mount the PIR 2–2.5 m high, looking across the path people walk; PIRs detect movement across their field of view better than straight towards them.
- Keep it away from heaters, air vents, windows with direct sun and pet height, which are the usual sources of false alarms.
- A 60 s cooldown is a starting point. For a front door, 2–5 minutes avoids repeat alerts while someone is unloading shopping; for a rarely-used storeroom, shorter is fine.
- No notification doesn't mean an empty room: the device might be offline. Blynk shows the device's online/offline status, and you can add an Offline notification in the template's device settings.
5. Test it properly
| Test | Expected result |
|---|---|
| Power up and wait 60 s | No notification during warm-up |
| Walk past once | Exactly one notification; V0 LED blinks on then off |
| Keep moving in front of it | No extra notifications (output stays HIGH, no new edge) |
| Walk past again within 60 s | Serial shows "Suppressed (cooldown)" |
| Walk past after the cooldown | A new notification |
| Switch V2 off and walk past | LED changes, no notification |
| Unplug the router and walk past | Serial shows motion; no alert queued; device shows offline in the app |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Event appears in Timeline but no push notification | Notifications not enabled for the event, no recipient, or phone blocks Blynk notifications | Enable notifications + Device Owner recipient; allow notifications in phone settings |
Nothing at all after logEvent | Event code doesn't match (name used instead, or different case) | Copy the code exactly: motion_detected |
| Worked, then stopped for the day | Daily event limit reached (often from calling logEvent in a loop) | Use the edge + cooldown logic; the limit resets within 24 hours |
| Notifications every few seconds | No edge detection, or trigger jumper on L with long hold time | Use the sketch above; set the jumper to H |
| Random alerts with nobody there | Heat sources, sunlight, unstable power, warm-up period | Re-aim the sensor, lower sensitivity, use a decent 5 V supply |
| Motion LED never lights | Wrong GPIO, PIR not powered from 5 V, or V0 widget on a different datastream | Check wiring and widget datastream |
| Device keeps going offline | delay() added to loop() or weak Wi-Fi | Keep loop() to Blynk.run() and timer.run() |
FAQ
How many notifications can Blynk send? Events are limited per device over a rolling 24-hour window; 100 per day is the figure most often quoted on the Blynk forum. Check your plan in Blynk.Console, and design the sketch so it never comes close.
What's the difference between an event and a notification in Blynk?
An event is a logged occurrence (logEvent). A notification (push, email or SMS, depending on plan) is an optional action attached to that event in its settings.
Can I get an email instead of a push notification? Yes, if your plan includes it: add email as a delivery method in the event's Notifications tab.
Does this work with an ESP8266 / NodeMCU?
Yes. Replace the includes with ESP8266WiFi.h and BlynkSimpleEsp8266.h, and use a safe input pin such as D5 (GPIO14). See NodeMCU ESP8266 with Blynk.
Can I send a photo with the alert? Not with a PIR alone. Pair it with an ESP32-CAM: see the ESP32-CAM Blynk remote security system.
Next steps
- Blynk 2.0 complete guide for ESP32 and ESP8266.
- Blynk automations and schedules: arm the sensor automatically at night.
- ESP32 relay control with Blynk and state sync: switch a light when motion is detected.
- Prefer self-hosted alerts? Node-RED PIR alerts to Telegram and email.
- ESP32 pinout: choose another input pin.
Official reference: Blynk documentation. Motion notifications are a convenience feature, not a certified alarm system.




