Using MaxSonar EZ1

sbscott

Senior Member
I am trying to build an accurate measuring 'eye' on my car robot (yep, back at it!). I have tried using the HC-SR04 but it is relatively unstable. I am hoping the EZ1 unit is more stable and accurate. But, not having much luck. Have tried the code posted awhile back, see below, but it very unstable. Any suggestions?
Code:
symbol varA = w0
symbol varB = w1
symbol varC = w2
symbol varD = w3
symbol varE = w4
symbol varX = w4

main:
setfreq m8
do
serin b.1, N9600_8, varA, varB, varC, varD, varE
'let varX = VarB - 48 * 10 + VarC - 48 * 10 + VarD - 48
debug
pause 600
loop
stop
 

inglewoodpete

Senior Member
Three points:
1. Please provide a link to the EZ1 device's datasheet. Help forum members to help you.
2. Asynchronous serial is a byte protocol, so putting bytes into word variables only complicates your software (and burns up limited register resources).
3. Does the EZ1 need a trigger pulse to start it's range finding?
 

neiltechspec

Senior Member
I have used a few of these sensors.
Unfortunately, the PDF is too large to upload here (I tried).
It outputs it rangefinding result continuously - free run by default.
It has a pin connection for each output type :-
Serial @ 9600,n,8,1 - RXXXcr inches
Analogue 0-5v - output is (VCC/512) Inches
PWM - pulse width is 147uS per inch

I found it's important to follow power up self calibration requirements for reliable operation.

I would agree with IP, using word variables complicates things, use byte variables instead.

I have never actually used one in anger with PICAXE, but do remember testing on an 08M2 with ease.

My use for them was with Quadcopters, before the government made it to difficult & Taxed it, so I gave that up.

Code:
'LV-MaxSonar EZ series test

#picaxe 08m2
#no_data

main:
    pause 1000
    setfreq m8
    serout c.0,n2400_8,(254,1)
    pause 100
    do
    serin c.1,n9600_8,("R"),b1,b2,b3,b4
    b1=b1-48
    b2=b2-48
    b3=b3-48
    w4=b1*100 + b2*10 + b3
    b5=w4 / 12
    b6=w4 // 12
    w10=w4*100
    w11=w10 / 40
    'sertxd("Distance ",#b5,"' ",#b6,34," or ",#w4,34," or ",#w11,"cm",13,10)
    serout c.0,n2400_8,(254,128," ",#b5,"f ",#b6,"in ")
    serout c.0,n2400_8,(254,192,"  ",#w11,"cm ")
    pause 1000
    loop
Neil
 
Last edited:

Aries

New Member
Not sure about this line of code:
Code:
w4=b1*100 + b2*10 + b3
I think it should be
Code:
w4=b1*10 + b2*10 + b3
because the arithmetic goes left-to-right, which means in the first case that b1 is multiplied by 1000. It only matters if you have numbers greather than 99, of course.
 

neiltechspec

Senior Member
I don't know your level of experience.

But, my test code was primarily displaying on an PICAXE LCD. SO comment those out & use
the sertxd line instead.
Don't use debug, that was probably screw things up.

Neil.
 

neiltechspec

Senior Member
Those sensors read up to 6.5 metres - 255.9 inches.
b1 is hundreds
b2 is tens
b3 is units.

So, I say
w4=b1*100 + b2*10 + b3
is correct.
 

AllyCat

Senior Member
Hi,
So, I say
w4=b1*100 + b2*10 + b3
is correct.
Sorry, no, Aries is correct. If b1 > 0 (1 metre) then it will contribute 1000 * b1 to the result, whilst b2 and b1 introduce only tens and units (of cms). PICaxe's strict left-to-right calculation multiplies b1 by both 100 and then by 10 (to give 1,000s of cms, or tens of metres).

But I do agree that it's much better to use SERTXT in preference to DEBUG.

Cheers, Alan.
 

neiltechspec

Senior Member
OK, I give in. It was at least 5 years ago. But for info :-
If b1 = 1 then that's 100 inches
It's in inches not cm (american product).

I guess, from the original posters question, he won't need b1 anyway (the hundreds digit).
Just b2 & b3 will give 6 to 99 inches (6 is the minimum it will read).

I only ever attempted to use it for below 6ft, never found it that accurate or reliable anyway
so abandoned it & went for TF-Mini-LIDAR modules which worked a treat.
 
Last edited:

hippy

Technical Support
Staff member
I think it should be
Code:
w4=b1*10 + b2*10 + b3
because the arithmetic goes left-to-right
That is correct, written out with brackets to show the left to right implicit precedence ...

w4 = ( ( ( b1 * 10 ) + b2 ) * 10 ) + b3
 
Top