Skip to content

Tutorial

Blynk PIR Motion Notifications on ESP32 (logEvent + Cooldown)

Get a Blynk phone notification when an HC-SR501 PIR sees motion: event and notification setup, ESP32 sketch with rising edge, cooldown and arm switch, and fixes.

  • 4 min read
  • Updated November 2, 2026
Blynk PIR Motion Notifications on ESP32 (logEvent + Cooldown)

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 needNotes
ESP32 dev boardESP8266 works with BlynkSimpleEsp8266.h
HC-SR501 PIR module5 V supply, 3.3 V logic output, safe for ESP32 GPIO
Blynk account + Blynk IoT phone appNotifications arrive through the app
Jumper wires, micro-USB cable
Rising-edge detection plus a 60-second cooldown: four motion events become two notifications

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 meansIn practice
It detects changes in heat, not presenceSomeone sitting perfectly still stops triggering it
Movement across zones works bestWalking past is detected further away than walking straight at it
Any moving heat source countsPets, heaters cycling, sun patches and warm air from vents can trigger it
Output is a simple digital HIGH/LOWNo 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 pinESP32 pin
VCC5V / VIN
OUTGPIO27
GNDGND

Set up the module before you write any code:

ControlSet toWhy
Sensitivity potMiddle, then adjustRange is roughly 3–7 m
Time-delay potFully anticlockwise (shortest, a few seconds)Let the sketch's cooldown handle repeats
Trigger jumperH (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

  1. 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.
  2. 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.
  3. Turn on Send event to Timeline so you can see every event in the app, even ones that didn't notify.
  4. Datastreams: add V0 Motion (integer 0–1), V1 Last motion (string) and V2 Armed (integer 0–1, default 1).
  5. Dashboard: an LED widget on V0, a Label on V1 and a Switch on V2. Add the same widgets in the phone app.
  6. 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.

sketch.ino
#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

PartPurpose
WARMUP_MSSkips the HC-SR501's unstable first minute after power-up
risingEdgeFires once when motion starts, not on every 100 ms sample while it continues
COOLDOWN_MSCaps 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

TestExpected result
Power up and wait 60 sNo notification during warm-up
Walk past onceExactly one notification; V0 LED blinks on then off
Keep moving in front of itNo extra notifications (output stays HIGH, no new edge)
Walk past again within 60 sSerial shows "Suppressed (cooldown)"
Walk past after the cooldownA new notification
Switch V2 off and walk pastLED changes, no notification
Unplug the router and walk pastSerial shows motion; no alert queued; device shows offline in the app

Troubleshooting

SymptomLikely causeFix
Event appears in Timeline but no push notificationNotifications not enabled for the event, no recipient, or phone blocks Blynk notificationsEnable notifications + Device Owner recipient; allow notifications in phone settings
Nothing at all after logEventEvent code doesn't match (name used instead, or different case)Copy the code exactly: motion_detected
Worked, then stopped for the dayDaily event limit reached (often from calling logEvent in a loop)Use the edge + cooldown logic; the limit resets within 24 hours
Notifications every few secondsNo edge detection, or trigger jumper on L with long hold timeUse the sketch above; set the jumper to H
Random alerts with nobody thereHeat sources, sunlight, unstable power, warm-up periodRe-aim the sensor, lower sensitivity, use a decent 5 V supply
Motion LED never lightsWrong GPIO, PIR not powered from 5 V, or V0 widget on a different datastreamCheck wiring and widget datastream
Device keeps going offlinedelay() added to loop() or weak Wi-FiKeep 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

Official reference: Blynk documentation. Motion notifications are a convenience feature, not a certified alarm system.

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.