Ultrasonic Sensor - Working but keeps dropping signal?

Blazemaguire

Senior Member
Hi,

I am making a simple project to show the kids on my electronics course what can be done with PIC chips and ultrasonics (the SRF04 module - I know its discontinued but I found it lying in a cupboard in my electronics classroom and its all I have!) - In essence its an ultrasonic range finder that displays the distance (under 1 meter) on 2 seven segment displays in real time.

- I have incoporated it into a circuit with an 18X chip linked to 2 4026 decade counters (one decade counter is providing a 'count out every 10th pulse' to count the second decade counter in a chain and by quickly pulsing the 4026's quickly I can get the 7 segments displays to display the range that the ultrasonic sensor is detecting - obviously the decade counters are in fact counting from zero to whatever value is being measured quicker than the eye can see - one of the PICs outputs is also connected to the reset pin of the 4026's to allow resetting of the displayed value when needed.

The ultrasonics and program appear to work, and the range is displayed on the 7 segments as intended, However, it seems to have an annoying intermittent fault that I dont know how to fix.

Even if its completley still, facing a solid wall it will randomly loose its signal every few seconds and then (bearing in mind the time it takes the 4026's to pulse from 0 to whatever measurement) it regains it - you can move it closer and further from the object and the range displayed will adjust accordingly, but it keeps fluctuating and losing its reading. - The strange thing is these 'fluctuations' happen in sync with an audible 'clicking' from the ultrasonic sensor module. - I find this strange as I diddnt think you were supposed to be able to 'hear' ultrasound?! (correct me if i'm wrong however as this is my first time playing with them!)

So.... - it appears to drop its signal in relation to an audible pulsing from the ultrasonics...... - Is there anything I might have done wrong in either my circuit or on the programme to cause this? - Or do I just have a faulty ultrasonic module? - have I missed an important pull up / pull down resistor on one of the input / output connections to the PIC?

I'm afraid i'm not too good with the BASIC language and I do all my programming using PIC logicator flowcharts - I had to teach myself PIC's from scratch when I started my first teaching job and discovered I was teaching GCSE electronics! - hence Logicator was the easiest route in.

Anyhow, included below is the 'code' generated from PIC logicator which i'm well aware is probably not the most efficient it could be..... if it helps.

'Converted on 5/4/2009 at 23:54:08


main:
let b0 = 0 'Expression command
let b1 = 0 'Expression command
label_345: let pins = 0 ' %00000000
label_362: pulsout 0, 2 'Start of Ultra command.
pulsin 1, 1, w6
pause 10
w6 = w6 * 10 / 58
let b0 = b12
if b13 = 0 then label_u362
let b0 = 255 'End of Ultra command.
label_u362:
label_356:
if b1 < b0 then label_360 'Compare command
if b1 = b0 then label_369 'Compare command
let b1 = 0 'Expression command
let pins = 32 ' %00100000
label_369:
if b0 > 99 then label_370 'Compare command
goto label_362

label_370: let pins = 32 ' %00100000
goto label_345

label_360: let b1 = b1 + 1 'Inc command
pulsout 4,1
let pins = 0 ' %00000000
goto label_356



'Notes
'Variables A-H are shown as B0 to B7 in BASIC


Thanks for your time......

Rob

P.S Does anyone have any links to intersting ultrasonics projects they've done or know off that I could also show the kids i'm teaching?
 

BeanieBots

Moderator
Being able to hear the transmit pulse is normal. True, humans cannot hear ultra-sound but when the transmitter is "whacked" by the pulse, it gives off more than just the resonant frequency. A bit like hitting a gong.

Try to eliminate as much as possible when fault finding.
Start with just controlling the US module and use sertxd or debug to read back the values. More likely than not, it's something in your display routine effecting things.
 

eclectic

Moderator
@ Blaze / Rob
First, follow BB's advice. Start simple.

Try using this, or something similar,
from the Programming Editor samples folder:
Code:
' Sample program for SRF04 ultrasonic range finder
' Note the NC pin on the module must be connected to 0V
' or else the SRF04 will not work with PICAXE.

symbol trig = 3            ' Define output pin for Trigger pulse
symbol echo  = 6           ' Define input pin for Echo pulse
symbol range = w1          ' 16 bit variable for range (remember w1 = b2 + b3)

main:
    pulsout trig,2         ' produces about 20uS pulse (must be minimum of 10uS)
    pulsin echo,1,range    ' measures the range in 10uS steps
    pause 10               ' SRF04 mandatory 10mS recharge period after ranging completes

' now convert range to cm (divide by 6.2) or inches (divide by 14.9)
' as picaxe cannot use fractions, multiply by 10 then divide by full number
' e.g. range x 10 then / 62 is the same as range / 6.2

    let range = range * 10 / 62 ' multiply by 10 then divide by 62 
    debug range			' display range via debug command

    goto main           	' and around forever
Second. re. your last question:

Last year, I prototyped a variable-distance trigger for wildlife photography.
http://www.picaxeforum.co.uk/showthread.php?t=8389&highlight=srf05+camera
You could easily create your own version.

Third, please have a look at this thread.

http://www.picaxeforum.co.uk/showthread.php?t=11783

If you're interested, I can supply your school with a
lucky-bag, which includes a new SRF005. (A spare, from the above project)

Last.
I know this may be iconoclastic, but, can Technical supply a Babel fish
for the Logicator Basic output? :)

e
 

bgrabowski

Senior Member
Interesting ultrasonic projects?

One of my pupils won the Young Engineer for Britain award about 5 years ago with the very equipment you have. It was an attachment to a fireman's helmet which warned him, via an LED display, of obstacles ahead in dark or smoke-filled areas.

A more straightforward project would be an attachment to a blind person's cane which beeped an earphone with increasing frequency as obstacles were approached. This was made as a GCSE project using a picaxe 08M and everyone wanted a to have go with it.
 

Blazemaguire

Senior Member
Thanks for your replies guys,

(and thanks for the links to the projects! they have given lots of scope for imagination!)

I have tried going right back to basics and used the 'debug' command in PIC logicator that allows you to take live readings from an analogue sensor just by 'programming' it with a debug programme and then leaving the serial connector plugged into the circuit.

- This displays the distance from 1 - 255 (in cm intervals) on the screen perfectly with no 'jumping' so as correctly diagnosed, its obviously not the sonic module.
- As has been said, its obviously something to do with my program but I dont know what to do to correct it! This is where I need to put some time into learning BASIC!


For information:
- The 4026 Decade counters clock pulse or 'count' pin is linked to output 4, and both the counters resets are connected to pin 5 (so if pin 5 is sent 'high' the counters will reset to display zero)

The ultrasonic module is connected to output '0' and input '1'.

- I want it to display the live measurement from the ultrasound module on the seven segments in 'cm' measurements - hence it could display 1 to 99 cm with the 2 seven segments I have. - If the range detected is greater than 99cm I want it to display '0' on both segs.

Don't suppose anyone could russtle up some code to achive this? - I know thats asking a lot. - If I see what the 'code' equivalent is, I might have some chance of seeing where I'm going wrong with the flow chart. - I know that 'dropping' the flowcharts are probably the easiest solution, but for a potential project for GCSE electronics kids, I need to stick with the flow chart programming method as thats as far as their skills stretch!

thanks so far

Rob
 

SilentScreamer

Senior Member
I think this will work (I have never used range finders or 4026s though). Test it if you can, if it doesn't work I expect someone can correct it.

Code:
symbol trig = 0 'Define output pin for Trigger pulse
symbol echo = 1 'Define input pin for Echo pulse
symbol range = w1 '16 bit word variable for range
symbol reset = 5
symbol clock = 4

main:
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10uS)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recharge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range * 10 / 58 'multiply by 10 then divide by 58
	goto display

display:
	'Is the range greater than 99 cm
	if range > 99 then
		'If true goto main
		goto main
		endif
	'Display the values on the LED displays
	'Loop the number of times specified by the range variable
	for b0 = 1 to range
		pulsout clock,2 'pulse the decade counter
	next 'go back to "for b0 = 1 to range" (unless the b0 variable is equal to the range variable
	goto main
Also BASIC can be used for GCSE students, my class (for the pupils able to use PICAXEs) all use BASIC over flowcharts.

EDIT: Try this code instead.

Code:
symbol trig = 0 'Define output pin for Trigger pulse
symbol echo = 1 'Define input pin for Echo pulse
symbol range = w1 '16 bit word variable for range
symbol reset = 5
symbol clock = 4

main:
	pulsout trig,2 'produce 20uS trigger pulse (must be minimum of 10uS)
	pulsin echo,1,range 'measures the range in 10uS steps
	pause 10 'recharge period after ranging completes
	'now convert range to cm (divide by 5.8) or inches (divide by 14.8)
	'as picaxe cannot use 5.8, multiply by 10 then divide by 58 instead
	let range = range * 10 / 58 'multiply by 10 then divide by 58
	goto display

display:
	
	'clear the display
	pulsout reset,10
	'Is the range greater than 99 cm
	if range > 99 then
		'If true goto main
		goto main
		endif
	'Display the values on the LED displays
	'Loop the number of times specified by the range variable
	for b0 = 1 to range
		pulsout clock,10 'pulse the decade counter
	next 'go back to "for b0 = 1 to range" (unless the b0 variable is equal to the range variable
	goto mainn
 
Last edited:

Technical

Technical Support
Staff member
A couple of suggestions, there doesn't seem to be much wrong with the ultra side of things.

Firstly, you don't have any wait anywhere in the system. It's therefore doing readings lots of time every second, do you really need this? Try adding a wait in the main loop before ultra to slow things down. The SRF004 does have a 'recharge' time, I guess you are going too fast for it when the values are the same (see www.rev-ed.co.uk/docs/srf004.pdf).

Secondly, when you use the 'outputs' cell, use the '-' ignore rather than red '0' for outputs you are not interested with at that moment in time. This will prevent the outputs cell affecting the other outputs that you are not interested with at that time.

As for babel fish, well, any flowchart tool only creates a BASIC equivalent of what is drawn by the user! The best way for others to understand your flowchart here is to do a screenshot of the flowchart and post it as a jpg attachment.

PS, latest version of Logicator is 3.2.x, you are still running a very old 2.x something!
 
Last edited:
Top