Pages: [1]   Go Down
Author Topic: Sensor measuring distance between another sensor  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I've recently been looking for a way to measure distance between 2 sensors (not between sensor and closest obstacle) with Arduino, but I couldn't find anything, so I guess it's not that simple.
I need 2 sensors to measure distance between them, no matter if there's any obstacle between them (by obstacles I don't mean walls or anything like this but a sheet of paper or something smaller). Also I need about 0.5 cm precision to measure distances from 1 to 10 cm. Is this possible, or is it too difficult problem for today's technology? smiley-wink
Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the speed of sound is 34000 cm per second ==> 34 cm per millisecond.
for a 0.5 cm precision you need to be able to measure in the order of 15 microseconds (1/68 of a millisecond).
That means you probably have to use a hardware timer of the Arduino, [or at least do direct port reading]

Start counting after the blib and stop counting when receiving.
To know when the blib starts you could use an electrical or optical pulse and some IRQ routine.
So it seems to me technically possible with an Arduino but it will be on the edge of what is possible.

Something like this might get you started. It assumes a second Arduino to send a sound pulse and at the start it gives some electrical pulse too
It will be a bit easier when you start
Code:
volatile bool wait = true;
uint8_t mask = 0x40; // some value  determined by the pin you use.

void setup()
{
  Serial.println(115200);
  attachInterrupt(0, ISR, RISING); // this waits for the electrical pulse from a 2nd arduino.
}

void loop()
{
  while (wait);
  unsigned long start = micros();
  while (PORTD & mask == 0);  // wait for the pulse
  unsigned long stop = micros();  // micros() will have a 4uS granularity == ~0.2 cm per measurement => 0.5 for 2 measurements is just feasable this way.
  unsigned long duration = stop - start;

  Serial.print("Time used = ");
  Serial.println(duration);
  Serial.print("Distance = ");
  Serial.print((340UL * duration + 500UL)/1000UL);  // includes rounding UL = Unsigned LOng
  Serial.println("mm");
  wait = true;
}

void ISR()
{
  wait = false;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Thanks for your answer.
I don't know if I understood you correctly, but does it work like this: that I have 2 wire connected arduinos, the first one waits for electrical pulse through a wire from the second one, and when it receives it then second arduino sends sound pulse and then first one reads it and calculates distance? Will it be accurate? I mean, electricity also requires time to travel, doesn't it?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8517
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I mean, electricity also requires time to travel, doesn't it?
Yes, but not enough to worry you in this application. For all intents and purposes the trigger pulse will be "instant".

Quote
the first one waits for electrical pulse through a wire from the second one, and when it receives it then second arduino sends sound pulse and then first one reads it and calculates distance?
Close, but the one sending the pulse is also the one reading the return signal.


______
Rob
« Last Edit: March 04, 2013, 09:08:59 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

@up I meant it this way: second one sends electric pulse and sound pulse simultaneously, if the first one receives electric pulse then it knows that it should start measuring time and when it receives sound pulse then it stops, so the first only receives and second one only sends. Sorry for not being accurate smiley-wink

Anyway, sounds like a great idea and not that hard to do.

3 more questions:
Will I be able to tell one sound pulse from another so this measurement system won't get interrupted by another one? Different wave lengths or something?
This sound pulse won't be audible by human, right? smiley
What if there will be an, let's say, eraser or anything else small between these 2 sensors, will it still work accurate? What if there will be a sheet of paper?

Thanks in advance.

//edit
oh, and what sound sensor and emitter should I have? I need this sound emitter to be able to spread sound in all directions, not just 1 direction.
« Last Edit: March 04, 2013, 11:15:19 am by ariath » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
3 more questions:
Will I be able to tell one sound pulse from another so this measurement system won't get interrupted by another one?
Different wave lengths or something?
This sound pulse won't be audible by human, right?
What if there will be an, let's say, eraser or anything else small between these 2 sensors, will it still work accurate?
What if there will be a sheet of paper?
That are 5 questions smiley-wink

1) No, on 2nd thought, depends on the sensor your use
2) to be investigated, you didn't tell that much about the context of your experiment / application / project
3) is an option, ultrasound travels at the same speed AFAIK
4) don't know, depends on the size of the transmitter/receiver and the thing in between.
5) it will at least dampen the signal strength,

Good questions BTW, you need to build and experiment to get the answers, but you understood the concept.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

OK, thanks smiley

I'm gonna spend some time experimenting and trying to get it work. I'll go with ultrasonics.
Interrupting by another sound emitter when having more than 1 is not a problem anymore. I'll just iterate through all emitters one after the other, not at the same time.

If this works and is quite accurate there will be plenty usages for it for me like motion capture for 3d skeletal animation smiley-wink
Now I have to buy some ultrasonic emitter and mic.

Thanks for all.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PLease keep us informed of the progress you made (or if some quirk pops up in the code smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Low frequency sound is better at getting around or through things than high frequency. It is also less directional.

If no substantial magnetic metal (iron, most steel, some exotic stuff) or moving conductors or current carrying conductors get in the way then you can tell distance by magnetic field strength.
It would be best to pre-calibrate that one by taking measures alongside a scale and trying different orientations of magnet and linear Hall sensor.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: