Go Down

Topic: Sensor measuring distance between another sensor (Read 872 times) previous topic - next topic

ariath

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? ;)
Thanks

robtillaart

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: [Select]

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;
}
Rob Tillaart

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

ariath

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?

Graynomad

#3
Mar 04, 2013, 03:06 pm Last Edit: Mar 04, 2013, 03:08 pm by Graynomad Reason: 1
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
Rob Gray aka the GRAYnomad www.robgray.com

ariath

#4
Mar 04, 2013, 04:28 pm Last Edit: Mar 04, 2013, 05:15 pm by ariath Reason: 1
@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 ;)

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

robtillaart

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

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.
Rob Tillaart

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

ariath

OK, thanks :)

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 ;)
Now I have to buy some ultrasonic emitter and mic.

Thanks for all.

robtillaart

PLease keep us informed of the progress you made (or if some quirk pops up in the code ;)
Rob Tillaart

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

GoForSmoke

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up