Pages: [1] 2   Go Down
Author Topic: Speed Sensor (Velocity Km/H) Using Ultrasonic Sensor (Ping)  (Read 5206 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

i have arduino mega and ping ultrasonic sensor, but the coding is problem for me. hopefully someone can help me......  smiley-cry
« Last Edit: December 30, 2012, 09:47:25 am by matt121187 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's the problem, and why are you SHOUTING?

1kmh-1 is 0.277ms-1

The Ping can take maybe 20 readings a second, out to a sensible range of what? Four metres?

Do some simple sums.
« Last Edit: December 30, 2012, 06:10:49 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can u help me for the coding part?i really blur to do the speed sensor coding...
« Last Edit: December 30, 2012, 09:47:42 am by matt121187 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you stop and think for a moment, and tell us exactly what you're trying to do?
Also think about the limitations I described in my other post.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Your ping sensor is a simple time of flight sensor where the variable time represents the variable distance to a fixed object. To turn that into a speed sensor would require that you sense the change of distance of a return pulse of a moving object over a specific time period, so it's a second order value that would be based on the integration of the primary time of flight measurement that the sensor provides you. So the solution is math based.

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you stop and think for a moment, and tell us exactly what you're trying to do?
Also think about the limitations I described in my other post.


sorry..
i try to do the speed sensor between two car to find the crash impact..

for the example..i put ping ultrasonic sensor in front of my car..than, find the different speed between my car and the front car..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for the example..i put ping ultrasonic sensor in front of my car..than, find the different speed between my car and the front car..

"If you can see my Ping, you're driving too close"   smiley

OK, so show us your code so far.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 107
Posts: 4709
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well assuming you have the coding done to measure the distance in the first place, which is well documented if I recall from looking at Ping)) some time ago....

In it's simplest form you just need to take two distance readings noting the time using millis() or micros() at which you took the readings. Then you calculate the speed as distance / time as:

Speed = (FirstDistance - SecondDistance) / (SecondTime - FirstTime)

If FirstDistance < SecondDistance the distance is increasing and the car in front is moving away; that would give a -ve speed.

Something like that, anyway  smiley-cool

Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for the example..i put ping ultrasonic sensor in front of my car..than, find the different speed between my car and the front car..

"If you can see my Ping, you're driving too close"   smiley

OK, so show us your code so far.

yes..u are too close if u see my ping sensor..hehehe...
i dont know how to start the coding for speed sensor..before this i just 'play' with range..sens and show the range between your car, and front car..

Code:
#include <LiquidCrystal.h>   // lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcd

const int pingPin = 7; //sensor

const int redPin = 6; //led
const int greenPin = 5; //led
const int bluePin = 4; //led

void setup() {
  // initialize serial communication:
 
  pinMode (redPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  
  
  Serial.begin(9600);
  
  
  lcd.begin(16, 2);
  lcd.print("Distance (cm):");
}


void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  lcd.setCursor(0, 1);
  
  long duration, inches,cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(20);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  


  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  // convert the time into a distance

  if (inches > 30) {
    digitalWrite(greenPin, HIGH);  // green LED
    digitalWrite(redPin, LOW);
    digitalWrite(bluePin, LOW);
  }
  else if (inches <= 30 && inches > 12) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);    // orange LED
    digitalWrite(bluePin, LOW);
  }
  else if (inches <= 12 && inches > 6) {
    digitalWrite(redPin, LOW);    // red LED
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  }
  else {
    digitalWrite(redPin, HIGH);    // purple LED
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  }


  lcd.print(cm);
  lcd.print("cm");
  lcd.print("");
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Moderator edit: CODE TAGS
« Last Edit: December 30, 2012, 10:35:49 am by AWOL » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well assuming you have the coding done to measure the distance in the first place, which is well documented if I recall from looking at Ping)) some time ago....

In it's simplest form you just need to take two distance readings noting the time using millis() or micros() at which you took the readings. Then you calculate the speed as distance / time as:

Speed = (FirstDistance - SecondDistance) / (SecondTime - FirstTime)

If FirstDistance < SecondDistance the distance is increasing and the car in front is moving away; that would give a -ve speed.

Something like that, anyway  smiley-cool



thank you for your idea.. if you can see from my code, where i can get the value of secondtime and first time?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
where i can get the value of secondtime and first time?
Here
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 107
Posts: 4709
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Every time through loop() you are measuring the ping duration and then calculating a distance...

Where you read the duration, you could add a line like time = millis so that would record when in the day (or in fact since the sketch started running) that duration was captured.

You'll need to do something to move each current time reading into an oldtime variable (at the top of loop() ?) so that you can subtract one from the other to give the time between readings. Similarly, you'll need to move the current distance to an olddistance variable so you can subtract them.. as it stands now, values like cm and so on are overwritten each time through loop() so you need to have a means of storing the previous one before the new one comes along.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay friends..i will try first...
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 107
Posts: 4709
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay friends..i will try first...

Awesome  smiley-cool .... and my advice is that before you dive into code, draw a flowchart of what it is you want to do. Get the logic clear on paper and in your head before you get into the nitty-gritties of coding.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay friends..i will try first...

Awesome  smiley-cool .... and my advice is that before you dive into code, draw a flowchart of what it is you want to do. Get the logic clear on paper and in your head before you get into the nitty-gritties of coding.

thanks for your advice..i just draw the flowchart..but i bit confusing to get the value of FirstDistance ,SecondDistance, SecondTime, FirstTime smiley-cry
Logged

Pages: [1] 2   Go Up
Jump to: