INPUT_PULLUP will force the pin high so the val == HIGH is going to be true all the time unless the PIR pulls the pin low on detection and if that's the case no detection will keep the buzzer going as the val == HIGH stays true with the current code ; Not sure what type of output PIR you're using.
If the PIR outputs a positive voltage when a detection is made then you would want to pull the input pin to ground with a resistor and the PIR drives it high.
This would then trigger if the PIR detects something and the pin gets pulled low.
If the PIR drives its output HIGH - outputs a voltage on detection then try this and place a 10K ohm + resistor between the PIR output and ground
In this case the input is held LOW by the resistor and then driven HIGH by the PIR output on detection, the resistor is needed to define a LOW state on the pin when the PIR is not outputting a voltage.
Ok, everyone was at some point. So admitting this, it should be obvious that you will have to spend some time learning as much as you need to with every component. Those PIR sensors can be tricky, despite how easy they seem. See the two little potentiometers on the module? You may need to experiment with those to "dial in" the sensor response type and sensitivity you require.
Another tip with those modules: take a discarded toilet paper tube and cut it and maybe tape it to the module to create a little tunnel-shaped "window" surrounding the sensor, so the sensor is sort of "looking down" the tube. Those sensors capture warm bodies very well and very quickly if they are set as such. The TP tube really helps avoid those unwanted sensor events while you experiment around with things.
By looking at your code, it needs no Arduino. The output of the PIR you connect it to pin 2 as an input. This pin (on the PIR) will either transition from HIGH to LOW or LOW to HIGH. Capture that with a multimeter.
Understand that you are triggering it just by being in the area, so you must leave the room for the timeout period (could be seconds, could be minutes).
To verify Pin 2 is working as an input. Connect a jumper to Arduino GND pin. Run this sketch:
byte pinTwo = 2;
void setup() {
pinMode(pinTwo, INPUT_PULLUP); // pull pinTwo HIGH
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalRead(pinTwo);
if (digitalRead(pinTwo) == 0) // transition pinTwo LOW
digitalWrite(LED_BUILTIN, HIGH); // "L" ON when GND is shorted to Pin 2
else
digitalWrite(LED_BUILTIN, LOW); // "L" OFF when GND is not shorted to Pin 2
}
You should see the "L" LED turn ON when you touch the jumper from ground to Pin 2.
If you ran the sketch in Post #11, and grounded Pin 2 during the sketch, and the "L" LED did not light, Pin 2 might have been compromised. Try the sketch on another DIO pin, changing this line to reflect the change.
Without a DMM, showing how to test for LOW and HIGH with small sketches are what is needed, not guessing what the module does and changing a sketch that might or might not be accurate. After learning how to detect, measure outputs, then try analyzing the original sketch.
@unknown-force Here is the next sketch to test... it looks very similar... it is for trying to find what your PIR does when INACTIVE or ACTIVE...
byte PIR = 2; // use a working DIO pin... Pin 2 seems to be compromised.
void setup() {
Serial.begin(115200);
pinMode(PIR, INPUT_PULLUP); // pull PIR input pin HIGH
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalRead(PIR);
if (digitalRead(PIR) == 0) // testing PIR for LOW output when INACTIVE
digitalWrite(LED_BUILTIN, LOW); // inactive PIR turns "L" OFF
else
digitalWrite(LED_BUILTIN, HIGH); // active PIR turns "L" ON for 5 seconds
}
I'm assuming the PIR has "push/pull" output that will be connected to GND when OFF and Vcc (5V?) when ON, then no pulldown required, but you didn't say which PIR you have.