# Read speed between two ultrasonic sensors

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()
{

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

first_time = time();

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

second_time = time();

final_time = second_time - first_time;

}
``````
• 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 Sep 29 '16 at 7:22
• What is your question, exactly? – jfpoilpret Sep 29 '16 at 20:01

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).

• So its not really possible to measure the speed with a ultrasonic sensor? – flyer3110 Sep 29 '16 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 Sep 29 '16 at 15:47

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.