HC-SR04 not working on 4Mhz and 8Mhz

erbtech1963

New Member
Hi
Im using Picaxe 20X2 with HC-SR04 ultrasonic range finder, it is not working on setfreq m4 and m8 but its working at m16 and above. Please by any chance if someone can have a look at my code.

;Program for HC-SR04 ultrasonic range finder
;Target is 20X2

setfreq m8 ;Why only work from 16mhz up? why not on 4 to 8Mhz
DirsC = %00101000 ;C.3,C.5 set as output

symbol trig = C.5
symbol echo = C.3
symbol range = w1

main:
pulsout trig,2
pulsin echo,1,range
pause 10
debug range
 

Technical

Technical Support
Staff member
It's not recommended to use two pins on this sensor due to the sensors timing requirements. Use this instead (forum search)>





Try this for that particular HC-SR04 sensor, works here at 4MHz on 08M2 with just one i/o pin.

Connect echo to trigger with a 1K8 or similar, then connect trigger to a single PICAXE pin e.g. C.2

Then simply use:
Code:
High C.2
PulsIn C.2, 1, w0

This works as pulsin converts the pin to input again, and the pull down resistor on the module on the trigger pin pulls the trigger low again at that time.
It also means the pulsin command is immediately ready to catch the echo.
The 1K8 resistor means the echo output is not damaged by the trigger pulse from the PICAXE.
 

erbtech1963

New Member
It's not recommended to use two pins on this sensor due to the sensors timing requirements. Use this instead (forum search)>


Try this for that particular HC-SR04 sensor, works here at 4MHz on 08M2 with just one i/o pin.
Many thanks for your help
 

AllyCat

Senior Member
Hi,

Note also that the parameter in DEBUG range doesn't do anything useful; according to the Command Syntax : "DEBUG {var} Var - is an optional variable value (e.g. b3). Its value is not of importance and is included purely for backwards compatibility with older programs."

Thus the full list of all variables is always transferred, which "blocks" the running of the PICaxe's code for about 150 ms. If you need to see only the one variable and/or if time is critical, then you can use SERTXD(#range) to the standard PE6 Terminal Emulator (#terminal 9600 for an X2), which will take only around 1 - 2 ms.

Cheers, Alan.
 

erbtech1963

New Member
Hi,

Note also that the parameter in DEBUG range doesn't do anything useful; according to the Command Syntax : "DEBUG {var} Var - is an optional variable value (e.g. b3). Its value is not of importance and is included purely for backwards compatibility with older programs."

Thus the full list of all variables is always transferred, which "blocks" the running of the PICaxe's code for about 150 ms. If you need to see only the one variable and/or if time is critical, then you can use SERTXD(#range) to the standard PE6 Terminal Emulator (#terminal 9600 for an X2), which will take only around 1 - 2 ms.

Cheers, Alan.
Hi
Many thanks for the very useful info.
 
Top