Sonar test

hbl2013

Senior Member
I am in the process of developing a object avoidance toy car and have some problems getting the Sonar Detection system to work. I can not get the circuit anything to read and I don't know what part is not working, the Sonar Unit or the Software. Does anyone know how to trouble shoot it? I am using a AXE401 Shield Base (28X2) as the Controller and a HC-SR04 as the Sonar Unit. (The HR-004 uses a separate IN and OUT port). I wrote code just to check if I could see anything in three directions where there were different objects in each direction. The readings were all 'Zero". The code used is below.
(I did not convert the distance in meters or inches since I was only interested if I could detect anything.)
Any comments. anyone?



-------------------------------CODE HERE------------------------------------ (I know there is a way to post code, I forgot how to do it)
'Just to test Sonar 5 times at three positions

'Uses the 28X2 Shield and a range sensor HC-SR004 mounted on a servo.

'---------------------------------------------------------------------------

#picaxe 28X2

'Sensor PULSE = C.0,
'Detect Echo = A.0

symbol serv = B.7 'Servo Driver port

symbol LED1 = C.3 'Indicator Lights
symbol LED2 = C.4


symbol cntr=150 'Center position of servo
symbol rght=100 "Right
symbol lft=200 'Left

symbol trig= C.0 'define output pin for trigger pulse
symbol echo= A.0 'define input pin for echo pulse
symbol range= w1 '16 bit word variable for range

'LED Indicators

'left = LED1 = lit
'right = LED2 = lit
'straight LED1 + LED2 are lit
'pause = LED1 + LED2 are both OFF


low LED1
low LED2

symbol times = b8

let adcsetup = %0000000000000001

let b8=1

'Initialize Servo

servo serv,cntr
wait 2
high LED1
high LED2
wait 3

main: ' the main loop

sertxd ("Servo Range Center",CR,CR,LF)

low LED1
low LED2
wait 1

for b12 = 1 to 5
high LED1
high LED2
servopos serv,cntr
wait 1
pulsout trig,2 'produce 20uS trigger pulse
pulsin echo,1,range 'measures the range in 10uS steps
pause 10

sertxd (#b8,". Range Center Value is = ",#range,CR,LF)

b8=b8+1
pause 500
next

'Now the left side

sertxd ("Servo Range Left",CR,LF)
b8=1
high LED1
low LED2
for b12 = 1 to 5
servopos serv,lft
wait 1
pulsout trig,2 'produce 20uS trigger pulse
pulsin echo,1,range 'measures the range in 10uS steps

pause 10 'recharge period after ranging completes

sertxd (#b8,". Range Left Value is = ",#range,CR,LF)

b8=b8+1

pause 500
next


'Now the right side

sertxd ("Servo Range Right",CR,LF)
b8=1
for b12 = 1 to 5
low LED1
high LED2
servopos serv,rght
wait 1
pulsout trig,2 'produce 20uS trigger pulse
pulsin echo,1,range 'measures the range in 10uS steps
pause 10 'wait after ranging completes

sertxd (#b8,". Range Right Value is = ",#range,CR,LF)
b8=b8+1

pause 500

next

'we start all over again
b8=1

goto main

end
 

hippy

Technical Support
Staff member
It is probably best to start with a simpler program; trigger, read pulse back, report result, pause, repeat.

Running servos could cause some issues, especially if a servo pulse is generated between the trigger and the turn round for the PULSIN.
 

erco

Senior Member
Expect herky-jerky servo motion when reading ultrasonic sensors and using servo/servopos. You can run into timing conflicts, especially when there's no object detected yet PULSIN waits forever. You may get smoother operation using PULSOUT loops to drive your servo.

With servos, it's easier to use a Sharp IR sensor instead of an ultrasonic sensor. You'll use READADC and SERVO commands, which don't conflict.

BTW, you can simplify your program and save an I/O pin if you "hack" your sensor for one-pin operation per http://www.picaxeforum.co.uk/showthread.php?22639-HC-SR04&p=260470&viewfull=1#post260470

You can use the ULTRA command to read the sensor instead of PULSIN.
 

erco

Senior Member
Speaking of toy cars and obstacle avoidance sensors, have a look at this article: http://servo.texterity.com/servo/201512/?folio=44&pg=46#pg46

I had one input-only pin left on an 08M2, where I typically mount a 38 kHz IR receiver for remote control. Instead, I used a dollar IR sensor which emits a 38 kHz signal and looks for any reflection. It outputs a digital yes/no signal and the range is limited ( a foot or less depending on conditions) but it works in some situations. Additionally, the sensor can be used to receive IR remote control signals.

See also https://www.youtube.com/watch?v=mm7IKb4gnjc


 

hbl2013

Senior Member
Good ideas, erco. I am not too familiar with PICAXE, just learning how to use it. My code is mainly copies from others, modified for my application. I will try your suggestions, got my self some IR sensors and see what they can do instead of using Ultra Sonic units. ( just for the heck of it!)
I was aware of problems between servos and US pulses, but I thought I had separated their functions wide enough to avoid these.
BTW even with discontinuing the use of the servo, and using the Sonar unit in a fixed position, I still could not get any readings from it. Will try the IR method instead later.
After reading your reference material on the HR-04 it seems that the use of these units is more complicated than I thought, specifically, the use of correct timing turns out crucial for its operation.
 

erco

Senior Member
After reading your reference material on the HR-04 it seems that the use of these units is more complicated than I thought, specifically, the use of correct timing turns out crucial for its operation.
Oh that's not the case. If you mod the 04 for one pin operation, you just use the ULTRA command and it does everything for you, even converts to centimeters. Very simple, works great.

 

hbl2013

Senior Member
Oh that's not the case. If you mod the 04 for one pin operation, you just use the ULTRA command and it does everything for you, even converts to centimeters. Very simple, works great.
I am not familiar with the ULTRA Command, and can not find this command in the BASIC reference page. Can you tell me where I can find it and how to use it? Also can you give more details of the circuit and program you used in your sample video? Looks like a good way to test a unit that I can use.
 
Last edited:

erco

Senior Member
@hbl2013: Once you use ULTRA, you'll never go back! The only caveat is that it requires a 3-pin ultrasonic module. The dollar HC-SR04 types need a 1.8K resistor added. Mod details and discussion at http://www.picaxeforum.co.uk/showthread.php?27677-Help-with-HY-SRF05-ultrasonic-distance-sensor&p=287965&viewfull=1#post287965
Short test code shown below. Happy hacking and please let us know how you made out.

Code:
do			' start  loop
ultra 4,b0		' read ultrasonic sensor on pin 4, save cm distance as b0
sertxd (#b0,13,10)	' display b0 value in serial monitor
pause 200		' wait 200 ms
loop			' end do loop
 
Last edited:

hbl2013

Senior Member
@erco - Thanks, but you are confusing me. Which chip are you using and which I/O pin = 4? Also you show a digital readout and sertxd is a terminal program. How did you display the output on a digital readout and what hardware did you use?
 

erco

Senior Member
Last edited:
Top