Counter PIR sensor with led blinking

the below code will show the detection value ether 0 for low and 1 for high...
and also count the detection ..

int led = 12;
int sensor = 8;
int c=1;
void setup() {
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorval = digitalRead(sensor);
  Serial.println(sensorval);
  
  if (sensorval == HIGH) {
    digitalWrite(led, HIGH);
    Serial.println("Motion detected!");
    c++;
    Serial.println(c);
  }
  else {
    digitalWrite(led, LOW);
    Serial.println("No Motion detected!");
    c=1;
  }
  delay(1000);
}

the most problem with beginner is to not getting the info of the sensor state..
active state or passive state.
firstly needed to chick at as the sensor is active or passive because it's impact the whole code and function of the project..
like

int val=DigitalRead(sensor) ;
Serial.print(val);

it will show the state of sensors..
as if at detect it's show 0 or 1.

sp. “digitalRead”

Do you have a question?

Merged what appear to be threads of the same topic.

Asad_Ullah019:
and also count the detection ..

Are you sure? Looks to me more like it will c++ while there's motion.

adafruit's example uses a state variable pirState over and above just the state of the pin, and can therefore log new motion. You code looks like theirs (hers?) with parts missing.

But I can't test it: the dog chewed the Fresnel lens half-a-ping-pong-ball thingy on my only pir sensor.

It counts roughly the seconds that the PIR sensor's output is HIGH.

This is only very loosely related to their being motion (the PIR sensor remains activated for some time after there's no more motion detected), and barely related to the number of individual motion events.

Quite a few of such sensors are active LOW, using an open collector output.

just add a variable true if it’s been counted. Than insert in you code “if true” before you ask “if high”. you need to set the variable too false when the sensor is low.