Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:40 15 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Problem with ultrasonic range

     Page 3 of 6    
Author Message
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2927
Posted: 02:00am 09 Mar 2014
Copy link to clipboard 
Print this post

Hi Frank,

OK - After some thorough testing, and some very strange behaviour, I have the following info for you:

The following code works flawlessly on a TQFP PIC using Pin 1 (Trig) and Pin 2 (Echo)

SETPIN(1),dout
PAUSE 100

DO

PULSE 1,0.1
d = DISTANCE(1,2)
PRINT INT(d)
PAUSE 100

LOOP


According to the manual I should NOT need the SETPIN(1),dout nor the PULSE 1,0.1 lines of code. HOWEVER, if they are not included then the program crashes out whenever distance = -1 (too short or nothing in detectable range). The unit then 'locks' the Echo line high and reading forums, people then have a working solution by power-cycling the Power to the U/S module.

So I then added a transistor to an I/O and checked for a -1 in my loop and if so pulsed the transistor to cycle power. All worked as planned but then I noticed something really strange . . .

- the U/S module worked even without a 5V supply!!!

So I started removing the transistor circuit and ended up back to where I started i.e. 5v supply + Trig (pin1) and echo (pin2).

I have studied the data sheet for SRF04 module upon which these cheap Chinese modules mimic. The code above, and the observed behaviour using a Logic Analyser fall within the data sheet's expectations.
A good reference to behaviour can be found here for those interested.

I suspect something isn't quite 100% bullet proof in Geoff's code maybe relating to the length of Trigger pulse. I will contact him directly to see if he can run his eyes over the code that sends the pulse to the U/S Trig input.

Hope this gets you up and running . . . .

Regards,

Phil




 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 939
Posted: 02:21am 09 Mar 2014
Copy link to clipboard 
Print this post

Hi Phil,

thank you very much! I will try your code!

Frank
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 939
Posted: 06:04am 09 Mar 2014
Copy link to clipboard 
Print this post

Ok Phil,

your code works with my two sensors too!
(Beta 10, 4.5V from battery and 5V from a step-up regulator, no pull ups, 17 - trigger, 18 - echo, 28 pin PI32MX150f128, 40 Mhz, no SETPIN command)

I get a negative distance after values of about 152 (cm?) - but I don't get a "Transducer not responding" error!
That's very good!!! Thanks!!!

I think it would be better to have a error-variable like MM.I2C at the I2C-Bus.
It's a a really great problem when the distance command interrupt the program because a transducer not responding error...


@plasma

I have no problems with near objects - the values are ok, not negative - only at values greater than about 152...


Frank
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6223
Posted: 11:08am 09 Mar 2014
Copy link to clipboard 
Print this post

My transducer has always worked so I am not much use in testing.
It is from Seeedstudio.com and is a three terminal device.

Putting the extra pulse command in before the distance command is effectively sending a longer pulse than the 10uS normally required.

Can you connect up a cro to monitor the echo and try sending a pulse of various lengths without the distance command and see how shorter pulses effect the operation of the sensor.

Start with 100 uS and gradually reduce it to 10 uS.
SETPIN(1),dout
PAUSE 100

DO

PULSE 1,0.1 ' start at 100uS and reduce gradually to 10uS
'd = DISTANCE(1,2)
'PRINT INT(d)
PAUSE 100

LOOP


I suspect that we might need a pulse slightly longer than 10 uS for reliable operation.

The time from the start of the trigger pulse to the start of the return echo in my case is ~460uS which is the same as Frank had on his CRO trace.

Jim
VK7JH
MMedit
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2927
Posted: 01:44pm 09 Mar 2014
Copy link to clipboard 
Print this post

TassyJim,

I will do the tests you mention but a couple of things of interest in the meantime:

1> I shortened the pulse down to 0.004 (4uS) in my code and still got reliable operation (along with the pulse from the DISTANCE command). At 0.003 I got the 'Transducer not responding' error whenever a value of -1 was returned. I don't understand C to decipher Geoff's code on Page one of this post (and am still waiting for a response to my PM to him regarding this).

2> I was going to demo a security sensor with the UltraSonic module and all worked really well UNTIL my wife came into the room wearing a 'fluffy' dressing gown (too much detail! ). Anyway, the Module return a stream of-1 values - obviously because the dressing gown absorbed all the Ultrasonic waves and hence the module never saw a reflection.
Morale of this story - DONT use this module as the basis for a reliable human Motion Sensor!!

Off to do TassyJim's tests - Back soon . . . .


 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6223
Posted: 02:36pm 09 Mar 2014
Copy link to clipboard 
Print this post

Another way to test is to replace the distance command 'trigger' fully.

SETPIN(1),dout
PAUSE 100

DO

PULSE 1,0.1 ' start at 100uS and reduce gradually to 10uS
d = DISTANCE(3,2) ' leave pin 3 disconnected, trigger is still on pin 1
PRINT INT(d)
PAUSE 100

LOOP


This will give one adjustable pulse on the trigger. It will be 10 or so uS sooner than the 'real' pulse but I don't think this will be a problem.

For once, I am annoyed that I don't a temperamental sensor to play with.

And I don't have a fluffy gown in the wardrobe either.
I do have a couple of fluffy dogs I could test it on.

Jim

VK7JH
MMedit
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3272
Posted: 04:44pm 09 Mar 2014
Copy link to clipboard 
Print this post

I cannot figure out what is going wrong here. The code appears to be correct and it accurately generates the 10uS starting pulse.

At this time I don't have a transducer to test but I have a couple of new ones on order and they should arrive soon. When they do I can run some more tests.

At this time the code is quite simple:
- Set the echo pin to input
- Throw an error if the echo pin is high (Transducer not responding).
- Set the trigger to output with the output set to low
- Pulse the trigger pin high for 10uS
- Wait for the echo pin to go high
- If it has not gone high after 2mS throw an error (Transducer not responding).
- Wait for the echo pin to go low
- If it has not gone low after 32mS return -1 (no echo)
- Otherwise calculate the distance

The problem might be caused by the trigger pin floating high before the DISTANCE() function sets it to an output and drives it low. That might keep some transducers permanently triggered. The fix for that would be to install a resistor of 1K from the trigger pin to ground. If someone could test that for me the results will be informative.

It could also be caused by marginal timing on the trigger pulse and that will have to wait until my new transducers arrive so that I can experiment with the timing.

Geoff
Geoff Graham - http://geoffg.net
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2927
Posted: 10:02pm 09 Mar 2014
Copy link to clipboard 
Print this post

Geoff,

The 1K resistor had no effect so I still get the 'Transducer not responding' message whenever a value of -1 is returned.

I can confirm that even a 0.004 pulse sent to the Trig pin (previously set with a dout) works 100% which makes me think that the Trig pin needs to be held low for a brief period of time between your third and fourth bullet points in your above post.

I am about to see how long a Trig pulse is needed to make things 100% reliable too - need a little longer but thought it useful to let you know straight away about the 1K resistor test . . .
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3272
Posted: 12:20am 10 Mar 2014
Copy link to clipboard 
Print this post

Pity, I had hoped that it would work.

There almost no delay between setting trig low and the pulse starting. This should not have been a problem as trig should have been low anyway.

It could be a mix of two things, a floating trig voltage and a marginal pulse width - although the code should work with a module that respected the specs. I think we will have to wait for my new modules to arrive.

Geoff
Geoff Graham - http://geoffg.net
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1967
Posted: 07:34pm 11 Mar 2014
Copy link to clipboard 
Print this post

Just got my HC-SR04s from DX.com. Connected them up using pin 22 Trig and Pin 21 Echo.
Using this code:
Do
x = Distance(22,21)
Print x
Loop

They seem to work fine, I measured from 3 to 30cm,they seemed accurate, below 3cm they start to give a higher reading until I get an error when too close. I successfully measured out to over a metre but here in my workshop there are too many objects to go any further. I will try and do a longer test when I have more time.
Seems like there are some defective units out there.
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: Australia
Posts: 1967
Posted: 07:55pm 11 Mar 2014
Copy link to clipboard 
Print this post

OK just cleared some things out of the way to get a clear view of the opposite wall.
When the distance changed quickly I got an error so I changed the code to

Do
x = distance (22,21)
Print x
Pause 100
Loop

It now works fine and I tested to 4.5 metres, below 3cm it still acts up until it gives an error

Paul.

edit. I do think however the unit needs a lot of clear space at the longer distances to prevent spurious echos

edit2. I'm using the 28 pin 150 chip.Edited by palcal 2014-03-13
"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 Kingdom
Posts: 2927
Posted: 10:12pm 11 Mar 2014
Copy link to clipboard 
Print this post

Hi palcal,

When you say 'gives an error' do you mean the program stops and you get a message saying 'Transducer not responding'?

IF SO then what happens when you try the following code:

SETPIN(22),dout
PAUSE 100

DO
PULSE 1,0.01
x = DISTANCE(22,21)
PRINT INT(d)
PAUSE 100
LOOP

Does this stop the error? If so this is what I have discussed with Geoff.

If you now go back to your code and add a 1K resistor between Echo (pin 21) and Gnd (0v) does the error stop? It did with me but gives a value of around 522 when it should give -1.

Let me know how you get on with any of these tests! . . .
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1967
Posted: 11:06pm 11 Mar 2014
Copy link to clipboard 
Print this post

If I move the target closer and closer I eventually get to about 2.7cm. and then get the error
  Quote  6.71394
6.30231
6.72604
9.70252
-09.7025
[2] x = Distance(22,21)
Error: Transducer not responding
>

these are the last reads and then the error.
If I point the transducers to the sky I get
  Quote  > run
-0n.9999
[2] x = Distance(22,21)
Error: Transducer not responding

I typed in your code there was a mistake with a pin no. but it worked the same as before. This is the result as I moved the target closer to the transducer. It worked OK until I got closer than 3cm then the distance increased until
  Quote   5
5
8
-08.9999
[5] x = Distance(22,21)
Error: Transducer not responding

I will try the 1K resistor and see what happens.
Paul.

edit.
I tried the 1K resistor and it was no different.Edited by palcal 2014-03-13
"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 Kingdom
Posts: 2927
Posted: 11:18pm 11 Mar 2014
Copy link to clipboard 
Print this post

sorry - two errors with my code (been up all night!)

Pulse 22,0.01 NOT pulse 1,0.01[\B]

and

int(x) NOT int(d)

But guess you modified these?

 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1967
Posted: 11:21pm 11 Mar 2014
Copy link to clipboard 
Print this post

Yes I did.
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 Kingdom
Posts: 2927
Posted: 11:30pm 11 Mar 2014
Copy link to clipboard 
Print this post


This just re-enforces my standard expectations with regards to the lack of consistency with very cheap copies of Modules.

The saying 'You get what you pay for' applies here I think! . . . .

Trouble is we are all trying to help Geoff launch the MicroMite to the general public in a form that can be used easily by everyone including beginners. An issue like this can back-fire and put people off (i.e. "it should work but it doesn't").

After many hours trying to resolve this along with communication directly with Geoff, I am confident this is nothing to do with Geoff's coding. I did find a workaround for my Distance Module but if this workaround doesn't resolve your error (for supposedly the same HC-SR04 module) then we need to keep trying.

I know Geoff will receive his Distance modules soon and perhaps we all have to wait to see if he can resolve it somehow in code.

However, my worry is that there will be differences in performance across all these cheap copies of the HC-SR04 but lets wait and see what the 'main man' comes up with . . .


 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1967
Posted: 11:52pm 11 Mar 2014
Copy link to clipboard 
Print this post

DX.com have about 10 different modules the HC-SR04 was the cheapest. They all are 4 pin devices and I expect they would all work OK with the Micromite. It would be interesting to try one of the others.
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
plasma
Guru

Joined: 08/04/2012
Location: Germany
Posts: 437
Posted: 11:53pm 11 Mar 2014
Copy link to clipboard 
Print this post

with my code i get no error , only -1 if the object is to near or to far.
@Palcal try to make a pause 100 in the loop.
i think this stops the error.

my Theory is i: the Sensor needs more Time or the programm try to get a result and spit no data out in the loop ( i mean the pic on the Module)
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2927
Posted: 12:32am 12 Mar 2014
Copy link to clipboard 
Print this post

Plasma, Please confirm the Module(s) you are using! And also your source of these!

Thanks . . .
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1967
Posted: 11:22am 12 Mar 2014
Copy link to clipboard 
Print this post

@Plasma
I have a pause 100 in the loop and my code works fine. The only time I get an error is when the target is too far or too close which is to be expected. I will post a photo of my module and you can compare to see if yours look exactly the same.
Paul
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
     Page 3 of 6    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025