calculating speed with two HCSR04 ultrasonic sensors on arduino

61 views Asked by At

We have two sensors. We want to calculate the speed of an obj that passes infront of the sensors like a car. Firstly the obj passes infront of sensor1 and after infront of sensor2.Between the two sensors we have a 5 cm distance. We also have two led lights that we want to light up when the obj passes infront of the sensors. For example when an obj passes infront of the first sensor we want led1 to light up and when it passes infront of the second sensor we want led2 to light up. But we also need for the sensors to calculate the speed because we want them to recognize if the obj that passes through is a car or a human. If the obj has speed bigger than 60km/h the sensors sense that it is a car and light up the led lights if not it is a human and the led lights do not light up

the code I used does not work at all

2

There are 2 answers

0
Daniel Bajo Collados On

I don't know what you did. I would do something like this.

//Global Variables
unsigned long initialTime=0, endTime=0;                                 //Time of entrance and exit of the object
bool read1=true, read2=true;                                            //Flags to read or not the ultrasonic snesors
int Ultrasonic1Trig, Ultrasonic1Trig, Ultrasonic2Trig, Ultrasonic2Echo; //Pins the ultrasonic sensors are using
float carLimit, maxLimit;

//User Functions
bool presenceDetected(int trig, int echo){
  float dist;
  //Measure distance
  //
  //
  
  if (dist>maxLimit)
    return false;//No object detected
  else
    return true;//Object detected
}

void setup() {
  //Setup of all devices

}

void loop() {
  if (presenceDetected(Ultrasonic1Trig, Ultrasonic1Trig)){
    digitalWrite(led1, true)//Turn on led
    if (read1){
      initialTime=millis();//record time of entrance
      read1=false;//Stop reading
    }
  }
  else
    digitalWrite(led1, false);//Turn off led
    
    
  if (presenceDetected(Ultrasonic2Trig, Ultrasonic2Trig)){
    digitalWrite(led2, true);//Turn on led
    if (read2){
      endTime=millis();//record time of exit
      read2=false;//Stop reading
    }
  }
  else
    digitalWrite(led2, false);//Turn off led
    
    
  if (!read1 && !read2){//Both measurements have been recorded
    unsigned long timeObj = endTime-initialTime;//Time from entrance to exit
    float speed = 500/timeObj;//Speed in m/s
    Serial.print("Speed of object is: ");
    Serial.print(speed);
    Serial.println(" m/s");
    if (speed > carLimit)
      Serial.println("It's a car!");
    else 
      Serial.println("It's a person!");
    read1=true;
    read2=true;
  }
}

This is just a template with some basic ideas.

0
Clifford On

You have given us nothing to work with, but assuming a hypothetical function objectPresent( sensorid ) returning 0 when no object present and 1 when present and where sensorid is one of the two proximity detectors, then the speed can be determined by the time between 0 to 1 transitions on each of the sensors. Given the speed, the size of the object can estimated by its time to pass one of the sensors.

          ┌───────────────────────────────┐
          │                               │
SENSOR A  │                               │
──────────┘                               └────────────────────────
          |                               │
          │                     ┌─────────┼─────────────────────┐
SENSOR B  │                     │         │                     │
          │                     │         │                     |
──────────┼─────────────────────┘         │                     └──
          │                     |         │
          │                     │         │
          ◄─────────────────────►         │
          │                     │         │
          │Time determines speed          │
          │                               │
          ◄───────────────────────────────►
          │ Time at speed determines size │

So given a function to capture the required timing from the sensor pair, such as:

bool sensor_time( unsigned long& speed_time,
                  unsigned long& size_time )
{
  static int stateA = objectPresent( sensorA ) ;
  static int stateB = objectPresent( sensorB ) ;
  static unsigned long sense_a_time_us = 0ul ;
  static unsigned long sense_b_time_us = 0ul ;
  static unsigned long pulse_time_us = 0ul ;
  bool new_detection = false ;

  unsigned long now_us = micros() ;

  // Test sensor A state...
  int new_state = objectPresent( sensorA ) ;
  if( stateA != new_state )
  {
    if( new_state == 1 )
    {
      // 0 to 1 transition
      sense_a_time_us = now_us ;
    }
    else if( stateB == 1) // Reject narrow objects
    {
      // On 1 to 0 transition, latch new timing
      pulse_time_us = now_us - sense_a_time_us ;
      size_time = pulse_time_us ;
      speed_time = sense_b_time_us - sense_a_time_us ;      
      
      new_detection = true ;
    }
    
    stateA = new_state ;
    digitalWrite( LEDA, stateA ) ;
  }

  // Test sensor B sensor state...
  new_state = objectPresent( sensorB ) ;
  if( stateB != new_state )
  {
    if( new_state == 1 )
    {
      sense_b_time_us = now_us ;
    }
    
    stateB = new_state ;
    digitalWrite( LEDB, stateB ) ;
  }
  
  return new_detection ; 
}

Then that function can be used in the executive loop thus:

void loop() 
{
  static unsigned people_count = 0 ;

  // Get sensor timing/count
  unsigned long speed_time = 0 ;
  unsigned_long size_time = 0 ;
  
  // If new object detected...
  if( sensor_time( speed_time, size_time ) )
  {
    static const unsigned SENSOR_SEPARATION_CM = 5u ;
    unsigned long transit_speed_cm_per_sec = (SENSOR_SEPARATION_CM * 10000ul) / speed_time ;
    unsigned long object_size_cm = (transit_speed_cm_per_sec * size_time) / 1000000ul ;

    static const MAX_HUMAN_SPEED_CM_SEC = 1000ul ;
    static const MAX_HUMAN_WIDTH_CM = 100ul ;
    bool human = transit_speed_cm_per_sec <= MAX_HUMAN_SPEED_CM_SEC && object_size_cm <= MAX_HUMAN_WIDTH_CM ;

    if( human )
    {
      people_count++ ;

      ...
    }
    
  }
}