hi guys in this video we will see how to use motion sensor as a counter with Arduino. So lets get started. For this you will need

  1. PIR sensor (HC-SR501),
  2. Arduino
  3. Breadboard
  4. Jumper Wire
  5. LED.

Sketch and circuit diagram is almost same with some part of coding added for counter. You can see previous tutorial for PIR sensor interface with LED. Do connection as shown in diagram.

sketch:

int ledPin = 13; // choose the pin for the LED
int inputPin = 3; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
currentState = 1;
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
currentState = 0;
// We only want to print on the output change, not state
pirState = LOW;
}
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
}
Explanation:

All things is same but only change will be done in sketch. You have to add integer counter, currentstate & previous state equal to zero that means its counter is zero.

int counter = 0;
int currentState = 0;
int previousState = 0;

When the sensor value detected high it will give currentstate equal to one.

if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
currentState = 1;
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
}

if current state is not equal to previous state then it will check current state is equal to one or not. If it is one counter will count one. If it is not one it will not count. Current state is given zero here because we want only output when there is motion detection.

if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);

Upload the sketch. Go to tool & serial monitor. When I move my hand over it. It will count how many times my hand pass over it.

Till then keep learning keep making.

Categories: Arduino

2 Comments

How to use buzzer with PIR sensor – Robotica DIY · July 21, 2019 at 2:41 pm

[…] PIR sensor projects you can have a look on my previous turorial for PIR sensor in Arduino & PIR sensor as a counter. Do connection as shown in […]

How to make counter using motion detection sensor (PIR) with LCD – Robotica DIY · July 26, 2019 at 12:01 pm

[…] can watch previous tutorial for PIR sensor as a counter only difference in this tutorial  is we are using LCD so that we will get output on LCD not on […]

Comments are closed.