Ultrasonic distance sensor...


33 posts   Page 1 of 2   1, 2
by Bigcat123 » Mon Sep 17, 2012 4:18 pm
Ok... I have just purchased a ultrasonic distance sensor from eBay.... This is the one:

http://item.mobileweb.ebay.co.uk/viewit ... 0436097359

And while it is taking its time to arrive I was wondering if anyone has used this or a similar product... If you have could you please be kind enough to share any info or point me in the right direction of a tutorial!? Thanks a million :)
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by MancunianLee » Mon Sep 17, 2012 4:24 pm
I couldn't work out how to wire this up, you need to put a 10k resistor between two pins, but couldn't sort it out. Bought another and had the same problem, so only really adding this reply so I can follow the thread :D
Posts: 4
Joined: Fri Jun 01, 2012 7:29 am
by techpaul » Mon Sep 17, 2012 4:58 pm
Firstly you will need to level translate the Echo signal to 3V3 levels.

You need to generate an [b[accurate[/b] 10 us pulse to trigger trhe device, possibly every 60mS so I would suggest using the GCLK output for that.

Secondly you need to measure a returned pulse width, which Linux and task management means that is difficult in software, this would be best done with an external counter reset every trigger pulse. This counter should really feed a data latch to ensure last reading is saved, then read the output of the latch, via GPIO.

I would use a tiny micro with hardware timer/counters to do this as it is possible to get accurate repeatable results.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by mikerr » Mon Sep 17, 2012 5:02 pm
Used that on arduino - powered it with 5v to Vcc, and connected pins to Trig and Echo

Arduino code for a single reading:
Code: Select all
digitalWrite(Trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(Trig_pin, LOW);
duration = pulseIn(Echo_pin,HIGH);

distance = duration  / 60


Pi will be similar, but as mentioned above, resistors needed on the GPIO pins
Got a Pi Camera? View it in my android app - Raspicam Remote ! No software required on the pi
User avatar
Posts: 1216
Joined: Thu Jan 12, 2012 12:46 pm
Location: NorthWest, UK
by cTn » Mon Sep 17, 2012 5:46 pm
Ill recommend using an separate microcontroler that can handle 5v logic, you could use PICAXE or AVR, currently i don't have much experience with this HF sensor, but quite a few HF sensors are on my way (should arrive in a week or two) and i will be using an Arduino Nano to generate the trigger signal + measure the echo, you can connect arduino via I2C to your raspberry pi as an slave device (At least this is one of the ways how to do so, the choice is up to you really).
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
by Bigcat123 » Mon Sep 17, 2012 6:17 pm
Hold on a second.... Didn't understand most of that! What do the legs do and where should I wire them? I would rather not get a Arduino nano.... I thought that people had used this before with Pi (only) robots? Is that not the case? And sorry for the rather noobish question here but.... How does it actually work? I know it uses Ultrasonic but.... What else?

Thanks for all your help guys!
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by techpaul » Mon Sep 17, 2012 6:40 pm
Bigcat123 wrote:Hold on a second.... Didn't understand most of that! What do the legs do and where should I wire them? I would rather not get a Arduino nano.... I thought that people had used this before with Pi (only) robots? Is that not the case? And sorry for the rather noobish question here but.... How does it actually work? I know it uses Ultrasonic but.... What else?

Thanks for all your help guys!

VCC to +5V from Pi
GND to GND on Pi
Trigger to GCLK output
ECHO needs a special circuit otherwise you will not guarantee accuracy of results as you cannot guarantee being able time the ECHO signal coming back. Also needs translation to 3V3 from 5V.

If others have used it they may have made theior own operating system standard Linux builds will not allow you to easily measure how long an output is high for on close distances as the pulse gets too short
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by cTn » Mon Sep 17, 2012 6:41 pm
Bigcat123 wrote:Hold on a second.... Didn't understand most of that! What do the legs do and where should I wire them? I would rather not get a Arduino nano.... I thought that people had used this before with Pi (only) robots? Is that not the case? And sorry for the rather noobish question here but.... How does it actually work? I know it uses Ultrasonic but.... What else?

Thanks for all your help guys!

Well, lets see :)

The HC-SR04 has 4 pins, vcc, trig, echo gnd, you connect +5v to the vcc pin, and ground to the gnd pin,
then to make HC-SR04 send a pulse you need to send an 5v HIGH signal (somebody correct me if i am wrong, but as far i know 3v3 signal wont do) which is 10 micro seconds long, after that you need to listen to the signal on Echo, sensor will reply with an PWL signal (which is still in 5v logic), which will represent the distance between the sensor and the object (you will find out how to convert the signal to an human readable output in the HC-SR04 datasheet)

So there is a couple of problems you need to solve first if you wanna use this sensor "directly" from your pi, first you will need some sort of logic converter from 3v3 to 5v, then you need to send at least 10 micro seconds long signal via gpio (which will be a problem again, because linux isn't an realtime OS) so timing such a small signal will be difficult and then you need to listen and time the echo signal on a different pin (which again needs to be converted from 5v logic to 3v3 logic) but again monitoring/timing such signal on your raspberry pi would be difficult because of the non realtime os).

That's why i suggested using an separate microcontroller to do this job and then you would just read the values from the microcontroller with your raspberry pi.

Hope this helps?

edit: seems techpaul was faster :)
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
by Bigcat123 » Mon Sep 17, 2012 6:58 pm
So a logic converter eh...? I will have a look into it... One last thing: I do not need this to be that accurate and so is it a problem that Linux is not a realtime OS? I just want some pretty basic proximity stuff!

Thanks so so much! :D
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by Bigcat123 » Mon Sep 17, 2012 7:03 pm
I have had a look into Logic converters! I am no expert as I have just done so for 2 minutes!

Where can I get one from? (UK seller as cheap as possible!) And how do you wire one up?

Thanks so so much!
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by mikerr » Mon Sep 17, 2012 7:20 pm
Joan on here has used this sensor with the Pi:

http://www.youtube.com/watch?v=xKSVp78PhWQ

Not sure if that was directly, or via a buffer...
Got a Pi Camera? View it in my android app - Raspicam Remote ! No software required on the pi
User avatar
Posts: 1216
Joined: Thu Jan 12, 2012 12:46 pm
Location: NorthWest, UK
by techpaul » Mon Sep 17, 2012 7:32 pm
Plenty of different circuits about from simple FET to buffers to level translator chips. Search for this on forum will give many links.

Alternatively you can look at my facebook and website links for board that level translates bi-directionally ALL GPIO signals on main GPIO connector
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by cTn » Mon Sep 17, 2012 7:47 pm
well the cheapest one i could find on ebay is this http://www.ebay.com/itm/Bi-Directional- ... 3cc9808653

But i would still rather recommend using an separate microcontroler, because even using a logic converter the crucial timing would be done on rpi, i don't know how the "buffer" technique works (sorry)
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
by cTn » Mon Sep 17, 2012 7:51 pm
and even buying that (its 5$ +5$ shipping), you could buy an arduino nano for 13$ (which would make things much simpler for the raspberry) and you could have lots of fun with it later on

http://www.ebay.com/itm/Meduino-Nano-En ... 2321b85a02
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
by techpaul » Mon Sep 17, 2012 7:56 pm
Last time I bought translators from Farnell they were about £1.50 in 10 offs about £2 each for an 8 way bi-directional level translator.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by Bigcat123 » Mon Sep 17, 2012 8:01 pm
I think I will probably get an Arduino... Even if I can't program it on the Pi (Due to the Arduino IDE not fitting in on my crappy composite display)... Is there away to 'link' your programs. As in when I do this on the Pi, run this?

Thanks you have been a great help!

Plus: I can also control the motors with the Arduino! Do you need a shield for simple motor control? :?: :)
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by Bigcat123 » Mon Sep 17, 2012 8:05 pm
Also If I do get an Arduino what would I use the Pi for in my Robot?
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by cTn » Mon Sep 17, 2012 8:16 pm
that depends on the type of motors, if they are servos (+ pin, ground, signal) then you can just connect the signal pin to one of the pwm enabled pins on arduino, but if they are just simple motors (2 cables), then you will need an arduino motor shield for that.

Sadly it seems like that i "burned" my self a little, because arduino is using 5v logic, you can't connect it directly to the pi without the logic converter (which adds another extra costs to your project), but even when you buy an logic converter, you won't be able to time the signals right on your pi, so an extra microcontroler.

If you can afford to wait and do some trial and error on your own, i would buy the logic converter, test how accurate readings of the sensor you can get with your pi, and then depending on the result buy the extra arduino.

To the second question, don't forget that the arduino can run only simple operations compared to code that you can run on your pi.
Posts: 49
Joined: Sat Aug 11, 2012 4:11 pm
by Bigcat123 » Tue Sep 18, 2012 8:37 pm
I know that 'Linux isn't real time' but how do you program 10 micro seconds??? As accurately as possible??? Thanks :)
Just a beginner sharing his experiences on his way to geek nirvana...
User avatar
Posts: 223
Joined: Thu Aug 23, 2012 2:41 pm
by techpaul » Tue Sep 18, 2012 9:41 pm
Look up Wiring Pi that has delay functions for less than 100 micro seconds

There are some other threads mentioning nanosleep function
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1509
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by MattHawkinsUK » Fri Jan 04, 2013 8:02 pm
I've got one of those Ultrasonic modules. I used a simple resistor divider to convert the 5V output to a more Pi friendly 3V.

Image

Vcc goes directly to Pin 2 on the GPIO header (5V) and GND goes to Pin 6 (Ground). I picked two GPIO pins for the TRIGGER and ECHO signals.

Further details on my blog :
http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/

The accuracy was quite good, even using Python. Good enough for basic distance measuring or collision avoidance! (in non-safety critical applications).
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)
User avatar
Posts: 485
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
by glaursian » Sun Feb 03, 2013 10:44 am
hello...i have an "error" on the console is shows me the message "sensor didn't fire"...what could be wrong that i did?
i conected the vcc to pin 2 (5v), gnd to pin 6...trigger to pin 18 and echo to pin 23...i installed the python gpio...what else?? :?
Posts: 3
Joined: Sun Feb 03, 2013 10:37 am
by joan » Sun Feb 03, 2013 10:55 am
Most likely problem is a mismatch between the gpios you think you are using and the gpios Python has been told to use. There are conflicting numbering schemes.

You should probably use a voltage divider on the echo line to get 3.3V from the 5V output (the gpios are not 5V safe).
User avatar
Posts: 5494
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by glaursian » Sun Feb 03, 2013 11:00 am
with python works...but not with c++ :|
Posts: 3
Joined: Sun Feb 03, 2013 10:37 am
by glaursian » Sun Feb 03, 2013 11:17 am
Some images...
Image
Image
Image
:|
Posts: 3
Joined: Sun Feb 03, 2013 10:37 am