Ultrasonic sensor visitor and people                  counter with arduino



need of component  

  1. Arduin.
  2. ultrasonic sensor.
  3. breadboard.
  4. Jumper wire.

circuit diagram


CODE :



#include <Wire.h>  // Comes with Arduino IDE
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
const int ledPin = 13;  // the pin that the LED is attached to



long duration, distance; // Duration used to calculate distance
int sensorCounter = 0;   // counter for the number of button presses
int lastsensorDistance = 0;
int setCounter = 20;
int incomingByte;

void setup() {
 Serial.begin (9600);

 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop() {

  


  if (Serial.available() > 0) {            // see if there's incoming serial data:
  incomingByte = Serial.read();            // read the oldest byte in the serial buffer:
  if (incomingByte == 'R') {              // if it's a capital R, reset the counter
      Serial.println("Reset");
      sensorCounter = 20;

    }
  }

 /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 if (distance <= 20 && lastsensorDistance >= 40){
      sensorCounter++;
    Serial.print("number of counts:  ");
    Serial.println(sensorCounter);
   Serial.println(distance);
   }
   
 else {
  
  //Serial.println("off");   not needed.

  }

  lastsensorDistance = distance;
  delay(500); 


   // turns on the LED when counter is at setCounter 
  if (sensorCounter >= setCounter) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }


}


EXPLANATION

In this sketch you need to add counter, current state and previous state. For initial state their values are zero. In this video I am going to cover only counter part of programming rest of sketch I have explained in previous video you can watch it by clicking on I button. When any obstacle comes under distance equal to or less than 10cm. Current state value will be equal to 1. Here I have given 10cm value you can give any value depend upon your application.  Now it will check current state is not equal to previous state then It will go to check current state is equal to one or not. If it is equal to one then counter will count one. If distance is more than 10cm then current state will be zero therefore it will be not counted. Serial.println will print the counter value on screen. Upload the sketch. Go to tools and serial monitor.

When I move box in front of sensor it will count how many times it passed across it.

Comments

  1. can you add more counter,example when object count to 10 the red led will light up.When the object is count 20 the green led will light up.

    ReplyDelete
  2. You are good dear,I really love it I can't even do that one ,u tried

    Please chat me up on Facebook: frizzy precious
    Whatsapp:09013665198
    Email: officialfrizzy60@gmail.com
    Please contact me let talk

    ReplyDelete
  3. JTG Interactive | JTG Interactive Hotel & Casino
    JTG Interactive is the new JTG International 충주 출장마사지 Hotel & 경주 출장안마 Casino in New 김해 출장안마 Cumberland, Maryland. This gaming 광양 출장안마 resort is located in the heart of the 세종특별자치 출장샵 entertainment

    ReplyDelete

Post a Comment

Popular posts from this blog