# Distance measurement using range sensor

#### vivek20055

Joined Nov 4, 2012
88
Hi,

I am measuring the distance using range sensor SRFO5.

The sensor is having the maximum range of 400 cm.

When I trigger the sensor, it releases echo signal which is used to measure the distance of the object.

The echo signal will become zero when the object is detected.

The counter value increments which measures the echo width and beccomes zero when the object is detected.

This counter value should be divided by 58 to convert it to cm.

For division I am multiplying with 3 and then right shift by 13 places which converts the value to cm.

Rich (BB code):
code:
process(echo, clk)
begin
if clk'event and clk ='1' then
if echo = '1' then
range_cm <= count1(23 downto 0)* "11";   --measuring distance
input <=range_cm(21 downto 13);	 --
else
output <=input;
end if;
end if;
end process;
I took only nine bits because the maximum range is 400 which can be represented in 9 bits

My professor told me that I need to consider MSB bits also. But I do not understand why I need to consider those bits

Regards
xilinx1001

Last edited by a moderator:

#### GopherT

Joined Nov 23, 2012
8,012
Right shifting by 13 places is equal to $$2^{13}$$ (like dividing by 8192).

Is that really what you intended?

#### Bob T.

Joined Oct 22, 2012
35
What language is this

#### vivek20055

Joined Nov 4, 2012
88
Hi,

Yes, you are right. I am dividing it by shifting 13 places to the right.

Is there any other method of doing this?

Regards
vivek20055

#### GopherT

Joined Nov 23, 2012
8,012
Multiplying by 3 then dividing by 8192 is like dividing by 2730. I don't understand why you are dividing by 2730 instead of dividing by 58 like you explained above.

#### dougy83

Joined May 11, 2011
12
You'd multiply by 141 then shift by 13 to divide by 58 (well, 58.099). You'll want to make sure that the multiplied value is stored in a variable of sufficient size and that the operation is not truncated.

The reason you'd want to use the MS bits as well would be if it's possible for the count to be greater than what 9 bits can hold.