![]() |
Forum Index : Microcontroller and PC projects : Problem with ultrasonic range
![]() ![]() ![]() ![]() |
|||||
Author | Message | ||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
Here is a picture of my module. ![]() ![]() Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
But if too far or too close then expect a -1 value returned BUT don't expect the error 'Transducer not responding' to halt your program. My unit works well to read distances; and returns a -1 when 'out of range' but DOESN'T give the error when I add in a the command PULSE trig,0.01 BEFORE the DISTANCE command. This is surely the behaviour expected rather than it halting the program. The delay of 100mS is in my loop too meet the requirements of the spec sheet. My module performs ok with a delay down to 60mS - much more below that and it behaves too erratically (as would be expected). Also as I previously mentioned, a 1K resistor between Echo and 0v also prevents the program from halting but instead of a -1 value, I get a figure around 522! I have the same looking module as you, but these are what are being copied in their 1000's and sold at pennies simply because of the inclusion of sub-standard components. This is proved by the inconsistent results people are obtaining when using these Modules with a MicroMite. I will order 3 more from different UK suppliers (to minimise delivery times) and see what other peculiarities pop up! I understand Geoff is looking into this amount all his other tasks! |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
OK now I understand, yes I agree the program should not stop. I don't get the -1 value but what you can see in a previous post. I am tempted to buy some different ones from DX but they take about 3 weeks to get here. I would expect that all the four pin modules would use the same technology. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
My HC-SR04 module (4 pins) looks similar to your photos until you study the component layout. I will try take photos of my module and post them so you can see how much the Modules do actually vary. Bear with me cos my kids keep waking up tonight for some reason so may be a delay posting ![]() |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6224 |
I have been doing some more experimenting with the ultrasonic sensors. The code below is a roll-your-own distance program. It allows you to experiment with the width of the trigger pulse which I think might be the culprit. The program uses a simple DO:LOOP to time the return echo so the timing has poor resolution. The code will always return 3.75 as the shortest distance. My sensor times out after 38mS with no target - this is 177 times through the timing loop. Any changes to the loop code, even adding a comment, will alter the timing. If you add a test to act as a timeout, the loop runs a lot slower. ' Ultrasonic module tester TassyJim 15 March 2014
echo_count = 0 trigger = 21 ' digital out echo = 22 ' 5V, digital in trig_width = 10 ' trigger width in microseconds ' trigger width should be 10uS but some units may need longer ' my test sensor was intermittent with a 5uS pulse and ' reliable with 8uS up to at least 200uS setpin 21, dout setpin 22, din main: ' send the trigger pulse ' wait until echo is low ' wait until echo goes high again ' count the number of loops until echo goes low ' one loop is approximately 220uS ' 32 loops for a distance of 120cm echo_count = 0 pulse trigger, trig_width/1000 do loop until pin(echo) = 0 do loop until pin(echo) = 1 do echo_count = echo_count + 1 loop until pin(echo) = 0 print "echo_count = "+str$(echo_count) print "Distance = "+str$(echo_count*3.75)+"cm" pause 2000 goto main If you have a temperamental sensor, could you run this code and try adjusting trig_width. A CRO on the trigger and echo lines would be helpful. I am hoping that the modules just need a longer trigger pulse to behave reliably. If it's the 3.3V level that's the problem, it could be more difficult to resolve. We could run open collector and have a pullup resistor to 5V. Geoff, Measuring long input pulses is easy with interrupts but it would be nice to have a PULSEIN function to get uS resolution for short pulses like we have here Jim Edit: I should have checked my email before posting - Beta 11 is now out so the problem has (hopefully) been fixed. VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
At least now I get the -1 value but the program still ends with the error message I would expect to get -1 when the target gets too close but revert to normal readings when I move the target away again. Paul. The same happens when the target is too far away. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Hi Paul, Is your last post using Beta 10 or 11? I am away from home at moment so not yet loaded Beta 11 to try it. I will do it as soon as home and let you all know if still crashes out whenever -1 value received from my sensor. |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
Sorry I forgot to mention, I was just about to edit. I am using Beta 11. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Again after re-reading I need you to confirm if your comment relates to just using the Distance command in a program, OR using TassyJim's code in the previous post to yours. Thanks for clarifying Think I know as just looked at TassyJim's code and he displays 'cm' as his output |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
Just using the distance command but also tried, SetPin(22),dout
Pause 100 Do Pulse 22,0.01 x = Distance(22,21) Print Int(x) Pause 100 Loop Same result. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
Tried TassyJim's code and it gets stuck in the Do-Loop waiting for Pin(echo) to =1. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Hi, Ok, some good news ![]() ![]() I am testing the Distance command in Beta 11 on a 44-pin MicroMite. Using Pin 1 (Trig) and Pin 2 (Echo) powering four-pin Ultrasonic module at 5V. This is an HC-SR04 cheap Chinese module sold on eBay for under £2.00. All four connection leads in my set-up are under 4cm in length. I am using the following simple test program as used previously with Beta 10: Pause 100
Do d = Distance(1,2) Print Int(d) Pause 100 Loop With Beta 11 I no longer get the program crashing out with an error ("Transducer not responding") whenever a value of -1 is returned. So this appears to have been fixed - So thats my good news! (at least for me anyway. However, palcal still says he gets the error ![]() Unit does return pretty accurate values upto a range of around 2metres. However, much over 2meters and the value is very unstable. For example, pointing at a wall about 3metres away returns a value that 50% of the time is around 320, but the other 50% of the time gives anything jumping randomly from under 100, or in the 100's, or 200's, or 300's or 400's. This is so erratic that it is unusable now at this distance. Before with Beta10 it was stable up to about 3.5metres (but crashed out whenever -1 returned UNLESS I induced a 10uS Pulse on Trig immediately before the Distance command). I am still awaiting more distance units so will test these 3 new ones when they turn up. |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
I will post photo of module (front and back) tomorrow once my camera has charged overnight. |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1967 |
OK got TassyJim's code working. I had to change line 20 from "pulse trigger, trig_width/1000" to "pulse trigger, trig_width" It was rather a short pulse 1/100 of a micro second. The code works well down to about 3cm. and up to 4 metres. But when using the Distance command the program crashes when the distance is too short or too long. Paul. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3272 |
1/100 of a micro second is 10 nano seconds and the Micromite cannot generate that. Paul, exactly what trigger width (in uS) did you use to get it going. Could you please try reducing the trigger width and advise what the minimum working value is (in uS). Your sensor seems to be the only one that is still not working. Phil, nothing at all has changed in beta 11 related to measuring the echo pulse and the distance calculation. I suspect that something else (eg, a spurious echo from your wife) is causing your erratic readings over 3 metres (which is beyond the useful range anyway). Geoff Geoff Graham - http://geoffg.net |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Geoff, It looks like my module has turned faulty as now I am just getting totally strange behaviour. Cheap module syndrome maybe ![]() The only consistent thing is that Beta 11 does not cause a crash of the main program whenever a -1 value is returned so Many Thanks for fixing that issue. Out of interest, what did you alter to get this working? Will hopefully have three new modules early next week. Will return this one as faulty ![]() |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Geoff, If the pulse command second parameter is the duration of the pulse in mS then TassyJim's value of 10/1000 =0.01 mS equals 10uS as required. Or am I overlooking something simple and hence it's time for me to go to bed and have a rest?? |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6224 |
"pulse trigger, trig_width/1000" is only converting the trig_width from microseconds to milliseconds to suit the PULSE command. Beta 11 has increased the trigger pulse from 10uS (which the data sheets want) to 20 uS. Leave line 20 as it was and change trig_width up to 100 (which equals 0.1 mS)and then reduce the value by 10 at a time until things start playing up. I would be surprised if any sensors needed more than 20-30 uS to trigger. Jim VK7JH MMedit |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6224 |
Yes, my code starts with the pulse width at 10 uS Increasing the pulse width to 20 uS seems to suit a lot of sensors without annoying the 'good' ones. My sensor run happily with a pulse width between 8 and 200 uS but I would be wary of pulses too long. Jim VK7JH MMedit |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2927 |
Jim, I assume your code is only correct when running at default MPU speed. Please confirm as its very early in the morning here and my head is 'battered'. Thanks . . . |
||||
![]() ![]() ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |