Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:42 14 Jul 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 : HC-SR04 Ultrasonic Transducer

Author Message
busa
Regular Member

Joined: 12/02/2015
Location: Australia
Posts: 81
Posted: 03:38am 13 Apr 2015
Copy link to clipboard 
Print this post

Hi all,
In our garage at home the 4WD fits with just 6 inches to spare before it ends up in the spare bedroom. Understandably the wife refuses to park it so it is left to me. I came up with the idea to use the Micromite Mk11_4.6b with 3 LED's and a cheap HC-SR04 Ultrasonic Transducer from Ebay to help make the parking a little easier for both of us. I have posted the code below and what I thought would be a simple job is starting to make my brain hurt. As it stands the code works well and with anything beyond a metre away the flashing green LED signalling all is good. Herein lies the problem. When the all clear is given I don't want the green LED to flash but rather a solid green. If I comment out the last two lines of the SUB allClear the transducer starts giving ridiculous readings and is unusable. I am probably doing something wrong here but because I have been trying to figure it out for the last day or so I can't see what. I would appreciate if some of you guys out there could have a look at my code and see if I am making some simple silly mistake. The print lines are for debugging and will be removed if I get it working how I want it.
Regards

option autorun on
for i = 2 to 4
setpin i,dout
next i

const red = 2, green = 3, amber = 4
pause 1000


do
d = distance(15,16)
select case (d)
case is <= 15 'If distance to wall is 15cm or less red LED will come on solid
mustStop
case is <= 40 'If distance is 40 cm or less and greater than 15cm red and amber LED's will flash
prepareToStop
case is <= 100 'If distance is a metre or less and greater than 40cm amber LED will flash
caution
case else 'If distance is greater than a metre the green LED will come on solid
allClear
end select
loop

sub mustStop
pin(red) = 1
pin(green) = 0
pin(amber) = 0
? d, "cm STOP NOW!"
pause 200
end sub

SUB prepareToStop
pin(red) = 0
pin(amber) = 1
pin(green) = 0
pause 100
? d, "cm Prepare to STOP"
pin(red) = 1
pin(amber) = 0
pin(green) = 0
PAUSE 100
END SUB

sub caution
pin(red) = 0
pin(amber) = 1
pin(green) = 0
pause 100
pin(red) = 0
pin(amber) = 0
pin(green) = 0
? d, "cm CAUTION"
pause 100
end sub

sub allClear
pin(red) = 0
pin(amber) = 0
pin(green) = 1
pause 500
? d
pin(green) = 0
pause 500
end sub
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 05:56am 13 Apr 2015
Copy link to clipboard 
Print this post

OK - a few possible solutions:

1> Extend the garage
2> Swap the vehicle for a smaller one
3> Move the spare bedroom (allows you to do point 1 above)
4> Change the wife for one that can park it (perhaps best not do this one!)

To avoid any of the above I will take a look at the code and get back to you.

Just out of interest I have had various issues in getting reliable readings with these 'cheap' U/S units (have tried upwards of 30 units). Geoff managed to get the firmware working well (during the Beta testing) but things appeared to be a bit 'random' again when I next went to play with one. I have put it down to the U/S modules - not Geoff's firmware.

Will check your code later tonight and get back to you . . . .

WW


 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 06:05am 13 Apr 2015
Copy link to clipboard 
Print this post

A quick question - what power supply are you using?

If it has a low mA capability then you may just be 'stressing it a bit too much.

Note that green LEDs normally need a bit more power to achieve the same brightness as other colours. Commenting out the last two lines (as you stated) will mean the green LED remains on. Along with the U/S module power requirements and also the MicroMite power requirements then you may be exceeding things resulting in erratic readings.

So a quick test: leave the last two lines in, disconnect the LED and see if readings are back to 'normal'.

Better test - try a different power supply.

Also, do you have any caps on the power lines? I like to use 0.1uF caps on the MM if linking in 'other' hardware.

Finally note that a lot of the 'cheap clones' are base on chipsets that draw 'excessive' current. So do a quick check on mA being drawn from your PSU.

WW
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 06:12am 13 Apr 2015
Copy link to clipboard 
Print this post

I am hooked on this now!

Code looks fine so you can let the wife know she is safe!

I am convinced it is a Power supply issue you are having - otherwise maybe a 'faulty' U/S unit (surely not!!).

Check the things in my last post and let us know how you get on with this . . . .

WW
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1989
Posted: 11:20am 13 Apr 2015
Copy link to clipboard 
Print this post

I have exactly the same issue. I used a much more technical solution, I put a concrete block on the floor.
Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 11:54am 13 Apr 2015
Copy link to clipboard 
Print this post

I would try putting a delay in the main loop. The HC-SR04 needs time between readings.

I hang a tennis ball from the roof of the garage and when it hits the windscreen, it's time to stop. No power supply problems but I do need a longer bit of string for the side the MG parks on.

I am using some DYP-ME007Y modules for water level sensors in my tanks. They work the same as the HC-SR04 but use a single waterproof transducer with a long lead. They appear to be the same sensors that are used in car parking sensors.

Jim
VK7JH
MMedit
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 12:20pm 13 Apr 2015
Copy link to clipboard 
Print this post

Like Jim, I have a tennis ball which works well until the grand kids visit. They can't resist tugging on it and then it may need to be recalibrated. I have the cord running in a series of hooks across to the side wall of the garage where I can adjust the cord with another knot or two. I fill these tennis balls with silicone to weight them down a bit more.
Bob
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 12:52pm 13 Apr 2015
Copy link to clipboard 
Print this post

  TassyJim said   I would try putting a delay in the main loop. The HC-SR04 needs time between readings.

This was my initial thought but then you will see that the required delay is included in each of the LED subroutines.

 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 327
Posted: 02:40pm 13 Apr 2015
Copy link to clipboard 
Print this post

  TassyJim said  I am using some DYP-ME007Y modules for water level sensors in my tanks. They work the same as the HC-SR04 but use a single waterproof transducer with a long lead. They appear to be the same sensors that are used in car parking sensors.


@TassyJim:
Thanks for that piece of info... I was just getting ready to ask if anyone knew if the DYP-ME00Y7 was compatible with the HC-SR04. I been thinking I would love to mount one in the grill of the car. My mother (81 years young) always parks the car WAY back from the cars in front of her claiming she is about to hit them. When I get out the car and check, I find she is at LEAST 6 feet (2 meters) away. In her defense, the curvature of hood of her car does seem to have a weird optical illusion effect on how close you are to the objects in front of you.

--Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
busa
Regular Member

Joined: 12/02/2015
Location: Australia
Posts: 81
Posted: 02:53pm 13 Apr 2015
Copy link to clipboard 
Print this post

Thanks for the replies. @WW I thought of the 4 points you raised in your first post but dismissed them as being too costly (especially the last one!). I have been using a 5v 2.0A power supply so you would think that would be plenty. However disconnecting the green LED as you suggested does bring the readings back to normal. I have a 10uF cap on the 3.3v side of the regulator and have tried other psu's all giving unacceptable results. At the moment I don't have a multimeter handy to check the current draw on the psu (loaned it, I know, I know, should never loan your tools!). However, after looking back on some older posts I am beginning to think these units are very susceptible to current draw and not very reliable. @TassyJim, I was also thinking of obtaining some car parking sensors as I believe these would have to be much more reliable. Probably a visit to the wreckers may be in order here. Either that or as suggested by other posters a concrete block and/or a tennis ball but that would be admitting defeat!
Regards to all

By the way @TassyJim where did you source your DYP-ME007Y modules from?Edited by busa 2015-04-15
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 03:01pm 13 Apr 2015
Copy link to clipboard 
Print this post

eBay has heaps of cheap after market parking sensors. If you get desperate ... The hardest part is fitting them. A few years back I was going to get a sensor that was attached to the wall and showed traffic lights but I can't find any now. I think on-car sensors have obsoleted them.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 03:27pm 13 Apr 2015
Copy link to clipboard 
Print this post

@busa

It may be worth adding a couple of 100nF caps on the MM supply pins. If using a 28-pinner, then drop one across pins 27 & 28, and another between pins 8 & 13.

Are you running the U/S module from 5v supply (and not 3v3)? (just checking).

Once you get your multimeter back it will be worth measuring current (with and without U/S module); and also to measure the 5v output from the PSU (again, with and without the U/S module).

I can AirMail you a FREE concrete block if you like (but you must pay for the shipping!!)

WW



Edited by WhiteWizzard 2015-04-15
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 05:27pm 13 Apr 2015
Copy link to clipboard 
Print this post

@busa
search ebay for "DC 5V DYP-ME007Y Ultrasonic Sensor Module Board Measuring Range 30cm-3.5 M 40kHz"

A lot more expensive than the usual modules but I needed the waterproof sensor.
The lead to the sensor as about 2M long.

Are you running your sensor of 5V or 3.3V ?

Jim
VK7JH
MMedit
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2949
Posted: 02:02am 14 Apr 2015
Copy link to clipboard 
Print this post


Hi Busa,

Whilst I laud you for your efforts, I fixed a similar problem in our garage by tying a small rubber 'super ball' on a length of string.

When the ball taps the windscreen, STOP!!!!!!

Works perfectly and no batteries or power issues to worry about

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 02:30am 14 Apr 2015
Copy link to clipboard 
Print this post

Just in case you still want a technical solution, why not try this

 
busa
Regular Member

Joined: 12/02/2015
Location: Australia
Posts: 81
Posted: 11:24am 14 Apr 2015
Copy link to clipboard 
Print this post

Thanks everyone for the suggestions and ideas. I have found a solution of sorts. I find that if I shorten the pause in the sub allClear to 10 like this

sub allClear
pin(red) = 0
pin(amber) = 0
pin(green) = 1
pause 10 'make this pause shorter and comment out the other
? d
pin(green) = 0
' pause 500
end sub

it gives the appearance of a solid glowing green led and the U/S module is stable.
Not sure why it has to be done like that to make the U/S module give reliable readings but it appears to work ok. Have spent too much time on this already so now will set it up in the garage(with concrete block as backup for a while...) and see how it goes.
Regards

<Edit> No, didn't work. As soon as I disconnect the serial link the U/S throws a wobbly. Will just leave the pause there and put up with a flashing green led. This seems to be the only way I can get reliable readings.Edited by busa 2015-04-15
 
hwholmes
Newbie

Joined: 30/05/2014
Location: United States
Posts: 10
Posted: 05:57pm 21 Apr 2015
Copy link to clipboard 
Print this post

I think a previous poster had the right idea. When you delete the last TWO lines you are taking away the time delay for the green sub. Also when you disconnect the serial link prints you similarly reduce the time between executions.

My suggestion is to increase all your pauses statement durations and give the transducer time to function between readings. i.e. the flashing pauses are 2 * 100 + print string. To match it the green should be 200 + print string. to eliminate the print commands replace them with longer pause delays.

I would suggest you check the timing spec of the transducer and then give it more for comfort.

Bert
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025