Speed Sensor Using Ultrasonic Sensor

hello everybody..
can someone help me on this problem?

i have arduino uno and ultrasonic hc-sr04 sensor. i can program to find distance. but how to find difference between two change in time. so i can find speed of movement from a distance per time. like km/h, but the coding is problem for me. hopefully someone can help me...... =(
like this video >>> Flash / Arduino Based Speed Detector on Vimeo

Your main problem is likely to be your expectations.
A typical U/S sensor can take around 20 readings a second, but only over a range of about 3 or 4 metres.
If your object is fast moving, you may not get enough readings in the time it takes to pass the sensor for an acceptable result.
Forget about Doppler.

What are you trying to measure?

AWOL:
Your main problem is likely to be your expectations.
A typical U/S sensor can take around 20 readings a second, but only over a range of about 3 or 4 metres.
If your object is fast moving, you may not get enough readings in the time it takes to pass the sensor for an acceptable result.
Forget about Doppler.

What are you trying to measure?

speed of the car when it passes through the sensor. sorry for bad english

So, it's a very slow-moving car?
Take a range reading and time stamp it.
"millis()" is useful here.
Take another range reading and time stamp that.
Calculate ds and dt.

AWOL:
So, it's a very slow-moving car?
Take a range reading and time stamp it.
"millis()" is useful here.
Take another range reading and time stamp that.
Calculate ds and dt.

I just made ??a prototype only using the remote control car.

like this link >>>     K-Art: Speed measurement with Arduino

I will begin with my first experience working with Arduino and Arduino IDE. I made a speed detection "device" using 2 laser pointers and 2 LDR sensors connected to an Arduino UNO.

The resistors are used as pull-down resistors and I wired the sensors and put them in a case, to avoid them detecting surrounding light. For each case, a hole was drilled so that the laser beam can light the sensor while the ambient light does not affect the sensor.
The working principle is easy: an object that passes by will "cut" the laser beams, this means the LDR sensor will detect this sudden drop of light intensity. First I defined a threshold value under which the sensor is considered triggered, once the value is under threshold for the first sensor then Arduino waits for the second one to be triggered. During this waiting time it counts the elapsed time between the two events. When the second beam is interrupted, the timer stops and now is just simple math. The distance between the 2 sensors is known, the time between the two events is known, and speed can be computed as speed = distance/time.

Below you can find the Arduino code:

/*

by Claudiu Cristian
*/

unsigned long time1;
int photocellPin_1 = 0;     // 1st sensor is connected to a0
int photocellReading_1;     // the analog reading from the analog port
int photocellPin_2 = 1;     // 2nd sensor is connected to a1
int photocellReading_2;     // the analog reading from the analog port
int threshold = 700;        //value below sensors are trigerd
float Speed;              // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable

void setup(void) {
 // We'll send debugging information via the Serial monitor
 Serial.begin(9600);  
}

void loop(void) {
 photocellReading_1 = analogRead(photocellPin_1);  //read out values for sensor 1
 photocellReading_2 = analogRead(photocellPin_2);  //read out values for sensor 2  
 // if reading of first sensor is smaller than threshold starts time count and moves to calculation function
 if (photocellReading_1 < threshold) {
  time1 = millis();
  startCalculation();
}
}

// calculation function
void startCalculation() {
 calcTimeout = millis(); // asign time to timeout variable
 //we wait for trigger of sensor 2 to start calculation - otherwise timeout
 while (!(photocellReading_2 < threshold)) {
   photocellReading_2 = analogRead(photocellPin_2);  
   if (millis() - calcTimeout > 5000) return;
 }
 timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
 Speed = 0.115 / timing;  //speed in m/s given a separation distance of 11.5 cm
 delay(100);
 Serial.print(Speed);
 Serial.print("\n");  
}

how to implement the code with ultrasonic sensors?

Tricky - that code assumes that the car breaks two light beams, and calculates the time difference.
You wouldn't normally use ultrasonics in a beam, because most ready-built units are not designed for continuous operation.

AWOL:
Tricky - that code assumes that the car breaks two light beams, and calculates the time difference.
You wouldn't normally use ultrasonics in a beam, because most ready-built units are not designed for continuous operation.

but how to use like this >> http://jurtek.akprind.ac.id/sites/default/files/120_128_slamet_hani.pdf >> velocity measuring system is the first measures is how the reflection distance from ultrasonic sensor less than maximum limit monitoring. Sensor will begin to calculated time and when the second sensor obtain reflection time will be stopped. The velocity value that has gotten is result of quotient between both sensor with bounce time sensor

I can't read that paper.
Even the abstract looks like Google translate was confused.

AWOL:
I can't read that paper.
Even the abstract looks like Google translate was confused.

this the flowchart and design form mockups.

So, problem solved.

AWOL:
So, problem solved.

no =( how to sensor will begin to calculated time and when the second sensor obtain reflection time will be stopped?the coding is problem for me =(

#include <NewPing.h>
unsigned long time1;
#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     6  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN2  9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2     8
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
int threshold = 700;        //value below sensors are trigerd
float Speed;              // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable


void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
if (uS < threshold) {
   time1 = millis();
   startCalculation();
 }
}

void startCalculation() {
   unsigned int uS2 = sonar2.ping();
  calcTimeout = millis(); // asign time to timeout variable 
  //we wait for trigger of sensor 2 to start calculation - otherwiuse timeout
  while (!(uS2 < threshold)) {
    uS2 = digitalRead(ECHO_PIN2);  
    if (millis() - calcTimeout > 5000) return;
  }
  timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
  Speed = 0.4 / timing;  //speed in m/s given a separation distance of 40 cm
  delay(100);
  Serial.print(Speed);
  Serial.print("\n");  
}

I'm sorry, I don't see your problem.
You know the time your first detector got a range measurement that indicated an object in front of it.
You know the time your second detector got a range measurement that indicated an object in front of it.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

If you are going to number one of the variables, you really should number them both. That's the way you count, isn't it?

AWOL:
I'm sorry, I don't see your problem.
You know the time your first detector got a range measurement that indicated an object in front of it.
You know the time your second detector got a range measurement that indicated an object in front of it.

yes i know. when the sensor 1 and sensor 2 got a measuring range that indicates an object in front of it, as in the mockup there is a wall distance of 20 cm. And there is a car through the sensor 1. when the distance of the car > 3 cm or <20 cm (flowchart), then the start time and when the car got to the 2nd sensor, second sensor is interrupted, and time stopped.
the problem is how to make the code when a car pass through, then sensor 1 start time and when the car is up to the second sensor, the time stop

PaulS:

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);



If you are going to number one of the variables, you really should number them both. That's the way you count, isn't it?

yaaaa

I'm sorry, I really don't see your problem here.
You may be overthinkng.
Record the time when your first event happens.
Record the time when your second event happens.
Subtract the first time from the second
Don't forget; if your car is very small or very fast, neither sensor may even detect your car.

Hey,
i am just working on the problem statement but can this not be done with 1 ultrasonic sensor?

can this not be done with 1 ultrasonic sensor?

It could. You record when you send the first pulse, and the distance you get back. You record when you send the second pulse, and the distance you get back. The change in distance divided by the change in time is the speed. IF the change in distance is reasonable, then the speed will be. If not, the speed will be a SWAG.