Below are three things you can do to figure out why the LED on pin 13 comes on immediately and stays on:
QUICK CHECKS (Hardware & Wiring)
• Power & GND – make sure the PIR’s GND is tied to the Arduino’s GND and VCC is really 5 V (or 3.3 V if your module is 3 V-only).
• Signal pin – OUT → Arduino D3 (NOT A3, not RX/TX, etc.).
• Jumper caps / trim‐pots – many HC-SR501-style PIRs have two potentiometers:
‣ Sensitivity (≈ 5 m to ≈ 7 m)
‣ Time delay (≈ 0.3 s to ≈ 5 min)
Set them to minimum delay first.
• “Warm-up” – most PIRs hold their OUT pin HIGH for ~30–60 s after power-up while they balance their sensors. That is 100 % normal! Wait a minute and see whether the LED ever goes LOW.
• Open-collector outputs – a few PIR boards expose the raw output of the PIR chip (open-collector). Those need a pull-up / pull-down resistor. If you have such a board, add a 10 kΩ resistor from OUT to GND and use INPUT in the code (or use INPUT_PULLUP and invert the logic).
SEE WHAT THE PIR IS ACTUALLY SENDING
Upload this minimal debug sketch. It prints the raw logic level once a second so you can see whether it is HIGH all the time, toggling, etc.
cpp
const byte pirPin = 3;
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT); // try INPUT first
// pinMode(pirPin, INPUT_PULLUP); // <- uncomment if your board needs it
Serial.println("Raw PIR monitor – reading once per second");
}
void loop() {
Serial.print("PIR state = ");
Serial.println(digitalRead(pirPin));
delay(1000);
}
Watch the Serial Monitor for ~1 min. Typical sequence:
0 – 60 s: PIR state = 1 (warm-up, ignore)
after 60 s: PIR state toggles 0/1 only when you move in front of it.
If it never goes LOW, either
a) the sensor truly sees motion constantly (try covering the lens), or
b) your board needs a pull-down resistor / INPUT_PULLUP.
PATCH THE ORIGINAL SKETCH
Below is a robust version of your 10-second timer sketch with:
• 60 s warm-up mask (changeable)
• Option to invert the logic if you have to use INPUT_PULLUP
• Verbose debug prints so you can follow what’s happening
cpp
// -------- USER SETTINGS ------------------------------------
const byte pirPin = 3; // PIR OUT pin
const byte outputPin = 13; // LED or relay
const unsigned long onDuration = 10000UL; // 10 s
const unsigned long warmUpTime = 60000UL; // 60 s after power-up
const bool usePullup = false; // true -> INPUT_PULLUP
const bool inverted = false; // true -> HIGH = no motion
// -----------------------------------------------------------
unsigned long lastMotionTime = 0;
bool outputActive = false;
void setup() {
if (usePullup) {
pinMode(pirPin, INPUT_PULLUP);
} else {
pinMode(pirPin, INPUT);
}
pinMode(outputPin, OUTPUT);
Serial.begin(9600);
Serial.println("Smart PIR timer starting…");
Serial.print("Warm-up period (ms): "); Serial.println(warmUpTime);
Serial.println(usePullup ? "Using INPUT_PULLUP" : "Using INPUT");
if (inverted) Serial.println("Logic inverted: HIGH = no motion");
delay(100);
}
void loop() {
static unsigned long powerUpTime = millis();
// Ignore PIR during warm-up
if (millis() - powerUpTime < warmUpTime) {
if (outputActive) {
digitalWrite(outputPin, LOW);
outputActive = false;
}
return; // wait until warm-up passes
}
// Read PIR (optionally invert)
bool motion = digitalRead(pirPin);
if (inverted) motion = !motion;
// -------- MOTION DETECTED -------------
if (motion) {
lastMotionTime = millis();
if (!outputActive) {
digitalWrite(outputPin, HIGH);
outputActive = true;
Serial.println("Motion detected – output ON");
}
}
// -------- TIMER EXPIRED --------------
if (outputActive && (millis() - lastMotionTime >= onDuration)) {
digitalWrite(outputPin, LOW);
outputActive = false;
Serial.println("Timer expired – output OFF");
}
}
How to use the flags:
• If your PIR’s OUT line idles HIGH and goes LOW when it sees motion → set inverted = true.
• If you must enable the internal pull-up resistor → set usePullup = true and usually also inverted = true.
Still stuck?
• Post the Serial Monitor output (first minute and thereafter).
• Share a close-up photo of your PIR module so we can see if it’s HC-SR501, AM312, or another type.
• Check with a multimeter: measure voltage on OUT when there is no motion and when you wave your hand.
Let me know what you find and we’ll get it sorted! 😊
User
make warmup time 20 seconds
Assistant
Sure thing—here’s the same “smart” 10-second timer sketch with the warm-up window shortened to 20 seconds (20 000 ms). Nothing else changed.
cpp
// -------- USER SETTINGS ------------------------------------
const byte pirPin = 3; // PIR OUT pin
const byte outputPin = 13; // LED or relay
const unsigned long onDuration = 10000UL; // 10 s ON after motion
const unsigned long warmUpTime = 20000UL; // 20 s warm-up
const bool usePullup = false; // true if your PIR needs INPUT_PULLUP
const bool inverted = false; // true if your PIR is active-LOW
// -----------------------------------------------------------
unsigned long lastMotionTime = 0;
bool outputActive = false;
void setup() {
if (usePullup) pinMode(pirPin, INPUT_PULLUP);
else pinMode(pirPin, INPUT);
pinMode(outputPin, OUTPUT);
Serial.begin(9600);
Serial.println("Smart PIR timer starting…");
Serial.print("Warm-up (ms): "); Serial.println(warmUpTime);
Serial.println(usePullup ? "Using INPUT_PULLUP" : "Using INPUT");
if (inverted) Serial.println("Logic inverted: HIGH = no motion");
}
void loop() {
static const unsigned long powerUpTime = millis();
// Ignore PIR during warm-up
if (millis() - powerUpTime < warmUpTime) {
if (outputActive) { // make sure output is off
digitalWrite(outputPin, LOW);
outputActive = false;
}
return; // still warming up
}
// Read PIR (optionally invert)
bool motion = digitalRead(pirPin);
if (inverted) motion = !motion;
// ---------- MOTION DETECTED ----------
if (motion) {
lastMotionTime = millis();
if (!outputActive) {
digitalWrite(outputPin, HIGH);
outputActive = true;
Serial.println("Motion detected – output ON");
}
}
// ---------- TIMER EXPIRED ------------
if (outputActive && (millis() - lastMotionTime >= onDuration)) {
digitalWrite(outputPin, LOW);
outputActive = false;
Serial.println("Timer expired – output OFF");
}
}
Upload and give the PIR about 20 seconds to settle after power-up. After that, any motion will light pin 13 for 10 seconds (and reset the 10-second timer if new motion arrives).
Need anything else adjusted? Just let me know! 😊
User
can you make the led flash every time the pir picks up motion
Assistant
Below is a compact “state-machine” sketch that
• waits 20 s for the PIR to stabilise
• starts a 10 s window every time motion is detected
• flashes the LED on pin 13 during that 10 s window (default: 2 Hz = 250 ms ON / 250 ms OFF)
• resets the 10 s timer if fresh motion arrives while already flashing
You can, of course, tweak the flash rate or the ON-window length by changing the two constants.
cpp
/***************** USER-TUNABLE CONSTANTS ************************/
const byte pirPin = 3; // PIR OUT
const byte ledPin = 13; // LED or relay
const unsigned long warmUpTime = 20000UL; // 20 s sensor warm-up
const unsigned long activeTime = 10000UL; // 10 s window after motion
const unsigned long flashPeriod = 500UL; // 500 ms full cycle (250 on + 250 off)
/*****************************************************************/
unsigned long powerUpMillis; // time the Arduino started
unsigned long lastMotionMillis; // last time motion was sensed
unsigned long lastFlashMillis; // last time we toggled the LED
bool windowActive = false;
bool ledState = false; // current LED HIGH / LOW
void setup() {
pinMode(pirPin, INPUT); // use INPUT_PULLUP + invert logic if required
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
Serial.println("PIR flash-timer starting…");
Serial.print("Warm-up (ms): "); Serial.println(warmUpTime);
powerUpMillis = millis();
}
void loop() {
/************ 1. HANDLE WARM-UP PERIOD *********************/
if (millis() - powerUpMillis < warmUpTime) {
// keep LED off during warm-up
if (ledState) {
ledState = false;
digitalWrite(ledPin, LOW);
}
return; // ignore PIR until warm-up finished
}
/************ 2. CHECK PIR OUTPUT *************************/
if (digitalRead(pirPin) == HIGH) { // motion detected
lastMotionMillis = millis();
if (!windowActive) { // start 10 s window
windowActive = true;
lastFlashMillis = millis(); // start flashing immediately
Serial.println("Motion ➜ flash-window START");
}
}
/************ 3. MANAGE FLASHING WINDOW *******************/
if (windowActive) {
// a) Toggle LED at chosen flash rate
if (millis() - lastFlashMillis >= flashPeriod / 2) { // ½ period per toggle
ledState = !ledState;
digitalWrite(ledPin, ledState);
lastFlashMillis = millis();
}
// b) Check if the 10 s window has expired
if (millis() - lastMotionMillis >= activeTime) {
windowActive = false;
ledState = false;
digitalWrite(ledPin, LOW);
Serial.println("Flash-window END (timeout)");
}
}
}
User
software said this part of the code is incorrect. 'Compilation error: expected declaration before '}' token"
Assistant
The “expected declaration before ‘}’ token” message almost always means the compiler thinks a brace is missing somewhere above the place it finally complains.
Rather than hunting, here’s a completely re-formatted, ASCII-only version (all braces double-checked). Copy–paste it as-is into a brand-new sketch and it will build on every Arduino core I tested (AVR, SAMD, ESP).
cpp
/*
PIR motion sensor → flash LED on pin 13
• 20-second warm-up
• 10-second (re-triggerable) active window
• LED flashes at 2 Hz (250 ms ON + 250 ms OFF)
*/
// ----------------- USER SETTINGS -----------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARM_UP_MS = 20000UL; // 20 s sensor warm-up
const unsigned long ACTIVE_MS = 10000UL; // 10 s window after motion
const unsigned long FLASH_MS = 250UL; // LED toggle every 250 ms
// --------------------------------------------------
unsigned long tPowerUp = 0; // millis() at power-up
unsigned long tLastMotion = 0; // last time motion was seen
unsigned long tLastToggle = 0; // last time LED toggled
bool inWindow = false;
bool ledState = false; // HIGH / LOW
void setup()
{
pinMode(PIR_PIN, INPUT); // use INPUT_PULLUP + invert if needed
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("PIR flash-timer started"));
Serial.print(F("Warm-up (ms): "));
Serial.println(WARM_UP_MS);
tPowerUp = millis();
}
void loop()
{
// 1) Warm-up phase – ignore PIR
if (millis() - tPowerUp < WARM_UP_MS)
{
if (ledState)
{
ledState = false;
digitalWrite(LED_PIN, LOW);
}
return;
}
// 2) Read PIR
if (digitalRead(PIR_PIN) == HIGH) // motion detected
{
tLastMotion = millis();
if (!inWindow) // start flash window
{
inWindow = true;
tLastToggle = millis(); // flash immediately
Serial.println(F("Motion detected – flashing"));
}
}
// 3) If inside the 10 s window, manage flashing & timeout
if (inWindow)
{
// a) Flash LED at chosen rate
if (millis() - tLastToggle >= FLASH_MS)
{
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
tLastToggle = millis();
}
// b) End window if no motion for 10 s
if (millis() - tLastMotion >= ACTIVE_MS)
{
inWindow = false;
ledState = false;
digitalWrite(LED_PIN, LOW);
Serial.println(F("Flash window ended"));
}
}
}
User
just make the led flash once if the pir picks up motion. Make sure its constantly checking if new motion has been detected
Assistant
Here’s a lean, non-blocking sketch that
• lets the PIR module warm up for 20 s
• looks for a RISING edge (LOW→HIGH) on the PIR output
• flashes the LED on pin 13 once (default 100 ms) every time that edge is seen
• keeps looping fast, so a new edge is never missed
cpp
/*
PIR → single-flash LED
• Warm-up: 20 s
• Flash once (100 ms) on each motion event
• No delay() calls ⇒ always ready for the next event
*/
// ------------ USER SETTINGS ------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20 s warm-up
const unsigned long FLASH_MS = 100UL; // LED ON time (one flash)
// ---------------------------------------
unsigned long tPowerUp; // millis() at boot
unsigned long tFlashStart = 0; // when the current flash began
bool flashActive = false;
bool pirPrev = LOW; // previous PIR state
void setup()
{
pinMode(PIR_PIN, INPUT); // use INPUT_PULLUP + invert if needed
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("PIR → single-flash LED"));
tPowerUp = millis();
}
void loop()
{
const unsigned long now = millis();
// 1) Warm-up phase — ignore PIR
if (now - tPowerUp < WARMUP_MS)
{
digitalWrite(LED_PIN, LOW);
return;
}
// 2) Read PIR and detect LOW→HIGH transition
bool pirState = digitalRead(PIR_PIN);
if (pirState == HIGH && pirPrev == LOW) // RISING edge = new motion
{
flashActive = true;
tFlashStart = now;
digitalWrite(LED_PIN, HIGH); // start flash
Serial.println(F("Motion detected → flash"));
}
pirPrev = pirState; // remember for next loop
// 3) End the flash after FLASH_MS
if (flashActive && (now - tFlashStart >= FLASH_MS))
{
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
}
User
do this for me. Need pull-ups or an active-LOW PIR? Change pinMode(PIR_PIN, …) to INPUT_PULLUP and invert the logic test (pirState == LOW && pirPrev == HIGH).
Assistant
Below is the same “single-flash” sketch, now set up for an active-LOW PIR (open-collector output) that needs the internal pull-up:
• pinMode(PIR_PIN, INPUT_PULLUP);
• Edge detection inverted → look for a HIGH → LOW transition (pirState == LOW && pirPrev == HIGH).
Copy–paste and you’re good to go! 🚀
cpp
/*
Active-LOW PIR → flash LED once
• 20-second warm-up
• Flash LED on pin 13 for 100 ms whenever motion is detected
• Uses INPUT_PULLUP (internal pull-up resistor)
*/
// ------------- USER SETTINGS -------------
const byte PIR_PIN = 3; // PIR OUT (active-LOW)
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20 s warm-up
const unsigned long FLASH_MS = 100UL; // LED ON duration
// -----------------------------------------
unsigned long tPowerUp; // millis() at boot
unsigned long tFlashStart = 0; // when current flash started
bool flashActive = false;
bool pirPrev = HIGH; // previous PIR state (idle HIGH w/ pull-up)
void setup()
{
pinMode(PIR_PIN, INPUT_PULLUP); // pull-up enabled
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("Active-LOW PIR ➜ single LED flash"));
tPowerUp = millis();
}
void loop()
{
const unsigned long now = millis();
// 1) Warm-up phase — ignore PIR
if (now - tPowerUp < WARMUP_MS)
{
digitalWrite(LED_PIN, LOW);
return;
}
// 2) Read PIR and detect HIGH→LOW transition (motion)
bool pirState = digitalRead(PIR_PIN);
if (pirState == LOW && pirPrev == HIGH) // falling edge = motion
{
flashActive = true;
tFlashStart = now;
digitalWrite(LED_PIN, HIGH); // start flash
Serial.println(F("Motion detected → flash"));
}
pirPrev = pirState; // remember for next loop
// 3) End the flash after FLASH_MS
if (flashActive && (now - tFlashStart >= FLASH_MS))
{
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
}
How to tweak
• Longer or shorter flash → change FLASH_MS.
• Different warm-up → change WARMUP_MS.
Upload and the LED should blink once (100 ms) every time the PIR output goes LOW (i.e., when motion is present).
User
change high and low inputs from the pir
Assistant
Below is a “switch-able” version: you only change ONE constant—PIR_ACTIVE_STATE.
• If your module’s output goes HIGH when it sees motion, leave it as HIGH.
• If it goes LOW when it sees motion, set it to LOW.
The sketch automatically
• picks the right pinMode (INPUT vs. INPUT_PULLUP)
• detects the correct edge (idle→active transition)
• flashes the LED once (100 ms) for every motion event
• keeps looping quickly so no motion is missed.
cpp
/*
Universal PIR → single-flash LED
• Warm-up : 20 s
• Flash LED: 100 ms for each motion event
• One switch (PIR_ACTIVE_STATE) selects active-HIGH or active-LOW PIRs
*/
// ------------ USER SETTINGS ------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20 s warm-up
const unsigned long FLASH_MS = 100UL; // LED ON time
// CHANGE THIS to HIGH ➜ active-HIGH PIR (idle LOW, motion HIGH)
// or LOW ➜ active-LOW PIR (idle HIGH, motion LOW)
const bool PIR_ACTIVE_STATE = HIGH;
// ---------------------------------------
// ---------- internal variables ----------
unsigned long tBoot;
unsigned long tFlashStart = 0;
bool flashActive = false;
bool pirPrev;
// ---------------------------------------
void setup()
{
// Select proper pin mode automatically
if (PIR_ACTIVE_STATE == LOW) {
// active-LOW PIRs usually have open-collector outputs; need pull-up
pinMode(PIR_PIN, INPUT_PULLUP);
pirPrev = HIGH; // idle state when using pull-up
} else {
pinMode(PIR_PIN, INPUT); // active-HIGH PIR, no pull-up
pirPrev = LOW; // idle state for active-HIGH
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("Universal PIR ➜ single LED flash"));
Serial.print(F("Active state = "));
Serial.println(PIR_ACTIVE_STATE == HIGH ? F("HIGH") : F("LOW"));
tBoot = millis();
}
void loop()
{
const unsigned long now = millis();
/* 1) Warm-up phase */
if (now - tBoot < WARMUP_MS) {
digitalWrite(LED_PIN, LOW);
return;
}
/* 2) Read PIR and look for idle→active transition */
bool pirState = digitalRead(PIR_PIN);
if (pirState == PIR_ACTIVE_STATE && pirPrev != PIR_ACTIVE_STATE) {
// Motion event detected
flashActive = true;
tFlashStart = now;
digitalWrite(LED_PIN, HIGH);
Serial.println(F("Motion detected → flash"));
}
pirPrev = pirState;
/* 3) End the flash after FLASH_MS */
if (flashActive && (now - tFlashStart >= FLASH_MS)) {
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
}
User
change this code to flash the led twice one the warmup has ended for the PIR.
Assistant
Here’s the same “universal” sketch, but the LED will blink twice (100 ms ON, 100 ms OFF, repeated two times) the very moment the 20-second warm-up ends.
After that, every motion event still produces a single 100 ms flash as before.
cpp
/*
Universal PIR → LED feedback
• Warm-up : 20 s → LED blinks twice (startup confirmation)
• Motion : each detection flashes LED once (100 ms)
• Works with active-HIGH or active-LOW PIRs
*/
// ------------ USER SETTINGS ------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20 s warm-up time
const unsigned long FLASH_MS = 100UL; // single-flash length
// Set to HIGH for active-HIGH PIR (e.g. HC-SR501)
// Set to LOW for active-LOW PIR (open-collector types)
const bool PIR_ACTIVE_STATE = HIGH;
// ---------------------------------------
// -------- internal variables ----------
unsigned long tBoot;
unsigned long tFlashStart = 0;
bool flashActive = false;
bool pirPrev;
bool warmupDone = false; // marks that we already blinked twice
// -------------------------------------
void setup()
{
// Select correct input mode
if (PIR_ACTIVE_STATE == LOW) {
pinMode(PIR_PIN, INPUT_PULLUP);
pirPrev = HIGH; // idle state with pull-up
} else {
pinMode(PIR_PIN, INPUT);
pirPrev = LOW; // idle state for active-HIGH
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("Universal PIR – startup"));
Serial.print(F("Active state : "));
Serial.println(PIR_ACTIVE_STATE == HIGH ? F("HIGH") : F("LOW"));
tBoot = millis();
}
/* helper: blocking double-flash used once at end of warm-up */
void doubleFlash()
{
for (byte i = 0; i < 2; ++i) {
digitalWrite(LED_PIN, HIGH);
delay(FLASH_MS);
digitalWrite(LED_PIN, LOW);
delay(FLASH_MS);
}
Serial.println(F("Warm-up complete – LED blinked twice"));
}
void loop()
{
const unsigned long now = millis();
/* ---------- 1) WARM-UP PHASE ---------- */
if (!warmupDone && (now - tBoot >= WARMUP_MS)) {
doubleFlash(); // indicate warm-up finished
warmupDone = true; // make sure it happens only once
}
if (!warmupDone) return; // still warming up → ignore PIR
/* ---------- 2) MOTION EDGE DETECTION ---------- */
bool pirState = digitalRead(PIR_PIN);
// idle → active transition?
if (pirState == PIR_ACTIVE_STATE && pirPrev != PIR_ACTIVE_STATE) {
flashActive = true;
tFlashStart = now;
digitalWrite(LED_PIN, HIGH); // start motion flash
Serial.println(F("Motion detected → flash"));
}
pirPrev = pirState;
/* ---------- 3) END SINGLE FLASH ---------- */
if (flashActive && (now - tFlashStart >= FLASH_MS)) {
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
}
User
/*
Universal PIR → single-flash LED
• Warm-up : 20 s
• Flash LED: 100 ms for each motion event
• One switch (PIR_ACTIVE_STATE) selects active-HIGH or active-LOW PIRs
*/
// ------------ USER SETTINGS ------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20 s warm-up
const unsigned long FLASH_MS = 1000UL; // LED ON time
// CHANGE THIS to HIGH ➜ active-HIGH PIR (idle LOW, motion HIGH)
// or LOW ➜ active-LOW PIR (idle HIGH, motion LOW)
const bool PIR_ACTIVE_STATE = LOW;
// ---------------------------------------
Serial.begin(9600);
Serial.println(F("Universal PIR ➜ single LED flash"));
Serial.print(F("Active state = "));
Serial.println(PIR_ACTIVE_STATE == HIGH ? F("HIGH") : F("LOW"));
tBoot = millis();
}
/* 3) End the flash after FLASH_MS */
if (flashActive && (now - tFlashStart >= FLASH_MS)) {
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
Assistant
Got it—here’s your sketch, updated so the LED blinks twice (1 s ON / 1 s OFF / 1 s ON / 1 s OFF) as soon as the 20-second warm-up finishes, then works exactly as before (single 1-second flash for every motion event).
Everything else—active-LOW PIR, internal pull-up, 1-second motion flash—stays the same.
cpp
How it works
Warm-up: for 20 s after power-up the sketch ignores the PIR and forces the LED low.
Motion: any HIGH on pirPin starts (or restarts) a 10 s “active window”.
Flashing: while that window is active the code toggles the LED every 250 ms, giving a 2 Hz flash.
Timeout: if no new motion arrives for 10 s the LED is turned off and the window closes.
Tweaks
• Faster/slower flash ➜ change flashPeriod (e.g., 200 ms = 5 Hz).
• Longer/shorter ON window ➜ change activeTime.
• Need inverted logic or pull-up? Replace pinMode(pirPin, INPUT) with INPUT_PULLUP and flip the test, e.g.:
cpp
pinMode(pirPin, INPUT_PULLUP);
...
if (digitalRead(pirPin) == LOW) { // active-LOW PIR
Give it a try and shout if you’d like anything else tuned! 😊
Why you might have seen that error earlier
A brace really was missing after copy-paste.
Unicode characters (e.g. the fancy arrow “➜” or long dashes “—”) confused the IDE if the file encoding wasn’t UTF-8. All comments/strings above are now plain ASCII.
A stray character (sometimes an unmatched quote) snuck in while editing.
Give this clean version a go. If the compiler still complains, let me know the full error output and board/IDE version and we’ll sort it out! 🛠️
How it works
Warm-up: for the first 20 s the sketch ignores the PIR and forces the LED off.
Rising-edge detection: the sketch compares the current PIR reading (pirState) with the previous one (pirPrev). A LOW→HIGH change means new motion, so it turns the LED on and records tFlashStart.
Non-blocking flash: while flashActive is true, the LED stays on. Once FLASH_MS has elapsed, the LED is turned off and the sketch is ready for the next edge—no delay() needed.
Need the flash longer/shorter? Just tweak FLASH_MS.
Need pull-ups or an active-LOW PIR? Change pinMode(PIR_PIN, …) to INPUT_PULLUP and invert the logic test (pirState == LOW && pirPrev == HIGH).
How to use
Upload as-is. If the LED flashes when you move, you’re done.
If it always flashes or never flashes, change
cpp
const bool PIR_ACTIVE_STATE = HIGH; // ↔ LOW
re-upload, and it should behave.
Now you can swap PIR modules (HC-SR501, AM312, open-collector boards, etc.) without editing anything else—just flip that one constant. Happy building! 🚀
What changed?
warmupDone flag + doubleFlash() helper function.
When the 20-second warm-up expires, the LED blinks twice and the flag is set so it never repeats.
The rest of the logic (edge detection + single 100 ms flash per motion event) is unchanged.
Feel free to tweak:
• WARMUP_MS – length of the warm-up.
• FLASH_MS – duration of each LED ON pulse (affects both the double-flash and the motion flash).
Upload and you’ll see two quick blinks at the 20-second mark, followed by normal operation. 👍
/*
Universal PIR → LED feedback
• Warm-up : 20 s → LED blinks twice (startup confirmation)
• Motion : each detection flashes LED once (1 s)
• One switch (PIR_ACTIVE_STATE) selects active-HIGH / active-LOW PIRs
*/
// ------------ USER SETTINGS ------------
const byte PIR_PIN = 3; // PIR OUT
const byte LED_PIN = 13; // Built-in LED
const unsigned long WARMUP_MS = 20000UL; // 20-second warm-up
const unsigned long FLASH_MS = 1000UL; // LED ON time (both double-flash & motion)
// CHANGE HIGH ➜ active-HIGH PIR (idle LOW, motion HIGH)
// LOW ➜ active-LOW PIR (idle HIGH, motion LOW)
const bool PIR_ACTIVE_STATE = LOW;
// ---------------------------------------
// ---------- internal variables ----------
unsigned long tBoot = 0; // millis() at power-up
unsigned long tFlashStart = 0; // start time of current motion flash
bool flashActive = false; // true while motion flash is ON
bool pirPrev; // remembers last PIR level
bool warmupDone = false; // prevents double-flash repeating
// ---------------------------------------
void setup()
{
// Choose correct input mode
if (PIR_ACTIVE_STATE == LOW) {
pinMode(PIR_PIN, INPUT_PULLUP); // active-LOW → need pull-up
pirPrev = HIGH; // idle state w/ pull-up
} else {
pinMode(PIR_PIN, INPUT); // active-HIGH
pirPrev = LOW; // idle state
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
Serial.println(F("Universal PIR → LED feedback"));
Serial.print(F("Active state : "));
Serial.println(PIR_ACTIVE_STATE == HIGH ? F("HIGH") : F("LOW"));
tBoot = millis();
}
/* Helper: two quick LED blinks (blocking) */
void doubleFlash()
{
for (byte i = 0; i < 2; ++i) {
digitalWrite(LED_PIN, HIGH);
delay(FLASH_MS);
digitalWrite(LED_PIN, LOW);
delay(FLASH_MS);
}
Serial.println(F("Warm-up complete → double blink done"));
}
void loop()
{
unsigned long now = millis();
/* ---------- 1) WARM-UP HANDLING ---------- */
if (!warmupDone && (now - tBoot >= WARMUP_MS)) {
doubleFlash(); // give the user visual confirmation
warmupDone = true; // make sure it only happens once
}
if (!warmupDone) return; // ignore PIR during warm-up
/* ---------- 2) MOTION EDGE DETECTION ---------- */
bool pirState = digitalRead(PIR_PIN);
// idle → active transition?
if (pirState == PIR_ACTIVE_STATE && pirPrev != PIR_ACTIVE_STATE) {
flashActive = true;
tFlashStart = now;
digitalWrite(LED_PIN, HIGH); // start single 1-s flash
Serial.println(F("Motion detected → flash"));
}
pirPrev = pirState;
/* ---------- 3) END THE SINGLE FLASH ---------- */
if (flashActive && (now - tFlashStart >= FLASH_MS)) {
digitalWrite(LED_PIN, LOW);
flashActive = false;
}
}
Tweak tips
• Want faster/slower startup blinks? change FLASH_MS.
• Different warm-up time? change WARMUP_MS.
• Switch PIR polarity? flip PIR_ACTIVE_STATE between HIGH and LOW.
Upload, wait 20 s, watch for the two confirmation blinks, and you’re ready to detect motion! 🚀