Welcome, Guest. Please Login.
YaBB - Yet another Bulletin Board
May 10th, 2025, 2:11pm
News: Welcome to the Cabin! If you want to register send me an e-mail. you can link to my e-mail under the welcome page.


Pages: 1
Using an Ultrasonic sensor on the Raspberry Pi. (Read 107 times)
Fernando
YaBB Administrator
*****
NY City




Posts: 2380
Gender: male
Using an Ultrasonic sensor on the Raspberry Pi.
Jan 27th, 2017, 3:03pm
 
I have always thought that this sensor (the HC-SR04) was a bit more accurate in its resolution and thought it would be useful for ultra-sonic hi-res work. It is however still useful in many apps one needs to measure the distance of things with. And, according to one of the videos, has a range of 500cm, which is 5 meters or about 16ft with a +/- .3cm or 3mm (about an 1/8th of an inch) resolution.
 
The HC-SR04 is very cheap on ebay, like $2 to $3 each and as low as $1.50 if you buy in bulk. Unfortunately, my walk-in & buy source for many parts - Tinkersphere, has them for over $9 a piece.
 
Thing is how to connect it to a R-Pi or Arduino? And how to collect the information it receives?
 
Connecting it is a bit tricky as it is a 5V sensor with 5V data, which can burnout your R-Pi. Thus you need to connect a couple of resistors to it to lower the 5V data to around 3v for the R-Pi to handle safely. so double check your wiring before turning on the R-Pi. It only uses 4 wires and 2 resistors on a bread board. You will need 2 GPIO pins for this, one to transmit the pulse and another to read the echo; the difference in time is your distance. this takes some math to do.
 
Here is 2 videos that shows how to do it. The first one says to "go to Github and get my software for this" while the second shows you the python scrip on how to do it. The second is better for the programming while the first shows how to wire it up.
 
Video 1: https://www.youtube.com/watch?v=ShnzQSFwVXQ
 
Video 2: https://www.youtube.com/watch?v=xACy8l3LsXI
 
Personal note: From the looks of it, you can have more than one sensor and all use the same trigger GPIO pin, but each echo read needs its own GPIO Pin. So you can have a robot or device that measures front, back and the sides with 4 sensors. You just need to add about 4 lines of code per sensor to get each own measurements calculated and printed.
 
If you are interested in the Arduino version of connecting this sensor to it, see this video:
https://www.youtube.com/watch?v=ZejQOX69K5M
 
I recommend you should see this Arduino video as it explains the principles behind it and how things work mathematically.
Back to top
 
 
View Profile   IP Logged
Hondo I. Sackett
YaBB Administrator
*****
Behind you!




Posts: 1349
Gender: male
Re: Using an Ultrasonic sensor on the Raspberry Pi
Reply #1 - Jan 27th, 2017, 7:54pm
 
Neat!
Back to top
 
 

Well the cowboy, like the red man, you had to leave your land
You can't raise your stock and plant your crop in the gumbo and the sand
Greed disguised as progress has put us to the test
They won't be glad until we're gone from our home out in the west
It's sad to see those good old days replaced with greed and doubt
Soon we'll leave the country, the campfire has gone out
Bid 'em all adieu, you can't turn the world about
The cowboy left the country, the campfire has gone out
View Profile WWW   IP Logged
Pages: 1