20m2 and SR04 sensor

cachomachine

Senior Member
I would like to use multitasking with a Picaxe 20M2 and a SR04 ultrasonic distance sensor
How do i acheave a 10us pulse at 4Mhz to triger the sensor?
 

RNovember

Well-known member
Hello,
I don't exactly know what a SR04 is, but to send a 10us pulse at 4 megahertz, I would do this.
Code:
setfreq m4
pwm C.4,150,10
This just sends a 10us pulse at 4 megahertz out of pinC.4.

I don't know what the duty cycle needs to be, but you can change the second specification, 150.

There is a picaxe sight that has the rest of the info about pwm.

http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/pwm/
 

cachomachine

Senior Member
this works at setfreqm8 but not with setfreqm4
;Ultrasonic distance sensor

Symbol trig=c.4

Symbol echo=c.6

symbol inch=w12

distance:

setfreq m8

PulsOut trig,1 ;start a reading

PulsIn echo, 1, w13 ;return value in w13

let inch=w13*10/148/2 ;convert in inch in w13

debug

setfreq m4

goto distance
 

Technical

Technical Support
Staff member
Indeed. That is why you must use the same pin for both on that particular model sensor, as per the example linked.
 

darb1972

Senior Member
this works at setfreqm8 but not with setfreqm4
;Ultrasonic distance sensor

Symbol trig=c.4

Symbol echo=c.6

symbol inch=w12

distance:

setfreq m8

PulsOut trig,1 ;start a reading

PulsIn echo, 1, w13 ;return value in w13

let inch=w13*10/148/2 ;convert in inch in w13

debug

setfreq m4

goto distance
Hello

Just a tip. Best to put your code into the preferred forum format using the code insert function on the top task bar so it then looks like below. Good habit and particularly handy when posting large code snippets. Also makes it easier to follow and keeps your formatting, such as tabs/indents in the correct layout.

Code:
;Ultrasonic distance sensor


    Symbol trig=c.4
    Symbol echo=c.6
    symbol inch=w12

distance:

setfreq m8

PulsOut trig,1    ;start a reading

PulsIn echo, 1, w13 ;return value in w13

let inch=w13*10/148/2    ;convert in inch in w13

debug

setfreq m4

goto distance
 
Top