3

I'm new to Arduino and in need of help for this small project.

I'm trying to build an Arduino code that reads the speed of an object passing through two ultrasonic sensors. The plan now is to ping the first ultrasonic sensor once an object passes. The second sensor will then activate and wait for the object to pass by. The time it takes to reach the second sensor will be given into the equation.

Speed will be calculated by the distance between the two ultrasonic sensors divided by the time for the object to reach the second sensor. I would like to display the speed if possible on my computer screen.

Here is my code so far. Any help will be appreciated:

int trig1 = 3; //Trig 1
int trig2 = 4; //Trig 2
int echo1 = 5; //echo1
int echo2 = 6; //echo2
int ECHO1;
int ECHO2;
double first_time;
double second_time;
double final_time;
double speed;


void setup()
{
   pinMode(trig1, OUTPUT);  //sets the digital pin 3 as output
   pinMode(trig2, OUTPUT);  //sets the digital pin 4 as output
   pinMode(echo1, INPUT);  //sets the digital pin 5 as input
   pinMode(echo2, INPUT);  //sets the digital pin 6 as input
}

void loop()
{
  ECHO1 = digitalRead(echo1);

  while (ECHO1 == 0)
  {
    digitalWrite(trig1,HIGH);
    delay(100);
    digitalWrite(trig1,LOW);
    ECHO1 = digitalRead(echo1);
  }

  first_time = time();

  ECHO2 = digitalRead(echo2);

  while (ECHO2 == 0)
  {
    digitalWrite(trig2,HIGH);
    delay(100);
    digitalWrite(trig2,LOW);
    ECHO2 = digitalRead(echo2);
  }

  second_time = time();

  final_time = second_time - first_time;

}
2
  • Delay(100) means your measurement can be of by as much as a tenth of a second. This could cause quite a big error in the calculated speed.
    – Gerben
    Commented Sep 29, 2016 at 7:22
  • What is your question, exactly?
    – jfpoilpret
    Commented Sep 29, 2016 at 20:01

2 Answers 2

2

There are several issues with these ultrasonic modules and your code:

  • How fast are these objects moving? If you don't have any reflection, it'll take almost 20ms for one measure. So precision will be low.
  • What if the object is moving in oposite direction?
  • What if the object stucked between?

and so on.

Anyway Laser interrupter circuit (or similar) would be much more suitable for this job.

Or if you want to use ultrasonic, you can use Doppler effect. But it's not possible with those PING))) modules (or clones).

2
  • So its not really possible to measure the speed with a ultrasonic sensor?
    – flyer3110
    Commented Sep 29, 2016 at 14:15
  • @flyer3110 It might be possible to measure something slow. Or, you can measure distance of approaching object (again it must be slow). For example little faster walk is about 6km/h (1.67m/s) which means 32cm per measure.
    – KIIV
    Commented Sep 29, 2016 at 15:47
1

If I understand correctly you are using the ultrasonic sensors like light barriers.

Measuring speed with ultrasonic would be done by sending the sound towards the object of interest and measuring the time for the reflected sound. The traveling speed of sound in air is somehow a constant. You will get the distance of the object * 2. With two measurements in a defined time period you are able to determine the speed of the object.

When using light barriers (infrared) I would try to trigger interrupts with the photo diodes and measure the time between the triggerings like you described. If hooked to the same interrupt the direction of the object should be irrelevant. If two interrupts are used even the direction is known.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.