erco-laser range finder

AllyCat

Senior Member
Hi John,

There should be a number of threads on the forum about driving the SRF004/005s, but I've not used one myself. I do recall that there is a "trick" to make the PICaxe switch quickly from Transmit (a pulse) to Receive (PULSIN) on a single pin. I wouldn't expect the ultrasonics to be particularly affected by "loud" sounds, but I do recall that the early Remote Controlled TV sets used ultrasonics and could be "upset" by things like jangling keys on a keyring.

There is a current thread in the Active forum about the TOF10120 Laser Rangefinder (which appears to be the other "easy to use" device) and the Dronebots Workshop gives a useful comparative review of Ultrasonics versus Laser Time Of Flight.

The problem with Laser TOF is that the time delay is extremely short (nanoseconds), so it appears that these sensors use various "tricks", but the manufacturer won't give details even in their official data sheet. So unless size is a particular issue, personally, I would put my money on Ultrasonics (or there are also some rather interesting low cost "Radar" devices).

Generally it's best to describe your actual application in detail in an Active Forum post. But if you want to keep the details "secret", then give as much information as possible concerning the requirements for Range, Accuracy, Speed, Cost and the nature of any particular "Interfering" signals (i.e. Noise, Light Levels, Electrical Interference, etc.).

Cheers, Alan.
 

John F

Member
They look easy to use but for some reason I can't get mine going. I connected a 2.2 k resistor between echo and trigger and loaded this into an 08M2:

let dirs = %00010111
main:
ultra c.1, b1 'measure distance
if b1> 0 and b1 <10 then high b.4
'led on
else low b.4 'led off
end if
pause 200 '200 ms wait
goto main

I've tried adding a test line, b1=8, and sure enough the LED comes on but it won't come on for my hand waving in front of the sensor. Any thoughts on what I'm doing wrong?
 

AllyCat

Senior Member
Hi,
I connected a 2.2 k resistor between echo and trigger
I'm not sure where you were instructed to do that, but the ULTRA command does specify "The SRF005 module should be wired for "mode 2" operation (with pin 4 to 0V) so that it uses a single pin for both trigger and echo. This is described in the SRF005 datasheet. If you wish to use 'single pin mode' instead specify two pins (trigger, echo) in the command.". I'm not sure if that last sentence is a typo (for 'two pin mode') or means "specify the same pin twice".

The problem with a Go/NoGo type test is that you get little information if nothing happens. I would have tried a DEBUG or SERTXD... to the Terminal Emulator. Also that the power supply can easily deliver the required (up to) 50 mA (at around 4.5v) and of course try ALL the examples in the Datasheet. ;)

Cheers, Alan.
 

stan74

Senior Member
The input pin you use with pulseout is joined via a resistor to the sensor output measured with pulse in and converted to distance.
I think you make the pin output. Then turn it hi then turn it low. This will ping out.
The same pin is then made an input and pulsein used to get distance.
I may have posted the code under robots,can't remember.
 

John F

Member
Hi,

I'm not sure where you were instructed to do that, but the ULTRA command does specify "The SRF005 module should be wired for "mode 2" operation (with pin 4 to 0V) so that it uses a single pin for both trigger and echo. This is described in the SRF005 datasheet. If you wish to use 'single pin mode' instead specify two pins (trigger, echo) in the command.". I'm not sure if that last sentence is a typo (for 'two pin mode') or means "specify the same pin twice".


I was using an HC - SR04 (dead cheap at £2 and a nice connector available for an extra £) and there is an instruction elsewhere on the forum to make that modification. I have also tried the SRF005 from the Picaxe store (without the 2.2k resistor) and tied pin 4 to ground. Both without result. The code I was using comes directly from the SRF005 datasheet.

All the Best

John
 

stan74

Senior Member
I found this code for the picaxe robot I posted a video of.
This seems to use ping out on one pin and receive on another.
I'll see if I can find the picaxe code that uses 1 pin for ping out and in.

The idea isinstead of using pulse out, you just turn the tx ping on then off and it "rings" ie self oscillates at 38KHz.
Then make the same pin an input and USE PULSIN to measure the reading which can be converted to distance or used in a robot as "the minimum distance value to an object detected.

;***** Ultrasonic object avoiding robot in progress *****
init:
symbol minimumdistance = b3
let minimumdistance = 360 ; minimum distance to obstacle
symbol range = w4
symbol lmotordir = B.6
symbol rmotordir = B.7
symbol lping = B.1
symbol fping = B.2
symbol rping = B.3
symbol lecho = A.0
symbol fecho = A.1
symbol recho = A.2
input lecho,fecho,recho
output lping,fping,rping

let w0 = timer ; random

pwmout pwmdiv16, C.1, 124,0 ; 1000Hz @ 8MHz left motor
pwmout pwmdiv16, C.2, 124,0 ; 1000Hz @ 8MHz right motor

main: ;========================== Start =============================
high lmotordir ; motors
high rmotordir ; forward
gosub motors_fullspeed
;------------------------------- test if ahead blocked
pulsout lping,2
pulsin lecho,1,range
pause 15
if range < 240 then ;------------ left is blocked
goto turnright
end if

pulsout rping,2
pulsin recho,1,range
pause 15
if range < 240 then ;------------ right is blocked
goto turnleft
endif

pulsout fping,2
pulsin fecho,1,range
pause 15
if range < minimumdistance then ;------------ forward is blocked
low lmotordir ; short reverse motors
low rmotordir ; to stop quicker
pause 6
gosub motors_stop
else goto main
endif

;------------------------------- random test turn left or right
random w0
b2 = w0 // 255 + 1
if b2 < 128 then
pulsout rping,2 ;--------- test if right is clear
pulsin recho,1,range
pause 15
if range > minimumdistance then ;------ right is clear
goto turnright
else
goto turnleft
endif
else
pulsout lping,2 ;--------- test if left is clear
pulsin recho,1,range
pause 15
if range > minimumdistance then ;------ left is clear
goto turnleft
else
goto turnright
endif
endif
;......................................................
turnright:
gosub motors_slow
gosub rotate_clockwise

do
pulsout fping,2
pulsin fecho,1,range
pause 15
loop until range > minimumdistance ;-------- spin clockwise until no object ahead

do
pulsout lping,2
pulsin lecho,1,range
pause 15
loop until range > minimumdistance ;-------- spin clockwise until no object to left

gosub motors_stop
pause 100
goto main
;......................................................
turnleft:
gosub rotate_anticlockwise
gosub motors_slow

do
pulsout fping,2
pulsin fecho,1,range
pause 15
loop until range > minimumdistance ; ------- spin anti-clockwise until no object ahead

do
pulsout rping,2
pulsin recho,1,range
pause 15
loop until range > minimumdistance ; ------- spin anti-clockwise until no object to right

gosub motors_stop
pause 100
goto main
;======================================================
motors_stop:
pwmduty C.1,0 ; motors
pwmduty C.2,0 ; off
return
motors_fullspeed:
pwmduty C.1,499 ; motors
pwmduty C.2,499 ; full
return
motors_slow:
pwmduty C.1,124 ; motors
pwmduty C.2,124 ; 25%
return
rotate_clockwise:
high lmotordir ; left motor forward
low rmotordir ; right motor reverse
return
rotate_anticlockwise:
low lmotordir ; left motor reverse
high rmotordir ; right motor forward
return
motors_forward:
high lmotordir ; left motor forward
high rmotordir ; right motor forward
return


You can see the resistor mounted to the hsr04 so will search for correct code...but it was 2016 :(
 

Attachments

Last edited:

stan74

Senior Member
I found this but it's not picaxe and the basic idea which has been covered in picaxe somewhere or I would not have used it.

#define echo = portB.1 ;ultrasonic sensor trig pin.echo pin joined to trig by 1.8k resistor
do
ping
loop


ping:
retry:
dir echo out
set echo on
dir echo in
pulsein echo, range , 1 ms
wait 10 ms
if range = 0 then
goto retry
end if ;----out of range
return



I try not to use goto anymore but it can be useful.
 

stan74

Senior Member
Let's make it universal

select a pin for ping in-ping out
set portb.1 as output
portb.1=1 this is push the tx speaker out
portb.1=0 this lets it resonate at 38khz
set portb.1 as input
pulsein portb.1 to variable --- this is un-calibrated distance
 

John F

Member
Tried this but without result:
main:
high b.1 'push TX speaker out
pulsout b.1,200
'generates a 2 ms pulse
low b.1 'let speaker resonate at38 khz
pulsin b.1,1,b1
if b1>0 and b1 < 10 then high b4
else low b.4
'light comes on if object close
end if
pause 200
goto main
 

stan74

Senior Member
I uses pulsin so get a value that represents distance. it has to be converted to mm. but use the actual value instead.
#define echo = portB.1 ;ultrasonic sensor trig pin.echo pin joined to trig by 1.8k resistor.
range is a word value
ping:
retry:
dir echo out
set echo on
dir echo in
pulsein echo, range , 1 ms
wait 10 ms
if range = 0 then
goto retry
end if ;----out of range
return
 

stan74

Senior Member
Tried this but without result:
main:
high b.1 'push TX speaker out
delete-!!pulsout b.1,200-- It is supposed to self resonate at it's natural freq
'generates a 2 ms pulse
delete-!!low b.1 'let speaker resonate at38 khz NOT stop it by making lo
pulsin b.1,1,b1
if b1>0 and b1 < 10 then high b4
else low b.4
'light comes on if object close
end if
pause 200
goto main


not used picaxe for a while
main:
make b.1 output
high b.1
make b.1 input
pulse in variable, b.1...word value 0-ffff
test variable
goto main

you are using pulseout to ping but not needed, just turn it on then make it input, the tx cone will resonate and fade so catch it with pulse in.
this Does work and is "standard"...not cos my name's stan but the one pin srf04 way of use.
Hope that helps,best intentions.
 
Top