Guys/Gals - Following my post yesterday [attach below again because it gets buried too deeply in the previous threads and probably won't see the light of day AND I believe hwat I present now vindicates my earlier comments] regarding all of the "expert advice" on this HCSRO4 sensor on this forum that simply do not work as stated "despite being given reassurances they do work"!
I have actually now located a working code for this sensor and provide all of you frustrated HCSRO4 users with the details - so hopefully you haven't thrown out (or jumped on) your sensors prior to now!!! Over the past 24 hours I have found this sensor to be very reliable and reads accurately down to 2 cm and consistently measures 317+ cm - which is the distance to the ceiling/wall junction in my study!!! I have stopped and started this code many, many times and it simply consistently and accurately continues to perform - EVERY TIME WITHOUT FAIL!!!!
Please review the attached pics and word doc showing the full code for this sensor plus the Monitor superimposed on to the Arduino IDE showing the transition from short to long range measurement - AND ABOVE ALL NOW ENJOY THIS RELIABLE SENSOR! I welcome any feedback etc.
I take no credit for this code as I found it lurking on my pc but hadn't previously used it - the creator is Dejan Nedelkovski at www.HowToMechatronics.com - a really excellent site and projects. A word of caution here if you go to Dejan's site as his simple project presented here is the one I have found actually works - he has a far more complex project utilising an TFT 3.2" 320x240 LCD and HC-SRO4 incorporated into the project [and as yet I haven't got the HC-SRO4 part of this particular project to read as it does in the simple project code presented here! My next step will be to "check out" the sensor code in the TFT project and see what is different between these two codes.
Previously:
Guys - I hate to be the bearer of a blinding flash of reality but NONE of your suggestions actually work for the HC-SR04 as claimed, be it either " add these lines after this line, or use New Ping, or what ever - all i ever get is the correct reading on opening the monitor which then returns incessant "0" thereafter until a restart - then the same process repeats!!! I do feel certain I will receive a fair smattering of "oh you must have a faulty unit" - which of course is plausible but there must be one hell of a lot of faulty units doing the rounds globally given the weight of people like myself who have raised this ongoing issue and then given up by bashing our collective heads against the wall!!! I just wish you guys offering suggestions would include the complete code and how you are wiring it up etc etc as it is madeningly frustrating as a newby to follow all the "expert offers of how this works etc", only to find after wasting numerous hours that it simply doesn't!
Please don't get me wrong I am extremely appreciative of the efforts most of you guys go to help us 'not so experienced arduino users' out!
The one thing I haven't yet tried was docdoc's (?) suggestion of using the SR05 code and library then uncommenting the SR04 lock line of code - I would have done this except that I could not get a download of the SR05 library anywhere - at least one that only seemed a few years old at the latest!! But would this really work when using an SR04 unit???
Any helpful comments on trying to resolve this SR04 dilemma would be extremely well received by myself. Unfortunately I only have the SR04 units and do plan on buying the SR05's - but if I do do this - will I simply jumping from the frying pan into the fire????? can any of you guys offer reassurance that the SR05 does not suffer from similar problems the SR04 does? Cheers
Guys - I hate to be the bearer of a blinding flash of reality but NONE of your suggestions actually work for the HC-SR04 as claimed
I hate to be the bearer of a blinding flash of reality too, but a great many of us use HC-SR04 sensors without any problems whatsoever, and get accurate, repeatable results.
I have actually now located a working code for this sensor and provide all of you frustrated HCSRO4 users with the details
I've located a "working code for this sensor" too - "NewPing". And even the code example provided with the IDE works OK, but not as well.
Please don't get me wrong I am extremely appreciative of the efforts most of you guys go to help us 'not so experienced arduino users' out!
Yeah, reading your post, you sure do sound appreciative.
Your whole post looks like a case of sour grapes to me. I don't know what your problem is, whether it's noise, a bad sensor, poor connections, bad coding, or something else, but don't blame those that tried to help you. (I'm sure glad that I wasn't one of them. Obviously a complete waste of time.)
Dear OldSteve - i take your point but it was certainly not intended to be sour grapes as you say! The main point I was trying to convey [and clearly inadequately and also perhaps a bit over the top] was that I do believe (rightly or wrongly) that quite a number of well intended posts do not work as it is said they do for what ever reason. Perhaps a line of text was accidentally left out or wasn't clearly explained in the respective post and was misinterpreted as a consequence. However, the underlying issue is that (I believe) some of the "genuine experts" on this forum oftentimes assume too much existing knowledge of the "newby" asking the questions and it is the brevity of some of the expert replies that can often lead to the confusion by the newbie?
Showing the whole code [or at least the pertinent part] they are providing a fix for would provide a means of overcoming such omissions or confusion rather than just trying to explain the fix in words alone.
In my case I had tried quite a few of the "specific fixes given for the HCSRO4" and let me reassure that these failures certainly were not due to any of the reasons you put forward. Had you clearly read the top part of my post you would have realised this as there was nothing wrong with my sensor etc etc, as it turned out and the code I posted confirms this.
Until I found the working code I was just about to send this sensor into oblivion under the hammer based on the assumption that you guys are the experts in this matter and must be correct. Cheers
Hi All,
Never had a problem with them myself! I don't even bother with the special libraries either, this bit of code does it for me every time.
long scanner(long cm)
{
const int pingPin=7, EchoPin=8;
long duration;
//The HC/RC US devices are triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);
// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return (cm);
}
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
Cactusface:
I don't even bother with the special libraries either
The (NewPing) library is handy if you want a non-blocking version. That's the downside of the standard 'pulseIn()' method - 'pulseIn()' is blocking, so some events in 'loop()' can be missed. The 'ping_timer()' method is timer-based, so this isn't a problem.
Also, the 'ping_median()' function is very handy if you do get fluctuating results, from uneven surfaces like cloth etc. It takes a (user-determined) number of readings, discards out-of-range readings, then averages the rest.
In some situations I've used the standard 'pulseIn()' method, but find myself using NewPing more and more for the reasons I just mentioned.
Of course, we could do all of that ourselves, but why re-invent the wheel?
knut_ny:
There are several versions of this sensor..
They have x-tals 4MHz, 8MHz and 27MHz. The opamps use are also different TL074/LM323
Mine 4Mhz-versions all work OK when board is runnning @ 4V..4.9V , but fails @5V
Those with the 8MHz x-tal are fine.
I just had a look at the one I have sitting here at the moment, but there are no markings on the crystal so I'm not sure how fast it's running. It works well at 5V though, so I guess it's not a 4MHz version.